WinRAR批次文件,用於將每個資料夾中的第一個子目錄壓縮為zip

WinRAR批次文件,用於將每個資料夾中的第一個子目錄壓縮為zip

我正在努力尋找一個解決方案來掃描目錄中的所有資料夾,並在其中第一個/唯一子資料夾的內容的所述資料夾中建立一個 zip 檔案。

這是一個範例資料夾/結構/層次結構

I:\Unsorted\Sony - PlayStation Vita\NoNpDrm Physical\SENRAN KAGURA SV [PCSE00398] [NTSC]\PCSE00398

我想將裡面的內容PCSE00398壓縮成一個zip(同名)並留在I:\Unsorted\Sony - PlayStation Vita\NoNpDrm Physical\SENRAN KAGURA SV [PCSE00398] [NTSC]資料夾中。

答案1

@echo off 

set "_flag=a -ep1 -m5 -cfg- -y -o+"
cd /d "%~dp0" && for /d /r "I:\test\." %%i in (*
)do 2>nul tree.com /a "%%~dpnxi"|findstr /bl \\--- >nul || (
"%ProgramFiles%\WinRAR\Rar.exe" %_flag% "%%~i" "%%~dpni" |find/i ".rar" )

要到達子資料夾的最後一級,只需檢查每個子資料夾中是否有另一個資料夾,否則,壓縮該最後一級資料夾。

您可以使用for /d /r循環,它將遍歷所有資料夾,並在循環內的每個資料夾中,將命令tree與 一起使用findstr,您可以在其中檢查當前資料夾是否有更多子資料夾。

您可以在所有子資料夾中遞歸地使用tree "current_looping_folder" /afor /d /r,並透過檢查每個輸出尋找str“字串”"\---") 重定向到操作員||並採取行動(Rar) 如果未找到\---命令tree輸出中的該字串\---Last Folder

Folder PATH listing
Volume serial number is A0AD-DC56
F:\SUPER_USER\Q1599429
\---Last Folder

上面的輸出來自我所在的資料夾,F:\SUPERUSER\Q1599429這裡我們有Last Folder子資料夾,但是如果我在F:\SUPER_USER\Q1599429\Last Folder子資料夾中,我會得到以下輸出:

Folder PATH listing
Volume serial number is A0AD-DC56
F:\SUPER_USER\Q1599429\Last Folder
No subfolders exist

如果findstr沒有找到字串"\---",我在當前資料夾中沒有子資料夾,這是最後一個資料夾:

F:\SUPER_USER\Q1599429\Last Folder

在這種情況下,如果沒有子資料夾,命令命令將不會成功,然後讓操作員準確地在實際資料夾層級的最後一個資料夾中執行命令...tree "Actual_Loop_Folder" /a | findstr "\---"||Rar.exe

  • 觀察次數:1需要附加的\字元\來轉義findstr
 ... tree.com /a "%%~dpnxi"|findstr /bl \\--- ... 
  • 觀察次數:2這將嘗試說明||操作員如何機械地工作:
                         command1 || command2
                 execute command1 || only execute command2 (if) command1 fails
          if tree folder /a fails || there is no subfolder in it
      there is no subfolder in it || this is the the last subfolder
       this is the last subfolder || run rar flags in the \Last Folder
tree /a "%~fi"|findstr "\---">nul || Rar "I:\Unsorted\...\PCSE00398"
  • 觀察次數:3如果你需要運行WinRar.exe相反Rar.exe,只需刪除/替換/編輯:
"%ProgramFiles%\WinRAR\Rar.exe" %_flag% "%%~i" "%%~dpni" |find/i ".rar"

"%ProgramFiles%\WinRAR\WinRar.exe" %_flag% "%%~i" "%%~dpni" 
  • 觀察次數:4您可以編輯以使用自訂標誌/開關Rar/WinRar命令:
<Commands>
        a    ==  Add files to archive 

<Switches>

      cfg-   ==  Disable read configuration 
      ep1    ==  Exclude base directory from names 
  m<0..5>    ==  Set compression level (0-store...3-default...5-maximal) 
   o[+|-]    ==  Set the overwrite mode 
        y    ==  Assume Yes on all queries 
-----------------------------------------------------------------------
set "_flag=a -ep1 -m5 -cfg- -y -o+"

  • 一些進一步閱讀:

[√]For循環

[√]For/D 循環

[√]For /R 循環

[√]WinRar.exe | .rar.exe /?

[√]CMD/Bat 操作員 /dostips.com

[√]條件執行 || && ...

相關內容