Programing

Windows 배치 파일을 어떻게 반복합니까?

lottogame 2020. 6. 14. 10:23
반응형

Windows 배치 파일을 어떻게 반복합니까?


Windows 배치 파일에서 FOR 루프의 구문은 무엇입니까?


FOR %%A IN (list) DO command parameters

list 는 공백, 쉼표 또는 세미콜론으로 구분 된 모든 요소의 목록입니다.

command 는 내부 또는 외부 명령, 배치 파일 또는 OS / 2 및 NT의 경우 명령 목록 일 수 있습니다.

parameters 는 명령에 대한 명령 줄 매개 변수를 포함합니다. 이 예제에서는 지정된 경우 매개 변수를 사용하여 목록의 모든 요소에 대해 명령이 한 번 실행됩니다.

특수한 유형의 매개 변수 (또는 명령)는 %% A이며, 목록에서 각 요소로 연속적으로 대체됩니다.

에서 루프에 대한


x 번 무언가를하고 싶다면 다음과 같이하십시오 :

예 (x = 200) :

FOR /L %%A IN (1,1,200) DO (
  ECHO %%A
)

1,1,200 방법:

  • 시작 = 1
  • 단계 당 증분 = 1
  • 끝 = 200

유형:

for /?

여러 페이지의 도움말 텍스트가 표시됩니다.


조건부로 명령을 여러 번 수행하십시오.

  • 구문 -FOR- 파일

    FOR %%parameter IN (set) DO command 
    
  • 구문 -FOR- 파일 경로에서 루트

    FOR /R [[drive:]path] %%parameter IN (set) DO command 
    
  • 구문-폴더

    FOR /D %%parameter IN (folder_set) DO command 
    
  • 구문 -FOR- 숫자 목록

    FOR /L %%parameter IN (start,step,end) DO command 
    
  • 구문 -FOR- 파일 내용

    FOR /F ["options"] %%parameter IN (filenameset) DO command 
    

    또는

    FOR /F ["options"] %%parameter IN ("Text string to process") DO command
    
  • 구문 -FOR- 명령 결과

    FOR /F ["options"] %%parameter IN ('command to process') DO command
    

그것

  • 일련의 데이터를 가져옵니다
  • FOR 매개 변수 %%G를 해당 데이터의 일부와 동일하게 만드십시오.
  • 명령을 수행하십시오 (선택적으로 명령의 일부로 매개 변수 사용).
  • -> 각 데이터 항목에 대해 반복

배치 프로그램이 아닌 명령 행에서 FOR 명령을 사용하는 경우 %G대신 1 퍼센트 부호 만 사용하십시오 %%G.

FOR 매개 변수

  • 첫 번째 매개 변수는 단일 문자 (예 : 문자 G)를 사용하여 정의해야합니다.

  • FOR %%G IN ...

    FOR 루프의 각 반복에서 IN ( ....)절이 평가되고 %%G다른 값으로 설정됩니다.

    이 절의 결과 단일 값이면 %% G가 해당 값과 동일하게 설정되고 명령이 수행됩니다.

    절의 결과로 여러 값이 생성되면 각 매개 변수를 보유하도록 추가 매개 변수가 내재적으로 정의됩니다. 이들은 알파벳 순서로 자동 할당됩니다 %%H %%I %%J... (암시 적 매개 변수 정의)

    매개 변수가 파일을 참조하는 경우 고급 변수 참조를 사용하여 파일 이름 / 경로 / 날짜 / 크기를 추출 할 수 있습니다.

    물론 알파벳 이외의 다른 문자를 선택할 수 있습니다 %%G. 그러나 경로 이름 형식 문자 (a, d, f, n, p, s, t, x)와 충돌하지 않으며 암시 적 매개 변수로 사용하기 위해 가장 긴 비 충돌 문자를 제공하기 때문에 좋은 선택입니다 .


FOR 적절한 사용법에 대한 예를 포함하여 FOR 루프에 대해 알아야 할 모든 정보를 제공합니다.


이 코드를 사용해보십시오 :

@echo off
color 02
set num1=0
set num2=1 
set terminator=5
:loop
set /a num1= %num1% + %num2%
if %num1%==%terminator% goto close
goto open
:close
echo %num1%
pause 
exit
:open
echo %num1%
goto loop

num1 is the number to be incremented and num2 is the value added to num1 and terminator is the value where the num1 will end. You can indicate different value for terminator in this statement (if %num1%==%terminator% goto close). This is the boolean expression goto close is the process if the boolean is true and goto open is the process if the boolean is false.


@echo off
echo.
set /p num1=Enter Prelim:
echo.
set /p num2=Enter Midterm:
echo.
set /p num3=Enter Semi:
echo.
set /p num4=Enter Finals:
echo.
set /a ans=%num1%+%num2%+%num3%+%num4%
set /a avg=%ans%/4
ECHO %avg%
if %avg%>=`95` goto true
:true
echo The two numbers you entered were the same.
echo.
pause
exit

From FOR /? help doc:

FOR %variable IN (set) DO command [command-parameters]

%variable Specifies a single letter replaceable parameter.
(set) Specifies a set of one or more files. Wildcards may be used. command Specifies the command to carry out for each file.
command-parameters
Specifies parameters or switches for the specified command.

To use the FOR command in a batch program, specify %%variable instead
of %variable. Variable names are case sensitive, so %i is different
from %I.

If Command Extensions are enabled, the following additional
forms of the FOR command are supported:

FOR /D %variable IN (set) DO command [command-parameters]

If set contains wildcards, then specifies to match against directory  
names instead of file names.                                          

FOR /R [[drive:]path] %variable IN (set) DO command [command-parameters]

Walks the directory tree rooted at [drive:]path, executing the FOR    
statement in each directory of the tree.  If no directory             
specification is specified after /R then the current directory is     
assumed.  If set is just a single period (.) character then it        
will just enumerate the directory tree.                               

FOR /L %variable IN (start,step,end) DO command [command-parameters]

The set is a sequence of numbers from start to end, by step amount.   
So (1,1,5) would generate the sequence 1 2 3 4 5 and (5,-1,1) would   
generate the sequence (5 4 3 2 1)                                     

참고URL : https://stackoverflow.com/questions/1355791/how-do-you-loop-in-a-windows-batch-file

반응형