Page 1 of 1

ENABLEDELAYEDEXPANSION in called bat files

Posted: 18 Jun 2024 23:33
by miskox
Looks like I will be using Antonio's AutoMenu.bat (viewtopic.php?f=3&t=10377#p66198).

I noticed that ENABLEDELAYEDEXPANSION is set so my .bat files have delayed expansion enabled. This causes some problems with exclamation marks (!).

What would be the best way to avoid these problems?

Setlocal help says:

Code: Select all

These modifications last until the matching ENDLOCAL command regardless of their setting prior to the SETLOCAL command.
I think I have two options:
- I could use ENDLOCAL before I call my .bat and then use SETLOCAL ENABLEDELAYEDEXPANSION when I get back to automenu.bat.
- I could use SETLOCAL DISABLEDELAYEDEXPANSION before I call my .bat and use SETLOCAL ENABLEDELAYEDEXPANSION when I return from my .bat.

1.cmd:

Code: Select all

@echo off
setlocal ENABLEDELAYEDEXPANSION
set test_par=Y!Y
echo 1_cmd:%test_par%
echo 1_cmd:!test_par!
echo.
echo calling 2.cmd
call 2.cmd
echo return from 2.cmd
echo.
echo 1_cmd:%test_par%
echo 1_cmd:!test_par!
2.cmd:

Code: Select all

@echo off
set test_par2=X!X
echo 2_cmd:%test_par2%
echo 2_cmd:!test_par2!
echo.
Output:

Code: Select all

1_cmd:YY
1_cmd:YY

calling 2.cmd
2_cmd:XX
2_cmd:XX

return from 2.cmd

1_cmd:YY
1_cmd:YY
Advise please.

Thanks.
Saso

Re: ENABLEDELAYEDEXPANSION in called bat files

Posted: 19 Jun 2024 09:33
by miskox
I tried to add SETLOCAL DISABLEDELAYEDEXPANSION before this line

Code: Select all

   !action[%descrip:~-4%]! "%choice%-%option%-%descrip%"
followed by SETLOCAL ENABLEDELAYEDEXPANSION

but I can't the line above to work. I tried many different options. No luck.

If I add these two SETLOCAL commands in the called .bat (which calls the real .bat) everything works.

Advise?

Saso

Re: ENABLEDELAYEDEXPANSION in called bat files

Posted: 04 Jul 2024 13:15
by pieh-ejdsch
Hello Miskos;

You are trying to include exclamation marks in a variable while Delayed Expansion is enabled.
You are trying to display a variable containing exclamation marks in percent signs during Delayed Expansion.
You are aware of the fact that the inclusion of variables with exclamation marks either requires a deactivated delay or is preceded by escape characters.

If you still need the variables of the locality, then endlocal is only a good choice if the variables still exist afterwards.

Define the correct environment, set the variables, use the correct environment to process the variables.
Enter variables during endlocal with