Search code examples
batch-filewinrar

How to archive each folder in a directory separately using WinRAR?


I am trying to use WinRAR to compress all my different folders individually.

Example of folder content before

c:\projects\test
c:\projects\country
c:\projects\db

and after running the batch file

c:\backup\test.rar
c:\backup\country.rar
c:\backup\db.rar

I am trying the following command in a batch file. But it compresses all the folders in the projects folder being into the backup archive:

for /f "delims==" %%D in ('DIR C:\projects /A /B /S') do (
    "C:\Program Files\WinRAR\WinRAR.EXE" m -r "c:\backup\projects.rar" "%%D"
)

c:\backup\projects.rar contains all the files which I want in separate archives.

How to modify the 3 lines in batch file to get the desired archives?


Solution

  • I think you need to change a couple things.

    1. Change /A to /AD to get just the directories.
    2. Remove the /S so you will only get the top-level directories in C:\Projects.
    3. Inside your FOR loop, change the "c:\backup\projects.rar" to C:\Backup\%%D.rar"

    WARNING: This code is untested.

    FOR /F "DELIMS==" %%D in ('DIR C:\projects /AD /B') DO ( 
      "C:\Program Files\WinRAR\WinRAR.EXE" m -r "C:\Backup\%%D.rar" "%%D" 
    )