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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…