Windows CMD command to run a command inside every subdirectory?

2

1

I have a batch script I wrote called "joiner.bat." It needs to run inside each of 730 sub-directories of a folder. The joiner.bat file has already been copied into each of the sub-folders, it just needs to run in each. I tried this, but it doesn't work:

for %f in (*) do joiner.bat %f

I tried that command outside of a batch file just on the command prompt but it did nothing. I need the command to go into a folder, run the command, go back to the previous folder, into the next, run the command, and so on.

jlacroix82

Posted 2012-04-04T14:39:54.600

Reputation: 111

Answers

3

This should do it:

for /d %%a in (*) do (
    cd %%a 
    call joiner.bat
)

create this as a batch file in the top directory.

I modified your bach file from the comments. You may need more parens and DelayedExpansion requires the use of ! instead of %. Try this

setlocal enabledelayedexpansion
for /d %%a in (*) do ( 
    cd %%a 
    copy /b *.xml newfile.xml 
    @echo off 
    SET "CDIR=%~dp0" 
    SET "CDIR=!CDIR:~0,-1!" 
    FOR %%i IN ("!CDIR!") DO (
        SET "PARENTFOLDERNAME=%%~nxi" 
        move newfile.xml "C:\users\lacroixja01\desktop\test\%PARENTFOLDERNAME%.xml" 
        )
)

uSlackr

Posted 2012-04-04T14:39:54.600

Reputation: 8 755

Unfortunately that didn't work. After that command failed, I decided to copy your code and joiner.bat into one and to see if that would help and it didn't. This is the entire bat file:

`for /d %%a in (*) do ( cd %%a copy /b *.xml newfile.xml

@echo off SET "CDIR=%~dp0" SET "CDIR=%CDIR:~0,-1%" FOR %%i IN ("%CDIR%") DO SET "PARENTFOLDERNAME=%%~nxi"

move newfile.xml "C:\users\lacroixja01\desktop\test%PARENTFOLDERNAME%.xml" )` – jlacroix82 – 2012-04-04T15:31:37.560

Windows batch doesn't deal well with modifying variable in a loop. Try adding this to the top of the batch file: setlocal ENABLEDELAYEDEXPANSION – uSlackr – 2012-04-04T15:56:41.353

I posted modified code to my answer. its not tested! – uSlackr – 2012-04-04T16:07:40.497

This did it:

`for /d %%a in (*) do ( pushd %%a

call joiner.bat

popd )` – jlacroix82 – 2012-04-04T16:39:53.077