
我試圖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