1

I was trying to write a make analog in windows, using batch files

But I am a noob in batch scripting, here is my try -

I am trying to make a batch script which fill find all the .c files in the current directory and compile them

@echo off
for %%a in (*) do (
    if "%%a" == "*.c" (
        gcc "%%a" -o "%%a.exe"
    )
)

I think it should work and there is a syntax problem, but I really have no idea, there could be other problems too. Also if there is a better way to achieve this please specify

2
  • %%a is a filename. If you compare it to a literal *.c, it will never be true (* is not a valid character for a filename). To compare the extension, use if /i "%%~xa" == ".c" (see for /? for more modifiers). But you can also process only .c files: for %%a in (*.c) do gcc "%%a" -o "%%~na.exe"
    – Stephan
    Commented Jan 9, 2021 at 19:17
  • Thanks dude! It worked Commented Jan 9, 2021 at 20:49

1 Answer 1

2
@echo off

for %%i in ("%~dp0*.C")do gcc "%%~i" -o "%%~dpni.exe"

  • Obs.: 1. To use the code above, you need to have a copy from this script/bat in each one folder where you will use it...

You can direct your loop to make use of the same drive and folder where your bat file is being executed, thus already pointing out the .c files to be used, and the executables to be created:

  • Using and understanding %~DP0
Same Drive: \Path where your file.bat       is:
    |-----|  |------|       |--------|
      C:     \Folder\        file.bat
    | %~D |  | %~P  |       |   %~0  | ==>  %~DP0
  • Using and understanding %%~NXi
Same Name      eXtension  of   file.C   listed in loop
    |-------|  |--------|     |---------|
     Program       .C          Program.C
    | %%~N  |  |  %%~X  |     |  %%~NX  | ==>  %~NXi
  • Using and understanding %%~DPNXi
Same Drive:   \Path     \Name       eXtension  of  file.C   listed in loop
    |------|  |------|  |--------| |--------|
       C:     \Folder\   Program       .C 
    | %%~D |  | %%~P  | |  %%~N  | |  %%~X  | ==>  %%~DPNXi

  • Obs.: 2. Use For /? to get more information about the modifiers:
In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:

    %~I         - expands %I removing any surrounding quotes (")
    %~fI        - expands %I to a fully qualified path name
    %~dI        - expands %I to a drive letter only
    %~pI        - expands %I to a path only
    %~nI        - expands %I to a file name only
    %~xI        - expands %I to a file extension only
    %~sI        - expanded path contains short names only
    %~aI        - expands %I to file attributes of file
    %~tI        - expands %I to date/time of file
    %~zI        - expands %I to size of file
    %~$PATH:I   - searches the directories listed in the PATH
                   environment variable and expands %I to the
                   fully qualified name of the first one found.
                   If the environment variable name is not
                   defined or the file is not found by the
                   search, then this modifier expands to the
                   empty string

The modifiers can be combined to get compound results:

    %~dpI       - expands %I to a drive letter and path only
    %~nxI       - expands %I to a file name and extension only
    %~fsI       - expands %I to a full path name with short names only
    %~dp$PATH:I - searches the directories listed in the PATH
                   environment variable for %I and expands to the
                   drive letter and path of the first one found.
    %~ftzaI     - expands %I to a DIR like output line

In the above examples %I and PATH can be replaced by other valid
values.  The %~ syntax is terminated by a valid FOR variable name.
Picking upper case variable names like %I makes it more readable and
avoids confusion with the modifiers, which are not case sensitive.
  • Obs.: 3. %~f0 Is the same as this %~DPNX0

You can get the pathname of the batch script itself with %0, parameter xtensions
can be applied to this so %~dp0 will return the Drive and Path to the batch scrip
e.g. W:\scripts\ and %~f0 will return the full pathname W:\scripts\mybatch.cmd
Source linked to ss64.com


@echo off

for %%i in ("%~dpnx1\*.C")do gcc "%%~i" -o "%%~dpni.exe"

Saving this script as any name that is easy to your remember, like CGGc.cmd, put in C:\Windows\SYSTEM32 folder, where this folder appears in the %PATH% of the system, and will be found and executed without the need to be in the same folder where your files *.C are.

You can use a single bat to be applied to any folder, just pass the current folder as a parameter, since the bat has already defined *.C to handle, informs only one folder, using a relative path as an argument, .:

/ Scripts also accept arguments/parameters on execution.


To use this bat, just pass the argument .:

>GCCc  .

Where this (.) point is referring to the path to the current folder where you are invoking the bat, this will be an argument that For loop use as the Drive\Path\Folder_Name.eXtension\ of argument %~1, and the nx are present to prevent a bug:

Full Stop Bug
Although Win32 will not recognise any file or directory name that begins or ends with a '.' (period / full stop) it is possible to include a Full Stop in the middle of a directory name and this can cause issues with FOR /D.   Parameter expansion will treat a Full Stop as a file extension, so for a directory name like "Sample 2.6.4" the output of %%~nG will be truncated to "Sample 2.6" to return the whole folder name use %%G or %%~nxG



You must log in to answer this question.

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