Re: why can't i get variable value?
- From: "billious" <billious_1954@xxxxxxxxxxx>
- Date: Thu, 19 Jun 2008 02:04:42 +0800
"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.
.
- Follow-Ups:
- Re: why can't i get variable value?
- From: thinktwice
- Re: why can't i get variable value?
- References:
- why can't i get variable value?
- From: thinktwice
- why can't i get variable value?
- Prev by Date: Re: why can't i get variable value?
- Next by Date: Re: how to pass commandline arguments that with blank space to a batch file?
- Previous by thread: Re: why can't i get variable value?
- Next by thread: Re: why can't i get variable value?
- Index(es):
Relevant Pages
|