
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 파일이 실행되는 동일한 드라이브 및 폴더를 사용하도록 루프를 지시할 수 있으므로 이미.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
이다.
단일 배트를 사용하여 모든 폴더에 적용할 수 있습니다. 배트는 이미 정의되어 있으므로 현재 폴더를 매개변수로 전달하기만 하면 됩니다.*.C
처리하려면 상대 경로를 인수로 사용하여 하나의 폴더에만 알리고,.
:
박쥐/cmd스크립트는 실행 시 인수/매개변수도 허용합니다.
이 배트를 사용하려면 인수를 전달하세요..
:
>GCCc .
이 (.) 지점은 박쥐를 호출하는 현재 폴더의 경로를 참조하며 루프 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