Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a folder full of pdfs and I'm tired of Winraring each file separately. I don't want to put them all in one archive.

Isn't there a command line I can use to make Winrar compress a file with best method, delete the file after making the .rar and then do the same for the next file?

share|improve this question

migrated from stackoverflow.com Jan 1 '13 at 7:32

This question came from our site for professional and enthusiast programmers.

2  
I think writing a batch/script file is more convenient. – nhahtdh Dec 31 '12 at 10:52

Yes, there is an executable rar.exe at WinRAR install folder. Running it without parameters shows you full help on its usage. For example, you set best compression level with -m5 switch.

As it's been said, batch/shell script is a good way to do it, but you may find this project with similar goals interesting: http://code.google.com/p/autorar/ .

share|improve this answer

This is a script i wrote to compress each file separately with rar archiver (winrar5 format). To compress all *.pdf files in a folder separately you must put this script in a .bat file, then run it. First you specify a start(root) directory where your files are located, then you can specify extensions to find and compress in that dir (recursive searching,entering subdirectories of root dir)

@echo off

REM -m5 max compression    
REM -md64m dictionary size 64m
REM -s solid
REM -ma5 rar5 format (rar4 - ma4)
REM -y yes to all
REM -t test after packing (if test NOT ok, then files will not be deleted if the -df/-dr specified)
REM -df delete files after successful archivation
REM -ep1 exclude base folder of file from archive
:: %%~nZ - file name extracted from var Z
:: %%~fZ - full path of file+ext from var Z
:: %%~dpZ - disk+path
:: setlocal - use local environment vars
setlocal

:step1
    echo Enter start path where files a located
    set /p rarpath=
    echo.
    :: remove doublequotes from path
    set rarpath=%rarpath:"=%

:check dir exist
    if not exist "%rarpath%" echo   "!!!  [%rarpath%] doesn't exist, please enter correct path  !!!"
    if not exist "%rarpath%" echo.
    if not exist "%rarpath%" goto step1
:::::
:::::
    echo.
    echo Enter which extensions to compress (like *.mp4;*.wmv)
    set /p exten=
    echo.
:::::
:::::
    echo.
    echo Final command: 
    echo for /R "%rarpath%" %%Z in (%exten%) do "%programfiles%\WinRAR\rar.exe" a -ma5 -m5 -s -md64m -t -y -ep1 -df "%%~nZ.rar" "%%~fZ"
    echo.
:::::
:::::
    echo.
    echo Continue? [y/n]
    set /p go=
    if /i %go%==Y goto ok
    if /i %go%==N goto exit

:ok
:: CD to each file's directory, compress file, log output (codepage 866)
set rartime=%time:~0,8%
set rartime=%rartime::=_%
set rartime=%rartime: =%
set rarlog=rar_log_%date%_%rartime%.txt
:: For directories located in current directory
:: for /d %D in (*) do "%programfiles%\winrar\rar.exe" a -ma5 -m5 -s -md64m -t -y -ep1 -df "%D.rar" "%D"

    for /R "%rarpath%" %%Z in (%exten%) do (
        cd /d %%~dpZ
        "%programfiles%\WinRAR\rar.exe" a -ma5 -m5 -s -md64m -t -y -ep1 -df "%%~nZ.rar" "%%~fZ" >> c:\%rarlog% 2>&1
        type c:\%rarlog%
        echo. >> c:\%rarlog%
        echo ----------------------------------------------------------------------------- >> c:\%rarlog%
        echo. >> c:\%rarlog%
    )
    echo.
    echo.
    echo        !!! All Done !!!
    pause
    goto :EOF
    ::EXIT /B 0

:exit
    echo Program terminated
    pause
    goto :EOF
share|improve this answer

You must log in to answer this question.