2

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.

2 Answers 2

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" 
        )
)
4
  • 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
    Apr 4, 2012 at 15:31
  • 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
    Apr 4, 2012 at 15:56
  • I posted modified code to my answer. its not tested!
    – uSlackr
    Apr 4, 2012 at 16:07
  • This did it: for /d %%a in (*) do ( pushd %%a call joiner.bat popd )
    – jlacroix82
    Apr 4, 2012 at 16:39
0

I had to modify it to look like this:

for /d %%a in (*) do (
    cd %%a 
    call joiner.bat
    cd ..
)
1
  • Please edit your answer to explain why this works. Mar 9, 2023 at 0:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .