繁体   English   中英

powershell脚本-创建不包含某些文件和文件夹的zip文件

[英]powershell script - create zip file excluding some files and folders

我正在尝试创建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} Get-ChildItem "C:\\path\\to\\folder to zip" -Recurse -Exclude *.txt, *.pdf | %{7za.exe a -mx3 "C:\\path\\to\\newZip.zip" $_.FullName}可以作为powershell命令正常工作,但是如何在脚本文件功能中使用它

Plz建议...

如果该单行代码有效,则将其放入函数中非常简单:

function Create-Zip([string]$directory, [string]$zipFile, [string[]]$exclude=@())
{
    $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} |

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM