setlocal enabledelayedexpansion and its potential effect?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

setlocal enabledelayedexpansion and its potential effect?

#1 Post by goodywp » 10 Jan 2018 10:06

Hi All,

I just got an issue which could be related to "setlocal enabledelayedexpansion"

Please correct me if I am wrong...
I have the following code:

Code: Select all

@ECHO OFF
if exist C:\auto_pkg_build\Tools\PACKAGER\extract_scheme.log (del C:\auto_pkg_build\Tools\PACKAGER\extract_scheme.log)

if exist C:\auto_pkg_build\Scripts\scheme_replace\pkg_data\temp\fdata1.txt (set [0] = ldata_1.txt)
if exist C:\auto_pkg_build\Scripts\scheme_replace\pkg_data\temp\fdata2.txt (set [1] = ldata_2.txt)
if exist C:\auto_pkg_build\Scripts\scheme_replace\pkg_data\temp\fdata3.txt (set [2] = ldata_3.txt)
if exist C:\auto_pkg_build\Scripts\scheme_replace\pkg_data\temp\fdata4.txt (set [3] = ldata_4.txt)
if exist C:\auto_pkg_build\Scripts\scheme_replace\pkg_data\temp\fdata5.txt (set [4] = ldata_5.txt)
if exist C:\auto_pkg_build\Scripts\scheme_replace\pkg_data\temp\fdata6.txt (set [5] = ldata_6.txt)
if exist C:\auto_pkg_build\Scripts\scheme_replace\pkg_data\temp\fdata7.txt (set [6] = ldata_7.txt)
if exist C:\auto_pkg_build\Scripts\scheme_replace\pkg_data\temp\fdata8.txt (set [7] = ldata_8.txt)
if exist C:\auto_pkg_build\Scripts\scheme_replace\pkg_data\temp\fdata9.txt (set [8] = ldata_9.txt)
if exist C:\auto_pkg_build\Scripts\scheme_replace\pkg_data\temp\fdata10.txt (set [9] = ldata_10.txt)

for /F "tokens=2 delims==" %%n in ('set [') do (
	cd C:\auto_pkg_build\Scripts\scheme_replace
	del pkg_data.txt
	cd C:\auto_pkg_build\Scripts\scheme_replace\pkg_data
	copy %%n C:\auto_pkg_build\Scripts\scheme_replace
	timeout /t 1 

	cd C:\auto_pkg_build\Scripts\scheme_replace
	ren %%n pkg_data.txt	
	timeout /t 1 

	call C:\auto_pkg_build\Scripts\scheme_replace\get_variable.cmd
	timeout /t 1 

	call C:\auto_pkg_build\Scripts\scheme_replace\pre_sch_extract.cmd
	timeout /t 1
	
	call C:\auto_pkg_build\Tools\PACKAGER\list_comp_sch.cmd
	timeout /t 1

	call C:\auto_pkg_build\Scripts\scheme_maintenance\gen_sch_list.cmd
	timeout /t 1

	call C:\auto_pkg_build\Scripts\scheme_maintenance\sl_sch_list.cmd
	timeout /t 1

	call C:\auto_pkg_build\Scripts\scheme_maintenance\fk_sch_list.cmd
	timeout /t 1

	call C:\auto_pkg_build\Scripts\scheme_maintenance\merge_sch_list.cmd
	timeout /t 1

	call C:\auto_pkg_build\Tools\PACKAGER\md_S3S_extract.cmd
	timeout /t 1

	call C:\auto_pkg_build\Scripts\scheme_replace\move_schemes.cmd
	timeout /t 1

	C:\auto_pkg_build\Scripts\scheme_maintenance\remove_embedded_char.cmd
	timeout /t 1

	C:\auto_pkg_build\Scripts\scheme_maintenance\remove_embedded_duplicate.cmd
	timeout /t 1

	cd C:\auto_pkg_build\scripts
	
)

timeout /t 1
cd C:\auto_pkg_build\scripts
The above script run smoothly before I added two more commands at the end of that loop
C:\auto_pkg_build\Scripts\scheme_maintenance\remove_embedded_char.cmd
timeout /t 1

C:\auto_pkg_build\Scripts\scheme_maintenance\remove_embedded_duplicate.cmd
timeout /t 1

Both these two scripts having "setlocal enabledelayedexpansion", not sure if it is the cause or not
Now when I run the above script. the first loop is fine but the second loop afterward, it also echo on the script as this:

