batch script question

G

Guest

Guest
Archived from groups: microsoft.public.win2000.general (More info?)

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
 
G

Guest

Guest
Archived from groups: microsoft.public.win2000.general (More info?)

"chenzero" <chenzero@netease.com> wrote in message
news:uWCOsBhRFHA.1396@TK2MSFTNGP10.phx.gbl...
> 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.
 
G

Guest

Guest
Archived from groups: microsoft.public.win2000.general (More info?)

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

-chenzero


"Pegasus (MVP)" <I.can@fly.com> дÈëÓʼþ
news:eMXmIphRFHA.1348@TK2MSFTNGP15.phx.gbl...
>
> > ...
>
> 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.
>
>