[英]7zip - powershell exclude files/folders with variable
我正在嘗試壓縮文件夾,但使用powershell排除多個目錄。
我得到以下內容:
if (-not (test-path "$env:ProgramFiles\7-Zip\7z.exe")) {throw "$env:ProgramFiles\7-Zip\7z.exe needed"}
set-alias sz "$env:ProgramFiles\7-Zip\7z.exe"
$Source = "D:\Zip\*"
$Target = "D:\backup.7z"
$exclude = 'Customer\Images\*'
sz a -xr!'$Customer\Images\*' $Target $Source
這可以正常工作。但是當我想要更改-xr!
-xr!'$exclude'
或-xr!$exclude
出於某種原因停止工作。 上面分配的變量不會解析嗎?
嘗試這個...
if (-not (Test-Path "$env:ProgramFiles\7-Zip\7z.exe")) {throw "$env:ProgramFiles\7-Zip\7z.exe needed"}
$7ZipPath = "$env:ProgramFiles\7-Zip\7z.exe"
$Source = "D:\Zip\*"
$Target = "D:\backup.7z"
$foldersToExclude = 'folder1', 'folder2'
$7zipParams = @('a',$Target)
@($foldersToExclude) | ForEach-Object { $7zipParams += "`"-xr!$_`"" }
$7zipParams += $Source
Write-Host ($7ZipPath+' '+$7zipParams -join ' ') # debug - show the command line
&$7ZipPath $7zipParams
我們創建了一組參數以傳遞到7zip中,包括排除的文件夾。 在添加排除的文件夾時,我們在文件夾名稱前加上-xr!
標志並將它們包裝在雙qoutes中。
#debug
行將結果命令行輸出到控制台
通過使用7zip可執行文件創建一個字符串,然后在調用該字符串路徑的前面加上一個&號,我對您使用的7zip稍有不同。
此方法可以從最終存檔中排除多個文件夾,包括folder\\subfolder
結構。
兩件事情:
sz a "-xr!$exclude" $Target $Source
Customer\\Images\\*
排除目錄Images
中的所有文件,但在結果文件中包含一個空目錄。 如果您不想包括目錄(或其任何文件),請使用Customer\\Images
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.