runliuv

runliuv@cnblogs

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  710 随笔 :: 0 文章 :: 127 评论 :: 98万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

使用命令行(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   runliuv  阅读(289)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· SpringCloud带你走进微服务的世界
历史上的今天:
2023-04-20 由于找不到 XINPUT1_3.dll,无法继续执行代码。重新安装程序可能会解决此问题。
2019-04-20 C# .NET XML 序列化为对象,反序列化
点击右上角即可分享
微信分享提示