变量未设置

时间:2015-10-30 18:19:38

标签: batch-file delayedvariableexpansion

这是Why the loop is not running as expected?

代码的扩展

最终我想从Report.txt(一行中包含1,2,3,4之类的逗号分隔文件)中读取值,并在summary.yml的某些字段中更新这些值(... field10: var1,field11:var2,... field25:var3 ..)等。为此我试图存储来自Report.txt的值,但是没有设置变量。任何帮助都将受到高度赞赏

@echo off
setlocal enableextensions disabledelayedexpansion

rem E:\Backups\ code   \  Hazard \ test1 \ it0 ... itn
rem             ^root     ^ %%X    ^ %%Y           ^ %%~dpc

for /D %%X in ("*") do for /D %%Y in ("%%~fX\*") do for /f "tokens=1,2,*" %%a in ('
    robocopy "%%~fY." "%%~fY." Report.txt /l /nocopy /is /s /nc /ns /ts /ndl /njh /njs 
    ^| sort /r 2^>nul
    ^| cmd /q /v /c "(set /p .=&echo(!.!)"
') do (

    copy "%%~fY\it0\summary.yml" "%%~dpc."

    setlocal enabledelayedexpansion
    set vidx=0
    for /F "tokens=1-4 delims=," %%A in (%%~dpc\Report.txt) do (
        set "var1=%%A"
        set "var2=%%B"
        set "var3=%%C"
        set "var4=%%D"
    )
    set var



    @echo  !var1!>  %%~dpc\temp.txt
    @echo  !var2!>> %%~dpc\temp.txt
    @echo  !var3!>> %%~dpc\temp.txt
    @echo  !var4!>> %%~dpc\temp.txt
)

1 个答案:

答案 0 :(得分:1)

for /f tokens=*for正在读取整行,而不会将其拆分。

由于逗号是分隔符,您可以使用嵌套的setlocal enabledelayedexpansion for /F "tokens=*" %%A in (%%~dpc\Report.txt) do ( for %%x in (%%A) do ( SET /A "vidx=vidx + 1" set "var!vidx!=%%x" ) ) set var 循环分割该行

for /f

或者,要使用for /F "tokens=1-4 delims=," %%A in (%%~dpc\Report.txt) do ( set "var1=%%A" set "var2=%%B" set "var3=%%C" set "var4=%%D" ) set var 直接拆分,您需要指明需要检索的行中的令牌

$