2012-09-18 121 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将在加载该脚本时处理该脚本,并且该功能将可用。 –

相关问题