2012-09-18 123 views
2

我想創建PowerShell腳本功能來壓縮文件夾,這裏不包括一些文件夾和文件是我的代碼,其中創建壓縮文件,包括所有文件和文件夾PowerShell腳本 - 創建zip文件排除某些文件和文件夾

# Zip creator method 

function create-zip([String] $aDirectory, [String] $aZipfile){ 
    [string]$pathToZipExe = "C:\Program Files\7-zip\7z.exe"; 
    [Array]$arguments = "a", "-tzip", "$aZipfile", "$aDirectory"; 
    & $pathToZipExe $arguments; 
} 

但我想排除* .tmp文件和bin和OBJ文件夾

我發現Get-ChildItem "C:\path\to\folder to zip" -Recurse -Exclude *.txt, *.pdf | %{7za.exe a -mx3 "C:\path\to\newZip.zip" $_.FullName}工作正常的PowerShell命令,但如何使用它在腳本文件的功能

PLZ建議...

回答

1

如果一個班輪作品,然後把它在一個功能非常直截了當:

function Create-Zip([string]$directory, [string]$zipFile, [string[]][email protected]()) 
{ 
    $pathToZipExe = "C:\Program Files\7-zip\7z.exe" 
    Get-ChildItem $directory -Recurse -Exclude $exclude | 
     Foreach {& $pathToZipExe a -mx3 $zipFile $_.FullName} 
} 

如果您需要排除的目錄,然後將Get-ChildItem行更改爲:

Get-ChildItem $directory -Recurse -Exclude $exclude | Where {!$_.PSIsContainer} | 
+0

快一個 - 你在哪裏放置這些函數實際上是爲了讓powershell可以訪問它們 – pal4life

+0

你可以將上面的函數放在profile.ps1腳本中(位於$ home \ Documents \ WIndowsPowerShell下)。 PowerShell將在加載該腳本時處理該腳本,並且該功能將可用。 –

相關問題