How does “FOR” work in cmd batch file?

前端 未结 12 1058
面向向阳花
面向向阳花 2020-12-03 10:40

I\'ve been programming in dozens of languages for 20 years but I could never understand how \"FOR\" work in windows cmd shell batch file, no matter how hard I tried. I read

12条回答
  •  一整个雨季
    2020-12-03 10:54

    I know this is SUPER old... but just for fun I decided to give this a try:

    @Echo OFF
    setlocal
    set testpath=%path: =#%
    FOR /F "tokens=* delims=;" %%P in ("%testpath%") do call :loop %%P
    :loop
    if '%1'=='' goto endloop
    set testpath=%1
    set testpath=%testpath:#= %
    echo %testpath%
    SHIFT
    goto :loop
    :endloop
    pause
    endlocal
    exit
    

    This doesn't require a count and will go until it finishes. I had the same problem with spaces but it made it through the entire variable. The key to this is the loop labels and the SHIFT function.

提交回复
热议问题