batch script question

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
 
P

Pegasus \(MVP\)

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.
 
C

chenzero

Anyway it's a bit strange comparison with other shell script :)
Thanks for your kindly help!

-chenzero
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top