使用命令行(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 參數。
-