manu muraleedharan
manu muraleedharan

Reputation: 425

How do you loop inside sub directories in windows, and repeat command using windows batch scripting

I am on Windows 10. I have a folder with sub folder. All subfolders have "Subs" folder inside them. I want to loop through all subdirectories, goto Subs directory, unrar a file, Goto next subdirectory and repeat.

I tried below script but could not get to work.

@echo off
setlocal enableextensions enabledelayedexpansion

set "rootFolder=C:\Users\MNM\MAT"

 echo ----------------------------------------------------------------------
for /d /r "%rootFolder%" %%a in (.) do (
    set mypath=%cd%
    @echo %mypath%
    cd %%a
    set mypath=%cd%
    @echo %mypath%
    cd Subs
    set mypath=%cd%
    @echo %mypath%
    C:\Users\MNM\MAT\unrar e *subs.rar C:\Users\MNM\mat2\
    cd C:\Users\MNM\MAT
)

Upvotes: 0

Views: 322

Answers (3)

Compo
Compo

Reputation: 38579

Just because recursing all subdirectories and extracting all *subs.rar files wasn't requested here's an example that is based upon my assumptions:

@ECHO OFF
SET "rootDir=%USERPROFILE%\MAT"
IF /I NOT "%CD%"=="%rootDir%" CD /D "%rootDir%"
FOR /D %%A IN (*
) DO IF EXIST "%%A\Subs\*subs.rar" UNRAR e "%%A\Subs\*subs.rar" mat2\

Upvotes: 0

user6811411
user6811411

Reputation:

This is one possible way if I understood your folder structure correctly:

@echo off
set "Base=C:\Users\MNM\MAT"
echo ----------------------------------------------------------------------
for /F "delims=" %%A in (
  'dir /B/S "%Base%\*subs.rar" ^| findstr /i "^%Base:\=\\%\\[^\\]*\\Subs\\[^\\]*subs.rar$"'
) do Echo "C:\Users\MNM\MAT\unrar.exe" e "%%~fA" "C:\Users\MNM\mat2\"

  • the for /f will parse the output of the dir and findstr
  • dir will iterate all *subs.rar in the tree starting from %Base%
  • the complex RegEx in findstr will filter the rar's to those in a folder subs in a subfolder of %Base%
  • as a backslash is an escape char in a RegEx, literal backslashes have to be doubled.

If the output looks ok remove the echo in the last line.

Upvotes: 0

Mofi
Mofi

Reputation: 49084

This simple task can be done with just a single command line:

@echo off
for /R "%USERPROFILE%\MAT" %%I in ("*subs.rar") do "%USERPROFILE%\MAT\UnRAR.exe" x -c- -idcdp -y "%%I" "%USERPROFILE%\mat2\"

USERPROFILE is a predefined Windows Environment Variable which is on your computer for your user account defined with value C:\Users\MNM.

The command FOR searches in directory C:\Users\MNM\MAT and all its non hidden subdirectories because of /R for non hidden files matching the pattern *subs.rar. Each file name found is assigned with full path to loop variable I.

UnRAR is executed for each found RAR archive file for extracting the archive to directory C:\Users\MNM\mat2 with extracting also the directory structures inside the RAR archive file because of command x instead of e. Existing files in destination directory (tree) are automatically overwritten because of -y. The switches -c- and -idcdp are for displaying less information during extraction process.

For a brief description of used and additionally available switches of UnRAR run in command prompt window UnRAR without any parameter or with /? as parameter. A complete description of the commands and switches of UnRAR can be found in text file Rar.txt in program files folder of WinRAR if that shareware application is also installed and not just the freeware UnRAR.

It is absolutely not needed to change into the directory containing the RAR archive file on extracting all RAR archives into same destination directory as it can be seen here.

Upvotes: 2

Related Questions