Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

scripting - using 7zip to zip in powershell 5.0

I have customized one powershell code to zip files older than 7 days from a source folder to a subfolder and then delete the original files from source after zipping is complete. The code is working fine with inbuilt Compress-Archive and Remove-Item cmdlets with less volume of files, but takes more time and system memory for a large volume of files. So, I'm working on a solution using 7zip instead as it's faster.

Below script does zipping correctly but not following the condition of only files older than 7 days and deletes all the files from source folder. It should zip and delete only files older than 7 days.

I have tried all possible ways to troubleshoot but no luck. Can anybody suggest possible solution?

if (-not (test-path "$env:ProgramFiles7-Zip7z.exe")) {throw "$env:ProgramFiles7-Zip7z.exe needed"} 
set-alias sz "$env:ProgramFiles7-Zip7z.exe" 

$Date = Get-Date -format yyyy-MM-dd_HH-mm 
$Source = "C:Users529817New folder1New folder_2" 
$Target = "C:Users529817New folder1New folder_2ARCHIVE"
Get-ChildItem -path $Source | sz a -mx=9 -sdel $Target$Date.7z $Source
question from:https://stackoverflow.com/questions/65909002/using-7zip-to-zip-in-powershell-5-0

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are several problems here. The first is that 7-Zip doesn't accept a list of files as a pipe, furthermore even if it did your GCI is selecting every file and not selecting by date. The reason that it works at all is that you are passing the source folder as a parameter to 7-Zip.

7-Zip accepts the list of files to zip as a command line argument:

Usage: 7z <command> [<switches>...] <archive_name> [<file_names>...] [@listfile]

And you can select the files you want by filter the output from GCI by LastWriteTime.

Try changing your last line to this

sz a -mx=9 -sdel $Target$Date.7z (gci -Path $Source |? LastWriteTime -lt (Get-Date).AddDays(-7) | select -expandproperty FullName)

If you have hundreds of files and long paths then you may run into problems with the length of the command line in which case you might do this instead:

gci -Path $Source |? LastWriteTime -lt (Get-Date).AddDays(-7) |% { sz a -mx=9 -sdel $Target$Date.7z $_.FullName }

Consider a temporary file with a list of those files which need to be compressed:-

$tmp = "$($(New-Guid).guid).tmp"
set-content $tmp (gci -Path $Source |? LastWriteTime -lt (Get-Date).AddDays(-7)).FullName
sz a -mmt=8 out.7z @$tmp
Remove-Item $tmp

Also looking at the parameters to 7-Zip: -mx=9 will be slowest for potentially a small size gain. Perhaps leave that parameter out and take the default and consider adding -mmt=8 to use multiple threads.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...