[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文本处理] 【已解决】bat将当前目录往下的第三级子文件夹分别打包成zip或rar

bat将当前目录下的三级子文件夹分别打包成zip或rar
如下图:图3为压缩目标(文件夹名称不一定是123这些,有些可能是中文甚至有空格和特殊字符。

  1. <#*,:&cls
  2. @echo off
  3. pushd "%~dp0"
  4. powershell -NoProfile -ExecutionPolicy RemoteSigned -Command ". ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~0\" -ReadCount 0 | Out-String ))) "
  5. popd
  6. pause
  7. exit /b
  8. #>
  9. $dir2Compress = ".\*\*\*"
  10. Add-Type -AssemblyName System.IO.Compression.FileSystem -ErrorAction Stop
  11. foreach ($item in (Resolve-Path -Path $dir2Compress -Relative)) {
  12.   if (Test-Path -LiteralPath $item -PathType Container) {
  13.     try {
  14.       $zipfile = "$item.zip"
  15.       Write-Host "$item -> $zipfile" -ForegroundColor Green
  16.       Remove-Item -LiteralPath $zipfile -ErrorAction Ignore
  17.       [System.IO.Compression.ZipFile]::CreateFromDirectory($item, $zipfile, "Optimal", $true, [System.Text.Encoding]::UTF8)
  18.       
  19.     } catch {
  20.       $_ | Write-Host -ForegroundColor Red
  21.     }
  22.    
  23.   }
  24. }
复制代码
微信:flashercs
QQ:49908356

TOP

回复 2# flashercs

需要什么软件支持吗。我的是win7 64位,WinRAR.exe

TOP

rarPath设置为你winrar的路径
  1. @echo off & cd /d "%~dp0"
  2. set "rarPath=C:\Program Files\WinRAR"
  3. set "path=%path%;%rarPath%"
  4. set "dirList="
  5. for /d %%i in (*) do call set "dirList=%%dirList%% "%%i""
  6. for %%i in ("%cd%") do rar a "%%~nxi.rar" %dirList%
  7. pause&exit
复制代码

TOP

回复 4# went
大神,不行,这个只是把当前目录下所有文件打包。不是我想要的批量打包第三级目录,然后包就放在第三级目录

TOP

本帖最后由 flashercs 于 2021-1-23 15:13 编辑
  1. <#*,:&cls
  2. @echo off
  3. pushd "%~dp0"
  4. powershell -NoProfile -ExecutionPolicy RemoteSigned -Command ". ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~0\" -ReadCount 0 | Out-String ))) "
  5. popd
  6. pause
  7. exit /b
  8. #>
  9. $dir2Compress = ".\*\*\*"
  10. $rarexe="C:\Program Files\WinRAR\rar.exe"
  11. foreach ($item in (Resolve-Path -Path $dir2Compress -Relative)) {
  12.   if (Test-Path -LiteralPath $item -PathType Container) {
  13.     try {
  14.       $zipfile = "$item.rar"
  15.       Write-Host "$item -> $zipfile" -ForegroundColor Green
  16.       Remove-Item -LiteralPath $zipfile -ErrorAction Ignore
  17.       & $rarexe a $zipfile $item | Out-Null
  18.     } catch {
  19.       $_ | Write-Host -ForegroundColor Red
  20.     }
  21.   }
  22. }
复制代码
改成用WinRar,改下winrar安装路径
微信:flashercs
QQ:49908356

TOP

回复 6# flashercs


    感谢大神,不过我已经找到了,我把代码贴出来供大家借鉴学习。
  1. /*&cls
  2. @echo off
  3. rem 将一个指定目录下指定层级的子文件夹进行压缩打包
  4. set #=Any question&set @=WX&set $=Q&set/az=0x53b7e0b4
  5. title %#% +%$%%$%/%@% %z%
  6. set "mainfolder=C:\Users\Administrator\Desktop\aaaaaaaaa\"
  7. set "exefile=C:\Program Files\WinRAR\WinRAR.exe"
  8. set level=3
  9. if not exist "%mainfolder%" (echo;"%mainfolder%" not found&pause&exit)
  10. if not exist "%exefile%" (echo;"%exefile%" not found&pause&exit)
  11. if "%mainfolder:~-1%" equ "\" set "mainfolder=%mainfolder:~,-1%"
  12. for /f "delims=" %%a in ('dir /ad-h/b/s "%mainfolder%\"^|cscript -nologo -e:jscript "%~f0" "%mainfolder%" "%level%"') do (
  13. echo;"%%a"
  14. "%exefile%" a -y -ep1 -r "%%~dpa%%~na.rar" "%%a\*"
  15. )
  16. echo;%#% +%$%%$%/%@% %z%
  17. pause
  18. exit
  19. */
  20. var fd=WSH.Arguments(0);
  21. while(!WSH.StdIn.AtEndOfStream){
  22. var it=WSH.StdIn.ReadLine();
  23. var arr=it.substring(fd.length).split('\\');
  24. if(arr.length == Number(WSH.Arguments(1))+1){
  25. WSH.echo(it);
  26. }
  27. }
复制代码
set "mainfolder=C:\Users\Administrator\Desktop\aaaaaaaaa\"(后面必须带斜杠,不然就会把根目录下所有文件打包,开始就是这样以为这代码不能用。)
level=3(这里设置文件夹层数)

TOP

回复 5# 7803181
  1. @echo off & cd /d "%~dp0"
  2. setlocal enabledelayedexpansion
  3. set "rarPath=C:\Program Files\WinRAR"
  4. set "path=%path%;%rarPath%"
  5. set "dirList="
  6. for /r /d %%i in (*) do (
  7. pushd "%%i"
  8. for /l %%a in (1,1,2) do cd ..
  9. if "!cd!\"=="%~dp0" (
  10. echo pack %%i
  11. pushd "%%i"
  12. set "dirList="
  13. for /d %%i in (*) do call set "dirList=!dirList! "%%i""
  14. for %%i in ("!cd!") do rar a "%%~nxi.rar" !dirList!
  15. popd
  16. echo ------------------
  17. )
  18. popd
  19. )
  20. pause&exit
复制代码

TOP

回复 8# went
感谢,你这也可以用,

TOP

返回列表