runliuv

runliuv@cnblogs

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

使用命令行(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 } 

$tmpBinDir = "D:\Temp\O2OAdminBin"
#先删除临时文件夹,防止不需要的文件被压缩
Remove-Item -Path $tmpBinDir -Recurse -Force
#再新建
New-Item -ItemType Directory -Path $tmpBinDir

echo "有以下文件将被压缩:"
foreach ($file in $files) {  
     echo $file.FullName
     # 把要压缩的文件复制到临时目录,方便打包ZIP
     Copy-Item -Path $file.FullName -Destination $tmpBinDir
}
# 设置zip 的全路径
$outputPath = "d:\Temp\O2OAdminBin.zip"  
Remove-Item -Path $outputPath -Force -Confirm:$false
# 如果 7z.exe 不在系统路径中,请提供其完整路径 
$rarPath = "C:\Program Files\7-Zip\7z.exe"   

& $rarPath a -tzip $outputPath $tmpBinDir

echo "--  完成 --"

 

 

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

-

posted on 2024-04-20 11:40  runliuv  阅读(1)  评论(0编辑  收藏  举报