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
225 views
in Technique[技术] by (71.8m points)

archive - Zipping using powershell

I've written code to zip files older than 7 days from a source folder to a subfolder and then delete the original files. My code works best with Compress-Archive and Remove-Item cmdlets with fewer 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 code does zipping correctly but not limit itself to files older than 7 days and deletes all the files from source folder. It should zip and delete only files older than 7 days.

Is there anything wrong with the code.

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

$Days = "7"
$Date = Get-Date -format yyyy-MM-dd_HH-mm
$limit = (Get-Date).AddDays(-$Days)

$filePath = "C:Users529817New folder1New folder_2" 
 
Where LastWriteTime -lt $limit | 7z a -t7z -sdel "C:Users529817New folder1New folder_2ARCHIVE$Date.7z" "$filePath" 
question from:https://stackoverflow.com/questions/65836287/zipping-using-powershell

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

1 Reply

0 votes
by (71.8m points)

I don't think you are running the 7zip command correctly. You are simply telling it to add all the files from the directory $filepath to the archive then delete all the files. That and I have serious doubts that 7zip can take pipeline input as your sample suggests.

Look at the examples from 7Zip cmdline help:

7z a archive1.zip subdir

Adds all files and subfolders from folder subdir to archive archive1.zip. The filenames in archive will contain subdir prefix.

7z a archive2.zip .subdir*

Adds all files and subfolders from folder subdir to archive archive2.zip. The filenames in archive will not contain subdir prefix.

I'd have to download 7Zip to test but I think you need a loop to process the files you isolated with the Where clause. It might look something like:

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

$Days = "7"
$Date = Get-Date -format yyyy-MM-dd_HH-mm
$limit = (Get-Date).AddDays(-$Days)

$filePath = "C:Users529817New folder1New folder_2" 

Get-ChildItem $filePath |
Where-Object{ $_.LastWriteTime -lt $limit } | 
ForEach-Object{
    7z a -t7z -sdel "C:Users529817New folder1New folder_2ARCHIVE$Date.7z" $_.FullName
}

Note: At least in your sample you are missing the Get-ChildItem command. I don't think you need to reference the .Date property from the [DateTime] object returned by the .AddDays() method unless you want the boundary to be midnight of that date. Otherwise .AddDays() will return a [DateTime] naturally.


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

...