batch-filewinrar

Archive files inside subfolder output name


Inside "C:\Test" there is multiple folders. and inside those folder there is files. so far i able to make the batch script only issue i having is i want the archive name based on sub folder name. like

"C:\Test\Abc" => "C:\Test\Abc\Abc.rar"

i set directory name but it's using the root folder Test.rar for all archive name. i tried other variable but did not work. here is the batch script, any help please?

@echo off
for %%I in (.) do set CurrDirName=%%~nxI



FOR /D %%F IN ("C:\Test\*") DO (
    
    ECHO.
    ECHO Switching to folder '%%F'
    
 for %%a in ("%%F") do (
    pushd "%%~a"
    "C:\Program Files\WinRAR\rar.exe" -x*.txt a -r -- "%CurrDirName%.rar"
     RMDIR /s /q "%%S"
      )
    
    ECHO.
    
)

PAUSE

Solution

  • Maybe better first change folder to c:\Test and then run for in it

    cd test
    for /d %%f in (*) do (
      pushd %%f
      "C:\Program Files\WinRAR\rar.exe" -x*.txt a -r -- "%%f.rar"
      popd
    )