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:
- Go to each file location.
- Search the current folder for a 'makefile'.
- If found, run 'make' and break out, else go to parent folder and repeat step-2.
- 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?
0
, should be1
.