batch script question

  • Thread starter Thread starter chenzero
  • Start date Start date
C

chenzero

Hi,
I recently encountered a question on batch command, please see following
batch code.

the script result which I expect is that the 1st and 2nd echo output would
be same. however, it doesnot. very strange...

Thanks in advance for your any concern.

@rem a script to guess the right app path
@echo off
set editor=''
for %%k in ("c:\winnt\notepad.exe" "c:\win2k\notepad.exe" ) do (
if exist %%k (
set editor=%%k
echo 1. %editor%
goto editorOk
)
)
:editorOk
echo 2. %editor%


-chenzero
 
chenzero said:
Hi,
I recently encountered a question on batch command, please see following
batch code.

the script result which I expect is that the 1st and 2nd echo output would
be same. however, it doesnot. very strange...

Thanks in advance for your any concern.

@rem a script to guess the right app path
@echo off
set editor=''
for %%k in ("c:\winnt\notepad.exe" "c:\win2k\notepad.exe" ) do (
if exist %%k (
set editor=%%k
echo 1. %editor%
goto editorOk
)
)
:editorOk
echo 2. %editor%


-chenzero

This is not strange at all. Each line of your batch file is scanned once
before it is executed, to resolve any variables it might contain. A line
is defined as a single line or as a set of lines enclosed by parenthesis.
This is why the variable in your first example does not get resolved.

There are two ways out of your predicament:
- Use subroutines, or
- Use DelayedExpansion

The command "set /?" provides some instructions on delayed variable
expansion. Post again if you need more details.
 
Anyway it's a bit strange comparison with other shell script :)
Thanks for your kindly help!

-chenzero
 
Back
Top