How do I increment a DOS variable in a FOR /F loop?

后端 未结 5 1638
后悔当初
后悔当初 2020-12-13 00:02

I\'m trying to read text lines from a file, and increment a counter so I can eventually simulate an array in DOS.

I\'d like to be able to store the lines of text in

5条回答
  •  庸人自扰
    2020-12-13 00:39

    I would like to add that in case in you create local variables within the loop, they need to be expanded using the bang(!) notation as well. Extending the example at https://stackoverflow.com/a/2919699 above, if we want to create counter-based output filenames

    set TEXT_T="myfile.txt"
    
    set /a c=1
    
    setlocal ENABLEDELAYEDEXPANSION
    
    FOR /F "tokens=1 usebackq" %%i in (%TEXT_T%) do (
        set /a c=c+1
        set OUTPUT_FILE_NAME=output_!c!.txt
        echo Output file is !OUTPUT_FILE_NAME!
        echo %%i, !c!
    )
    
    endlocal
    

提交回复
热议问题