变量未设置 [英] Variable is not set

查看:79
本文介绍了变量未设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是为什么循环未运行的代码的扩展符合预期?

最终,我想从Report.txt(用逗号分隔的文件,其中一行中有1,2,3,4的值)中读取值,并在summary.yml的某些字段中更新这些值(... field10:var1,field11:var2,... field25:var3 ..)等.为此,我尝试存储Report.txt中的值,但未设置变量.任何帮助将不胜感激

Ultimately I want to read values from Report.txt (a comma separated file having values like 1,2,3,4 in a single line) and update these values in some fields of summary.yml (...field10 : var1, field11 :var2,... field25 :var3..) etc. For that I am trying to store values from Report.txt but the variable are not being set. Any help would be highly appreciated

@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
)

推荐答案

您的/f 的,带有 tokens = * ,它正在读取整行而不进行拆分.

Your for /f, with a tokens=* is reading the full line without splitting it.

由于逗号是分隔符,因此您可以使用嵌套的 for 循环

As the comma is a delimiter, you can split the line with a nested for loop

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

或者,要直接使用/f 进行拆分,您需要在需要检索的行中指示标记

Or, to directly split using the for /f you need to indicate the tokens in the line that you need to retrieve

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

这篇关于变量未设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