首页 > 系统相关 >使用命令行(powershell)压缩(7Z RAR)指定日期文件

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

时间:2024-04-20 11:44:05浏览次数:24  
标签:文件 Get 7z 7Z echo RAR file powershell

使用命令行(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 参数。

-

标签:文件,Get,7z,7Z,echo,RAR,file,powershell
From: https://www.cnblogs.com/runliuv/p/18147524

相关文章