尝试编写一个批处理脚本来查找当前目录中的所有.c 文件并进行编译

尝试编写一个批处理脚本来查找当前目录中的所有.c 文件并进行编译

我试图make用批处理文件在 Windows 中编写一个模拟程序

但我对批处理脚本还是个菜鸟,这是我的尝试 -

我正在尝试制作一个批处理脚本,查找当前目录中的所有 .c 文件并进行编译

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

我认为它应该可以工作,并且存在语法问题,但我真的不知道,也可能存在其他问题。此外,如果有更好的方法来实现这一点,请说明

答案1

@echo off

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

  • 观察:1.要使用上述代码,您需要在每个将要使用它的文件夹中复制此脚本/bat...

您可以指示循环使用执行 bat 文件的同一驱动器和文件夹,从而已经指出.c要使用的文件以及要创建的可执行文件:

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

  • 观察:2。使用For /?获取有关修饰符的更多信息:
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.
  • 观察:3。 %~f0与此相同%~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
来源链接ss64.com


@echo off

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

将此脚本保存为您容易记住的任何名称,例如CGGc.cmd, 普京C:\Windows\SYSTEM32文件夹,该文件夹出现在%PATH%系统,并且将被找到并执行,而不需要与文件位于同一文件夹中*.C是。

您可以使用单个 bat 应用于任何文件夹,只需将当前文件夹作为参数传递,因为该 bat 已经定义了*.C处理时,仅通知一个文件夹,使用相对路径作为参数,.

/脚本在执行时也接受参数/参数。


要使用这个球棒,只需传递参数.

>GCCc  .

其中,这个(。)点指的是你正在调用bat的当前文件夹的路径,这将是For循环使用的参数Drive\Path\Folder_Name.eXtension\ 的论点%~1,以及nx是为了防止出现错误:

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



相关内容