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

问题描述

我有一个文件REPORT.TXT有像(1,2,3,4)逗号分隔值。我检查,如果该文件不为空,然后与文件中值的分配4个变量。但变量未设置。任何帮助,为什么发生这种情况?

  SETLOCAL enabledelayedexpansion    FOR / F %% i的(REPORT.TXT)并设置大小= %%〜滋
    如果大小%GTR%0(    FOR / F令牌= 1-4 delims =%% A有(REPORT.TXT),做(
        设置VAR1 = %%一个
        设置VAR2 = %% B
        设置VAR3 = %% C
        设置VAR4 = %% D    )
    VAR集
    )
    回声%VAR1%


解决方案

您已经启用延迟扩展,但你不使用它。为了使用延迟扩展,你需要使用!变!而不是的%变量%。此外,在指定循环变量是大小写敏感的,所以你要么需要设置VAR1等于 或使用 %% A 作为循环变量。

  SETLOCAL enabledelayedexpansionFOR / F %% i的(REPORT.TXT)并设置大小= %%〜滋
如果大小%GTR%0(    FOR / F令牌= 1-4 delims =%%一中(REPORT.TXT)做(
        设置VAR1 = %%一个
        设置VAR2 = %% B
        设置VAR3 = %% C
        设置VAR4 = %% D
    )
    VAR集

回音!VAR1!

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 variables are not set. Any help why this is happening?

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%
解决方案

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!

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

08-23 08:37