powershell Compress-Archive压缩时排除指定文件名目录
在使用Compress-Archive命令进行压缩时,可以通过指定-ExcludePath参数来排除指定文件名的目录。
以下是一个使用Compress-Archive命令压缩时排除指定文件名目录的示例:
$sourcePath = "C:\Path\To\Source"
$destinationPath = "C:\Path\To\Destination"
$excludeDirectoryName = "DirectoryNameToExclude"
# 获取要压缩的文件和目录列表
$files = Get-ChildItem -Path $sourcePath -Recurse -File
$directories = Get-ChildItem -Path $sourcePath -Recurse -Directory | Where-Object { $_.Name -ne $excludeDirectoryName }
# 压缩文件和目录,并排除指定目录
$files | Compress-Archive -DestinationPath $destinationPath
$directories | ForEach-Object { $_.FullName } | Compress-Archive -DestinationPath $destinationPath -Update -ExcludePath $excludeDirectoryName
在上面的示例中,我们首先使用Get-ChildItem命令获取要压缩的文件和目录列表。然后,我们将文件和目录分别压缩,并使用-ExcludePath参数排除指定的目录。
请注意,Compress-Archive命令需要在Windows PowerShell 5.0或更高版本上运行
原文地址: https://www.cveoy.top/t/topic/hZQH 著作权归作者所有。请勿转载和采集!