Why variables are not set

前端 未结 2 1327
野的像风
野的像风 2020-12-22 10:00

I have a file report.txt having comma separated values like (1,2,3,4). I am checking if the file is not blank then assign the 4 variables with values in the file. But the va

相关标签:
2条回答
  • 2020-12-22 10:15

    You've enabled delayed expansion, but you aren't using it. In order to use delayed expansion, you need to use !variable! instead of %variable%. Additionally, the variable specified in for loops is case-sensitive, so you either need to set var1 equal to %%A or use %%a as the loop variable.

    setlocal enabledelayedexpansion
    
    for /f %%i in ("Report.txt") do set size=%%~zi 
    if %size% gtr 0 (
    
        for /F "tokens=1-4 delims=," %%a in (Report.txt) do (
            set "var1=%%a"
            set "var2=%%b"
            set "var3=%%c"
            set "var4=%%d"
        )
        set var
    )
    echo !var1!
    
    0 讨论(0)
  • 2020-12-22 10:21
    setlocal enabledelayedexpansion
    
    for /f %%i in ("Report.txt") do set size=%%~zi 
    if %size% gtr 0 (
    

    I changed the code as below and it started working. Thank you guys for your time. Appeciate it.

    for /F "tokens=1-4 delims=," %%A in (%cd%\Report.txt) do (
        set "var1=%%A"
        set "var2=%%B"
        set "var3=%%C"
        set "var4=%%D"    
    
    )
    set var
    )
    echo !var1!
    
    0 讨论(0)
提交回复
热议问题