如何在compress-archive中排除文件夹

Bal*_*zar 12 powershell

在压缩这样的存档时,我可以以某种方式排除文件夹吗?

$compress = Compress-Archive $DestinationPath $DestinationPath\ARCHIVE\archiv-$DateTime.zip -CompressionLevel Fastest
Run Code Online (Sandbox Code Playgroud)

现在它始终将整个文件夹结构保存$destinationpath到存档中,但由于存档位于同一文件夹中,因此它总是被压缩到新存档中,每次运行命令时存档都会变大.

Nko*_*osi 13

获取要压缩的所有文件,排除不希望压缩的文件和文件夹,然后将其传递给cmdlet

# target path
$path = "C:\temp"
# construct archive path
$DateTime = (Get-Date -Format "yyyyMMddHHmmss")
$destination = Join-Path $path "ARCHIVE\archive-$DateTime.zip"
# exclusion rules. Can use wild cards (*)
$exclude = @("_*.config","ARCHIVE","*.zip")
# get files to compress using exclusion filer
$files = Get-ChildItem -Path $path -Exclude $exclude
# compress
Compress-Archive -Path $files -DestinationPath $destination -CompressionLevel Fastest
Run Code Online (Sandbox Code Playgroud)

  • 排除规则仅对根文件夹生效,通配符规则对子文件夹不生效。在此示例中,子文件夹\xxx.zip 将被压缩到目标文件中。 (3认同)

Esp*_*o57 10

你可以使用Compress-Archive的-update选项.使用Get-ChildItem和Where选择您的子目录

喜欢它:

$YourDirToCompress="c:\temp"
$ZipFileResult="C:\temp10\result.zip"
$DirToExclude=@("test", "test1", "test2")

Get-ChildItem $YourDirToCompress -Directory  | 
           where { $_.Name -notin $DirToExclude} | 
              Compress-Archive -DestinationPath $ZipFileResult -Update
Run Code Online (Sandbox Code Playgroud)

  • 那么没有像 Linux 中那样简单的参数,如 `--exclude myfile` ? (2认同)