Tuesday, April 27, 2010

Create a Batch file to Archive or Extract using WinRAR

This will be a short article on how to program a simple batch file that will archive or extract files and folders using WinRAR software. This can be also used as command line syntax to do the same thing.

For this to work your WinRAR installation (default location: C:\Program Files\WinRAR) should have rar.exe and unrar.exe files which are installed by default when installing WinRAR software.

For this Example my document structure is as follows,

Given bellow is the example code for extracting a rar file named abc into the folder called core. Commands marked in bold will actually perform the operation.

  • First we must set the path to rar.exe or unrar.exe which we are going to use. In this case its unrar.exe
    set path="C:\Program Files\WinRAR\";%path%
  • Then we must give the path into which your rar file should be extracted.
    cd /d C:\WinRAR_Test\core 
  • Then we must give the location of the rar file to be extracted.
    unrar x ../abc.rar
    In here if we use the command as 'unrar e' all the files in all the folders of the rar file will be extraced to the folder mentioned in the step 2. But if we use the command as 'unrar x' we will be able to preserve the folder structure while extracting.
Given below is the completed batch program which need to be saved in a text file and renamed as somename.bat and double clicked to execute.

@echo off
@echo Extracting the file..
set path="C:\Program Files\WinRAR\";%path%
cd /d $INSTALL_PATH\core
unrar x ../abc.rar
@echo all files extracted
Hope you enjoyed this piece of code as much as I do!
Reference: http://www.respower.com/page_tutorial_unrar