Search code examples
batch-filefilereferenceall-files

Referencing all files in a folder and its subfolders of a certain type


In a .bat file I'm executing a compiler with the line

cl -GL -Od -Zi <all .h files> <all .c files>

but I don't know how to reference all .c and .h files in that manner. I was thinking */.c and */.h, but both of those are invalid arguments. Is there any way I could reference all .c and .h files in this manner?


Solution

  • cl doesn't let you do this, but if you want to compile all C files in every folder starting from a certain point, then you can do something like the following.

    Note, with those command options you can't pass in a header file either.

    
    @echo off
    set USAGE=Usage: %~n0 "C:\Existent Start Dir"
    if %1xx == xx ( echo %USAGE% & exit /b 2 )
    if not exist "%~1" ( echo %USAGE% & exit /b 1 )
    for /r "%~dpn1" %%f in (.) do (
      pushd %%f
      if exist *.c (
        echo *** Compiling C files in %%f ***
        call cl -GL -Od -Zi *.c
        echo.
      )
      popd
    )
    

    Test this with: test.cmd C:\mytest

    If C:\mytest contains several folders of simple *.c and *.h files this will work. test.cmd C:\mytest

    You might really be wanting to invoke nmake?