Simply compress 1 folder in batch with WinRAR command line?
Asked Answered
I

1

14

Using the WinRAR command line (C:\Program Files\WinRAR\rar.exe), all I'm trying to do is compress a single folder (C:\Users\%username%\desktop\someFolder) and possibly change the name of the .zip file created. I've tried just "rar.exe a "C:\Users\%username%\desktop\someFile" and it works, but it compresses another folder (not the one I put).

What am I doing wrong?

Can you also provide explanation (and maybe tell me what recursion is because I'm unfamiliar with it)?

Thanks

Indigenous answered 24/6, 2014 at 23:25 Comment(0)
E
58

Use either

"%ProgramFiles%\WinRAR\Rar.exe" a -ep1 -idq -r -y "Name of RAR file with path" "%UserProfile%\Desktop\someFolder"

or

"%ProgramFiles%\WinRAR\Rar.exe" a -ep1 -idq -r -y "Name of RAR file with path" "%UserProfile%\Desktop\someFolder\"

to create a RAR archive file with the specified name after command a (add to archive) and the switches

  • -idq ... enable quiet mode to display only error messages,
  • -ep1 ... exclude base directory from specified file/folder names,
  • -r ... recursively archive/compress all files and subdirectories,
  • -y ... assume Yes on all queries.

The folder someFolder is included in the archive with first command line without a backslash at end.

The folder someFolder is NOT included in the archive, just the files and the subdirectories of this folder, with second command line with the backslash at end.

In other words option -ep1 results in omitting everything from path up to last backslash in specified file or folder name on adding the file or folder to the archive which explains the difference on adding a folder without or with backlash at end specified on command line.

Recursion means to add not only the files in the specified folder, but also all subfolders and all files in all subfolders.

So RAR must search first in the specified folder for a subfolder. If found go into this subfolder and search again for a subfolder. If one is found, go into this subfolder and search for a subfolder. If no one found, add the files in this subfolder into the archive or just the folder name if the subfolder is empty. Then go back to parent folder and continue searching for next subfolder. If none is found, add the files of this subfolder. Then go back to parent folder and continue search for subfolder, and so on.

As you can read, the same procedure is done again and again for each branch of the entire folder tree until all subfolders were processed. This is done using a recursion. The subroutine searching for subfolders calls itself every time a subfolder is found.

NOTE:

Console version Rar.exe supports only creation/extraction of RAR archives. It does not support ZIP archives. This is clearly written in text file Rar.txt at top which is the manual for console version of WinRAR. It would be necessary to use WinRAR.exe instead of RAR.exe to create ZIP archives.

Example 1:

"%ProgramFiles%\WinRAR\WinRAR.exe" a -afzip -ep1 -ibck -r -y "Name of ZIP file with path" "%UserProfile%\Desktop\someFolder"

Example 2:

"%ProgramFiles%\WinRAR\WinRAR.exe" a -afzip -ep1 -ibck -r -y "Name of ZIP file with path" "%UserProfile%\Desktop\someFolder\"

GUI version WinRAR.exe has many commands and switches identical to console version Rar.exe, but there are differences as shown here with -afzip supported only by WinRAR.exe and -ibck instead of -idq to run WinRAR in background which means minimized to system tray instead of in foreground with a visible progress window.

For help on creating WinRAR.exe command line start WinRAR, click in last main menu Help on first menu item Help topics, select help tab Contents, expand list item Command line mode and make use of the help pages:

  • Command line syntax
  • Alphabetic commands list
  • Alphabetic switches list

It is advisable to read the help pages in listed order respectively the text file Rar.txt from top to bottom on creating the WinRAR.exe or Rar.exe command line for usage in a batch file or in a shortcut file (*.lnk).

Ehrlich answered 25/6, 2014 at 6:13 Comment(2)
thanks a lot for that details information you help me a lot there is one question came into my mind... is there way to check if compressing process successfully completed within batch fileAccordance
@Accordance Open text file Rar.txt in program files folder of WinRAR and read the section about Exit values. Help of WinRAR contains also the page List of WinRAR exit codes. So the condition if errorlevel 1 goto NoneSuccessful can be used on next command line to jump to label NoneSuccessful on exit value being greater or equal 1. The exit value of Rar.exe and WinRAR.exe is assigned to environment variable ERRORLEVEL as it is done for any executed application.Ehrlich

© 2022 - 2024 — McMap. All rights reserved.