循环遍历所有子文件夹,压缩内容

时间:2015-11-02 17:27:24

标签: powershell for-loop zip

我试图压缩超过31天的所有子目录中的所有文件。

以下代码主要有效,但有些不对劲。它根据需要创建新文件夹并将其拉链。

但是,它似乎并没有将旧文件移动到新创建的目录中,只要它们被压缩。第Get-ChildItem -Exclude *.zip | ? {$_.LastWriteTime -lt (Get-date).AddDays(-31) -and -not $_.psIsContainer} | Move-Item -destination $newpath行应该这样做,但不是。当我尝试在控制台中单独运行它时,它可以正常工作。

此外,在文件夹树中,此代码不能在树的外边缘上工作。我的意思是最后一个文件夹中没有创建新文件夹。

有谁知道我哪里出错?

function zipfiles ($path,$name){

$date = "$((Get-Date).ToString('yyyy-MM-dd'))"

$newpath = "$path$date"

New-Item -ItemType Directory -Path $newpath

Get-ChildItem -Exclude *.zip | ? {$_.LastWriteTime -lt (Get-date).AddDays(-31) -and -not $_.psIsContainer} | Move-Item -destination $newpath

[Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" )
$src_folder = $newpath
$destfile = "$newpath.zip"
$compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
$includebasedir = $false
[System.IO.Compression.ZipFile]::CreateFromDirectory($src_folder,$destfile,$compressionLevel, $includebasedir)

Remove-Item $newpath -recurse
}

Get-ChildItem -Recurse -Directory | ForEach-Object { zipfiles($($_.FullName),$($_.Name))}

0 个答案:

没有答案
相关问题