Re: why can't i get variable value?




"thinktwice" <memorialday@xxxxxxxxx> wrote in message
news:5a4ad5fb-f4d4-4ef3-b14c-73630b58c44d@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
i have a file called "IsPathRelative.bat".
it's usage like : IsPathRelative.bat yourpath, it'll set
IsPathRelative=True if the path i passed in is a relativepath(not very
accurate,but works for me) else set IsPathRelative=False

i call this bath file in another batch file.

... get path1, path2
call IsPathRelative.bat path1
if %IsPathRelative% =="True" dosomething

if %path2%=="" (
call IsPathRelative.bat path2
if %IsPathRelative% =="" do other ) <--------------------for example
if path2 is relative path, it should be True, but actually it is
False. i have traced into IsPathRelative batch file , everything's
fine . before it leaves IsPathRelative file scope, %IsPathRelative% is
still True. why this happens?

It's a battle between this question and date/time manipulation for #1 FAQ
status.

See Timo's FAQ in alt.msdos.batch.nt.

Batch parses the ENTIRE "IF" statement and substitutes the values of
%variable% AT THAT TIME.
THEN the "IF" statement is EXECUTED.

You are changing the value of ispathrealtive AFTER it has been parsed, and
batch executes the statement using the value AT PARSE TIME.

There are two common cures:

1. Before the IF statement (normally at the start of the batch) execute a

SETLOCAL ENABLEDELAYEDEXPANSION

statement. You may also need to add the ENABLEEXTENSIONS keyword to this
statement.

THEN use !variable! in place of %variable% to access the RUN-TIME (dynamic)
value of the variable - %variable% accesses the PARSE-TIME value.

(see
SETLOCAL /?
from the prompt, or alt.msdos.batch.nt for documentation/explanation)

This has the drawback that any environment changes made after the SETLOCAL
statement will be UNDONE when the batch terminates. (This can be bypassed -
see alt.msdos.batch.nt & alt.msdos.batch.nt FAQ for more info)

2. Use an internal routine call

....
IF .... CALL :INTERNAL
....
GOTO :EOF
:INTERNAL
:: variables values here are OUTSIDE of the context of the IF
....
GOTO :EOF

Note that the colons are important.

(see
CALL /?
from the prompt, or alt.msdos.batch.nt & alt.msdos.batch.nt FAQ for
documentation/explanation)


BTW:
are you aware that an

@ECHO OFF

statement will turn command-echoing OFF for the ENTIRE batch file - which
means you don't have to put "@" before each statement?

You can turn echoing on again with

@ECHO ON

for debugging purposes - commands will be echoed until another @ECHO OFF
statement is encountered.


.



Relevant Pages

  • Re: del not working in .bat file
    ... Haven't managed yet to do away with the .bat file and run this: ... Try using the CALL command inside the batch file. ... @echo off ... executes, which calls test2.bat and that executes. ...
    (microsoft.public.vb.general.discussion)
  • Re: del not working in .bat file
    ... Try using the CALL command inside the batch file. ... @echo off ... executes, which calls test2.bat and that executes. ...
    (microsoft.public.vb.general.discussion)
  • Re: Batching Updates?
    ... If you batch run them, they will automatically wait for one to finish before ... Echo Applying IE 5.5 Post-SP2 Hotfixes to system. ... Echo Reboot to activate changed files. ... >>the network login script or a batch file run on the ...
    (microsoft.public.security)
  • Re: Multi-zip files -- Windows server
    ... native DOS versions). ... TO BATCH OR NOT TO BATCH ... and using a batch file to try and solve the problem... ... The IBM PC operating system command line is a most ...
    (comp.sys.cbm)
  • Re: EWF RAM
    ... Do you mean it is running the stress test fine locally on the device? ... If it does and the issue has no repro, you should move to use local batch file approach in your support scenario. ... >> A tech plugs into it with laptop and remote desktops to it. ...
    (microsoft.public.windowsxp.embedded)