0
我想在每個文件夾裏遞歸一些位置,例如: 例如:C:\ Logs和zip文件到同一個文件夾。PowerShell腳本來壓縮多個文件夾內的文件遞歸
我需要跳過每個.zip文件。我不知道如何讓我的腳本來做到這一點:
function New-Zip
{
param([string]$zipfilename)
set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
(dir $zipfilename).IsReadOnly = $false
}
function Add-Zip
{
param([string]$zipfilename)
if(-not (test-path($zipfilename)))
{
set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
(dir $zipfilename).IsReadOnly = $false
}
$shellApplication = new-object -com shell.application
$zipPackage = $shellApplication.NameSpace($zipfilename)
foreach($file in $input)
{
$zipPackage.CopyHere($file.FullName)
Start-sleep -milliseconds 500
}
}
$targetFolder = 'C:\Logs'
$now = Get-Date -Hour 0 -Minute 00 -Second 00
$days = 5
$lastWrite = $now.AddDays(-$days)
Get-ChildItem $targetFolder -Recurse -Exclude *zip* | Where-Object { $_ -is [System.IO.FileInfo] } | ForEach-Object {
If ($_.LastWriteTime -lt $lastWrite)
{
$_ | New-Zip $($targetFolder + $_.Name + ".zip")
$_ | Add-Zip $($targetFolder + $_.Name + ".zip")
}
}
的'CopyHere'方法是一個交互的shell功能,是不是d用於自動化(例如,不檢查錯誤)。我強烈推薦使用諸如'7z.exe'的命令行工具。 –