C:\auto_pkg_build\Scripts>(
cd C:\auto_pkg_build\Scripts\scheme_replace
del pkg_data.txt
cd C:\auto_pkg_build\Scripts\scheme_replace\pkg_data
copy %%n C:\auto_pkg_build\Scripts\scheme_replace
timeout /t 1

cd C:\auto_pkg_build\Scripts\scheme_replace
ren %%n pkg_data.txt
timeout /t 1

call C:\auto_pkg_build\Scripts\scheme_replace\get_variable.cmd
timeout /t 1

call C:\auto_pkg_build\Scripts\scheme_replace\pre_sch_extract.cmd
timeout /t 1

call C:\auto_pkg_build\Tools\PACKAGER\list_comp_sch.cmd
timeout /t 1

call C:\auto_pkg_build\Scripts\scheme_maintenance\gen_sch_list.cmd
timeout /t 1

call C:\auto_pkg_build\Scripts\scheme_maintenance\sl_sch_list.cmd
timeout /t 1

call C:\auto_pkg_build\Scripts\scheme_maintenance\fk_sch_list.cmd
timeout /t 1

call C:\auto_pkg_build\Scripts\scheme_maintenance\merge_sch_list.cmd
timeout /t 1

call C:\auto_pkg_build\Tools\PACKAGER\md_S3S_extract.cmd
timeout /t 1

call C:\auto_pkg_build\Scripts\scheme_replace\move_schemes.cmd
timeout /t 1

C:\auto_pkg_build\Scripts\scheme_maintenance\remove_embedded_char.cmd
timeout /t 1

C:\auto_pkg_build\Scripts\scheme_maintenance\remove_embedded_duplicate.cmd
timeout /t 1

cd C:\auto_pkg_build\scripts

)

I wonder if it is related to the "setlocal enabledelayedexpansion" in the last scripts. Although it is no harm to run the scripts but why it echo on all these scripts in the screen...???, which did not happen before I added these last two scripts...

pieh-ejdsch
Posts: 240
Joined: 04 Mar 2014 11:14
Location: germany

Re: setlocal enabledelayedexpansion and its potential effect?

#2 Post by pieh-ejdsch » 11 Jan 2018 16:22

is an echo switched on in one of your scripts?
Just check it out with a script.
-----
checkcmd.cmd Yourbatch.cmd echo

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: setlocal enabledelayedexpansion and its potential effect?

#3 Post by goodywp » 11 Jan 2018 19:02

pieh-ejdsch wrote:
11 Jan 2018 16:22
is an echo switched on in one of your scripts?
Just check it out with a script.
-----
checkcmd.cmd Yourbatch.cmd echo
No! nothing changed... only added two scripts at the end of that loop....

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: setlocal enabledelayedexpansion and its potential effect?

#4 Post by jeb » 12 Jan 2018 00:04

You have to CALL your script.
Without CALL the batch context ends and the command line context is enabled.

Code: Select all

Call C:\auto_pkg_build\...

pieh-ejdsch
Posts: 240
Joined: 04 Mar 2014 11:14
Location: germany

Re: setlocal enabledelayedexpansion and its potential effect?

#5 Post by pieh-ejdsch » 12 Jan 2018 04:02

I did not see that yesterday:

Code: Select all

call Batch
That the batch kontex is started and Echo is switched on again within a bracket / command line I did not know either.
That would mean, however, that the CMD line parser does not notice the echo condition and the individual re-reads command line / bracket and executes - as if he would continue at a mark in a batch. Maybe I'm just a little bit wrong.
But the checkCMD.cmd also gives you all relevant lines, which should contain "certain commands".
I've used these snippet many times to find a few such little bugs - though it does not show any bugs - what irony ...

Code: Select all

 CheckCMD.cmd mybatch.cmd

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: setlocal enabledelayedexpansion and its potential effect?

#6 Post by jeb » 12 Jan 2018 06:59

Starting a batch from inside a batch file doesn't add a return conetxt onto the call stack.
Therefore, when the second batch is exited, normally the batch processing ends.

But in this case a command block of a FOR command is used and command blocks are cached in memory and these blocks are never readed again from the file.
But in this case the FOR/F reparses the cached block (necessarry for the FOR-parameter expansion) and in this reparsing the complete block will be echoed.


This can be demonstrated with a small test

Code: Select all

@echo off
echo( > emptyBatch.bat
FOR /L %%n in (1 1 2) DO (
  call echo RUN %%n: The variable is '%%=undefined=%%'
  REM Start a batch file without CALL
  emptyBatch.bat
  call echo RUN %%n: The variable is '%%=undefined=%%'
)
echo This line is never reached
Output wrote:RUN 1: The variable is ''
RUN 1: The variable is '%=undefined=%'

c:\temp\labels>(
call echo RUN 2: The variable is '%=undefined=%'
REM Start a batch file without CALL
emptyBatch.bat
call echo RUN 2: The variable is '%=undefined=%'
)
RUN 2: The variable is '%=undefined=%'
RUN 2: The variable is '%=undefined=%'

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: setlocal enabledelayedexpansion and its potential effect?

#7 Post by goodywp » 15 Jan 2018 09:06

jeb wrote:
12 Jan 2018 00:04
You have to CALL your script.
Without CALL the batch context ends and the command line context is enabled.

Code: Select all

Call C:\auto_pkg_build\...
Thanks jeb!!
You are right !! My bad.... after adding call, the problem is solved....

:oops:

Post Reply