0

Am trying to extract a RAR file to a directory (C:\autoexe\source). Here the "autoexe" folder name changes everyday. The fullname does not change, the constant thing is "auto" in the string "autoexe", the exe part changes. I tried the below

for /f "delims=" %%a in ('dir /b "%cd%\samples\package.rar"') do start "" "%ProgramFiles%\WinRAR\WinRAR.exe" x -ibck "%cd%\samples\package.rar" *.* "%cd%\auto * \source"
Anil
  • 39
  • 1
  • 3
  • Sorry if there's a mistake from my side. And again sorry if I have wasted your time here. The code works fine if the destination directory is constant like :for /f "delims=" %%a in ('dir /b "%cd%\samples\package.rar"') do start "" "%ProgramFiles%\WinRAR\WinRAR.exe" x -ibck "%cd%\samples\package.rar" *.* "%cd%\autoexe\source" – Anil Sep 10 '17 at 16:52

1 Answers1

1

This commented batch code with error handling should do the job:

@echo off
rem Get name of first non hidden subfolder starting with auto in current folder.
for /D %%I in (auto*) do set "FolderName=%%I" & goto CheckArchive

echo Error: There is no auto* folder in: "%CD%"
echo/
pause
goto :EOF

:CheckArchive
if exist "samples\package.rar" goto ExtractArchive

echo Error: There is no file "package.rar" in subfolder "samples" of
echo        folder: "%CD%"
echo/
pause
goto :EOF

:ExtractArchive
"%ProgramFiles%\WinRAR\UnRAR.exe" x -c- -idq -y -- "samples\package.rar" "%FolderName%\"
if not errorlevel 1 goto :EOF

echo/
echo/
pause

Read text file Rar.txt in program files folder of WinRAR for details on the used switches on UnRAR command line or run UnRAR.exe from within a command prompt window without any option to get displayed a brief help on how to use this freeware console application.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • for /?
  • goto /?
  • if /?
  • pause /?
  • set /?

Read answer on Single line with multiple commands using Windows batch file for an explanation of & operator. And read the Microsoft support article Testing for a Specific Error Level in Batch Files.

Mofi
  • 46,139
  • 17
  • 80
  • 143