好的,所以我还在学习,所以请像对待白痴一样对待我。 我已经汇集了来自各种来源的脚本,这些脚本将在文件夹中获取具有给定扩展名和年龄的所有文件,并使用7zip压缩文件。 我还想添加的是一种方法,可以删除成功压缩的文件。 现在7zip在每个文件之后返回一条消息:“一切正常”,所以我猜想有一些方法可以采取这种方式并将该文件标记为删除。
我当前的压缩代码低于任何帮助或删除过程的提示将是非常棒的。 卢卡斯
<#
This script compresses all files with given extension in the set folder
and add to daily archive ".zip" using 7izp.
#>
#### 7 zip variable I got it from the below link
#### http://www.7-zip.org/
# Alias for 7-zip
if (-not (test-path "$env:ProgramFiles\7-Zip\7z.exe")) {throw "$env:ProgramFiles\7-Zip\7z.exe needed"}
set-alias sz "$env:ProgramFiles\7-Zip\7z.exe"
############################################
#### Variables
# The location of the files you want to compress
$filePath = "D:\temp"
# select extension, use .* for all
$e = ".pdf"
# Select in days how old a file must be to be processed.
$d = 10
#get the files in the folder the match requirements
$ed = Get-ChildItem -Recurse -Path $filePath | Where-Object { _.Extension -eq "$e" -and $_.CreationTime –lt (Get-Date).AddDays(-$d) }
#Gets current date for creating daily archive
$date1 = Get-Date -Format "yyyyMMdd"
#Gets current dir for using in archive folder name
$path = Split-Path $filepath -Leaf
###########################################
############Function
foreach ($file in $ed) {
$name = $file.name
$directory = $file.DirectoryName
$zipfile = "$path-$date1.zip"
sz a -tzip "$directory\$zipfile" "$directory\$name"
}
########### END OF SCRIPT ##########