使用命令行(powershell)压缩(7Z RAR)指定日期文件

使用命令行(powershell)压缩(7Z RAR)指定日期文件,powershell ,7z。

 

WINDOWS命令行是无法按时间过滤文件的,我们通过powershell 里的Get-ChildItem + Where-Object 来过滤文件。

 

本例子是powershell +7z,RAR的自行更改。

 

echo "--  开始了 --"
# 只压缩今天编译出来的文件,根据实际情况修改
$targetDate = (Get-Date)  
echo $targetDate
# 将今天的文件集中到 $files 变量中
$files = Get-ChildItem -Path "D:\WebPub\O2OAdminWeb\bin\" -File | Where-Object { $_.LastWriteTime.Date -eq $targetDate.Date } 
echo "有以下文件将被压缩:"
foreach ($file in $files) {  
     echo $file.FullName
}
# 设置zip 的全路径
$outputPath = "d:\Temp\O2OAdminBin.zip"  
# 如果 7z.exe 不在系统路径中,请提供其完整路径 
$rarPath = "C:\Program Files\7-Zip\7z.exe"   

# 循环向压缩包添加文件
foreach ($file in $files) {  
    # $rarPath 之前要加 & 符号,否则 a 会报错,注意:要用 FullName
     & $rarPath a -tzip $outputPath $file.FullName
}

echo "--  完成 --"

 

我这里的实际情况不需要子目录的东西,所以Get-ChildItem 用了  -File 参数。

-

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章