mardi 2 mai 2017

bat file enabledelayedexpansion won't expand in IF statement

When I try delayed expansion of an environment variable with a : clause inside a .BAT file IF statement the line fails to parse/execute. Using % signs instead of ! for the expansion works. The identical delayed expansion works in other places, eg an ECHO statement. For example:

@echo off& setlocal enabledelayedexpansion
set t=abcd
echo !t:~0,2!
if %t:~0,2% == ab echo equal
if !t:~0,2! == ab echo equal

The echo !t:~0,2! correctly produces: ab

The if %t:~0,2% == ab echo equal correctly produces: equal

The if !t:~0,2! == ab echo equal issues the error: 2! was unexpected at this time

I don't understand why I can't use a delayed expansion with a : clause inside an IF statement. Without the colon clause the delayed expansion inside the IF statement works fine.

Aucun commentaire:

Enregistrer un commentaire