gpt4 book ai didi

powershell - 复制项目排除一个目录

转载 作者:行者123 更新时间:2023-12-03 00:07:45 43 4
gpt4 key购买 nike

我想复制文件夹的内容并排除“Cookies”。

我已经尝试了一些提供的类似问题的解决方案,但是它们对我没有用。

$excludes = "Cookies"
New-Item -Path $newdir -Type Directory -Name "AppData"
Copy-Item -Path (Get-Item -Path $path"\AppData\*" -Exclude ($excludes)).FullName -Destination $newdir"\AppData" -Recurse -Force

我只想复制目录的内容,不包括1个文件夹。

我正在使用PowerShell V5.1

最佳答案

该代码无效Get-Item -Path $path"\AppData\*",PowerShell无法将变量和字符串连接在一起。将代码更改为:

$excludes = "Cookies"
New-Item -Path $newdir -Type Directory -Name "AppData"

# Join the path correctly
$joinedPath = Join-Path $path "AppData\*"
Copy-Item -Path (Get-Item -Path $joinedPath -Exclude ($excludes) -Directory).FullName -Destination $newdir"\AppData" -Recurse -Force
仅供引用:请注意 -Exclude开关仅在路径中包含通配符时才起作用(在问题中正确完成)。 Source:

-Exclude

Specifies, as a string array, an item or items that this cmdlet excludes in the operation. The value of this parameter qualifies the Path parameter. Enter a path element or pattern, such as *.txt. Wildcard characters are permitted. The Exclude parameter is effective only when the command includes the contents of an item, such as C:\Windows*, where the wildcard character specifies the contents of the C:\Windows directory.


希望能有所帮助。

关于powershell - 复制项目排除一个目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57178111/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com