BATCH:在IF语句中将变量设置为LOOP并启用setlocal enableDelayedExpansion:IMPOSSIBLE

时间:2021-06-09 23:21:19

I have a pb with this batch script, i already spend 1 hour to try to get it fix ! but no chance

我有一个pb与这个批处理脚本,我已经花了1个小时试图解决它!但没有机会

I try to set the variable TYP with string regarding condition (CON is a string), the pb concern the TYP variable. I can't set this variable and of course i can't echo the value.

我尝试使用关于条件的字符串设置变量TYP(CON是字符串),pb关注TYP变量。我不能设置这个变量,当然我不能回应这个值。

I need help

我需要帮助

@ECHO OFF
 cls

setlocal enableDelayedExpansion

SET ROOT="C:\Users\7642FH\Local\RawFile"
SET ROOT=%ROOT:~1%
SET ROOT=%ROOT:~0,-1%
SET TYPE1=Actual
SET TYPE1=Forcast
set TYP =

FOR /F  %%d in ('DIR %ROOT% /AD /B') DO (

  FOR /F  %%f in ('DIR "%ROOT%\%%d\*.*" /B') DO (
      echo %%d
      ECHO %%f
      SET x=%%f
      set y=!x:~0,3%!


   IF CON ==!y! (
        set TYP = aaa
        echo aaa
        echo !TYP!
        ) else (
        echo rrr
        set TYP = rrrr
        echo !TYP!
        )

  )
)

endlocal
echo on

2 个解决方案

#1


1  

set y=!x:~0,3%!

has an extra % sign in it.

有一个额外的%标志。

#2


3  

Your problem is here:

你的问题在这里:

set TYP = aaa

and here:

set TYP = rrrr

You define a variable named "TYP ", not "TYP"

您定义名为“TYP”的变量,而不是“TYP”

write

set TYP=aaa

instead. (or reference it with !TYP ! if the space is an intended part of the variable name)

代替。 (或者引用它!TYP!如果空格是变量名的预期部分)

or even better:

甚至更好:

set "TYP=aaa"

which prevents unintended spaces at the end of the value.

这可以防止值末尾的意外空格。

#1


1  

set y=!x:~0,3%!

has an extra % sign in it.

有一个额外的%标志。

#2


3  

Your problem is here:

你的问题在这里:

set TYP = aaa

and here:

set TYP = rrrr

You define a variable named "TYP ", not "TYP"

您定义名为“TYP”的变量,而不是“TYP”

write

set TYP=aaa

instead. (or reference it with !TYP ! if the space is an intended part of the variable name)

代替。 (或者引用它!TYP!如果空格是变量名的预期部分)

or even better:

甚至更好:

set "TYP=aaa"

which prevents unintended spaces at the end of the value.

这可以防止值末尾的意外空格。