Search code examples
batch-filerarwinrar

Rar all files in subdirectories individually


I'm trying to use a batch file to rar all files in subdirectory. I found the following script online, which does the job, but it creates the RAR compressed version of the files in the parent directory. I'd like them to remain in their original directory.

:sindiv
echo.
echo.
for /R %b IN (*.*) do (rar a "%~nb" "%b" )
)
goto eof

Solution

  • You can try out this solution :

    @echo off
    Set "rar=%ProgramFiles%\WinRAR\Rar.exe"
    for /R %%b IN (*.*) do ( 
        If /I not "%%~xb" == "rar" (
            If /I not "%%~nxb" == "%~nx0" (
                echo "%rar%" a "%%~dpnb.rar" "%%~dpnxb" 
                "%rar%" a "%%~dpnb.rar" "%%~dpnxb">nul 
            )   
        )   
    )
    pause