我想复制文件夹的内容并排除“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:

希望能有所帮助。

10-07 19:19
查看更多