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.


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.