1

I am writing a Windows batch script which compiles the files which are passed to it as arguments. Here is what I want to do:

  1. Go to each file location.
  2. Search the current folder for a 'makefile'.
  3. If found, run 'make' and break out, else go to parent folder and repeat step-2.
  4. Exit if root of current drive is reached.

Here is what I have been able to come up till now:

Input : List of full-paths to files which are to be compiled.

Example: "D:/dir1/dir2/file1.cxx" "D:/dir1/dir3/file2.cxx"

@echo off
REM -- loop over each argument --
for %%I IN (%*) DO (
   cd %%~dpI

   call :loop

   echo "After subroutine"
)
exit /b


:loop
REM -- NOTE: Infinite loop, breaks out when root directory is reached --
REM -- or makefile is found                                           --
for /L %%n in (1,0,10) do (
   if exist "makefile" (
      echo "Building.."
      make -s
      echo "Exiting inner loop"
      exit /b 2
   ) else (
      if "%cd:~3,1%"=="" ( 
        echo "Reached root...exiting inner loop..."
        exit /b 2
      )

      REM -- Go to parent directory --
      cd ..
      echo "Searching one level up"
   )
)

Everything works file except this - After encountering the first 'makefile', the 'exit /b 2' causes the batch file to exit. What I want is that only the inner loop should exit. 'exit /b 2' is supposed to work according to this, but due to some reason it is not. Can anyone help me out with this?

2
  • I can't reproduce it with a minimal batch file, however the loop in your code has an incorrect step argument 0, should be 1.
    – wOxxOm
    Nov 19, 2015 at 10:42
  • @wOxxOm : The loop step is 0 so that it runs infinitely.
    – johngreen
    Nov 19, 2015 at 10:47

1 Answer 1

1

There are a couple problems in your code. The less important one is that the comparison of the current directory in the inner loop must be done with delayed expansion. Now the important one:

There is no way to break a for /L loop with exit /B command. Although any command after the exit /B in the loop is not longer executed, the loop never ends. You must use plain exit command to do this, but of course the whole cmd.exe session is also terminated by exit, so the solution is to start a second cmd.exe session that re-execute the same Batch file controlled by a special parameter:

@echo off

REM If this batch file was re-executed from itself: goto right part
if "%~1" equ ":loop" goto loop

REM -- loop over each argument --
for %%I IN (%*) DO (
   cd %%~dpI

   REM Execute the "subroutine" in a separate cmd.exe session
   cmd /C "%~F0" :loop

   echo "After subroutine"
)
exit /b


:loop
setlocal EnableDelayedExpansion

REM -- NOTE: Infinite loop, breaks out when root directory is reached --
REM -- or makefile is found                                           --
for /L %%n in () do (
   if exist "makefile" (
      echo "Building.."
      make -s
      echo "Exiting inner loop"
      exit
   ) else (
      if "!cd:~3,1!" equ "" ( 
        echo "Reached root...exiting inner loop..."
        exit
      )

      REM -- Go to parent directory --
      cd ..
      echo "Searching one level up"
   )
)

EDIT: Couple comments added

  • When you use an infinite loop it is clearer to not include any value in the parentheses; otherwise it seems that you made a mistake in the "0" increment.

  • The description of EXIT command that appears at the link you gave is incorrect at this point...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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