Windows BAT中7zip压缩时排除某些目录
具体命令是:
"C:Program Files7-Zip7z.exe" a -t7z 文件名.7z 目录 -xr!子目录名称
代码语言:txt复制#mkdir -force "C:pathtoyour"
#del C:pathtoyouroutput.7z 2>$null
# 创建输出目录并删除现有的输出文件(如果存在)
New-Item -ItemType Directory -Path "C:pathtoyour" -Force
Remove-Item "C:pathtoyouroutput.7z" -ErrorAction SilentlyContinue
# 7-Zip 可执行文件路径
$7zPath = "$env:ProgramFiles7-Zip7z.exe"
# 要压缩的文件或文件夹路径
$sourcePath = "C:windowssystem32"
# 压缩后的输出文件路径
$outputPath = "C:pathtoyouroutput.7z"
# 排除的子目录
$excludedDirs = @(
"config",
"WebThreatDefSvc",
"catroot2",
"LogFiles",
"Microsoft",
"msmq",
"SleepStudy",
"Tasks",
"wbem",
"WDI",
"winevt",
"spp",
"sru"
)
# 生成排除参数
$excludeArgs = $excludedDirs | ForEach-Object { "-xr!$($_.Substring(0))" }
$excludeArgs
# 以最快速度(-mx1)压缩文件
$compressJob = Start-Job -ScriptBlock {
param($7zPath, $sourcePath, $outputPath, $excludeArgs)
& $7zPath a -mx1 $outputPath $sourcePath $excludeArgs 2>$null
} -ArgumentList $7zPath, $sourcePath, $outputPath, $excludeArgs
# 等待 10 分钟
Start-Sleep -Seconds 600
# 停止压缩作业
Stop-Job -Job $compressJob
# 接收压缩作业的结果(如果需要)
$jobResult = Receive-Job -Job $compressJob
# 删除压缩作业
Remove-Job -Job $compressJob
# 输出压缩作业结果
$jobResult
如果要显示过程中的报错,把 & $7zPath a -mx1 $outputPath $sourcePath $excludeArgs 2>$null 这一行结尾的 2>$null 去掉,报错的路径可以添加到这个模块的结尾
我是win11系统,system32目录排除上述子目录后,压缩为.7z有3GB多