Werbung: SecurityConsole.de verwaltet Ihre Computer mit Security Essentails aus der Cloud!
30 Tage kostenfrei testen und 20% Rabatt für Ihre Bestellung mit Promocode: WBF2685582
(Promocode gültig bis 31.12.2011)

Group:  English: Windows Server » microsoft.public.windows.server.scripting
Thread: Command Script variable value lost during execution

HTVi
TV Discussion Newsgroups

Command Script variable value lost during execution
"Alain" <valain[ at ]live.com> 11/5/2008 2:19:19 AM
I made a Command Script to determine the version of RoboCopy.exe found of a
system but the value of the variable is lost during execution and it get
back it's value when exiting the script like if I was using SETLOCAL but I
don't use it...

The problem occur only when I call the script with no parameter, when I use
parameters I get the expected result.

Here is the script:

:: RCVer.cmd Version 8.11.3
::
:: Check the version of RoboCopy from the Date
::

[ at ]ECHO OFF
ECHO Starting %~n0 %* . . .

CALL :GetRCVer %*

IF %RC_Ver% GTR 0 (
ECHO Found RoboCopy Version %RC_Ver%
) ELSE (
ECHO RoboCopy Not Found
ECHO Check for RoboCopy in Path
CALL :GetRCVer RoboCopy.exe /Path
ECHO Found RoboCopy Version %RC_Ver%
)

GOTO :EOF

:GetRCVer
ECHO Calling GetRCVer %* . . .

IF "%~1"=="" (
CALL :GetRCVer RoboCopy.exe
EXIT /B
)
IF /I "%~1"=="/Path" (
CALL :GetRCVer RoboCopy.exe /Path
EXIT /B
)
IF /I "%~2"=="/Path" (
IF NOT EXIST %1 (
IF NOT "%~$PATH:1"=="" (
CALL :GetRCVer "%~$PATH:1"
EXIT /B
)
)
)

SET RC_Ver=0

ECHO Checking %1

IF NOT EXIST %1 (
ECHO %1 Not found
EXIT /B 0
)

SET FileDateTime=%~t1
SET FileDate=%FileDateTime:~,10%
ECHO Date/Time of %~nx1 is %FileDateTime%

IF %FileDate% EQU 18/04/2003 (
SET RC_Ver=10
) ELSE (
IF %FileDate% EQU 22/11/2005 (
SET RC_Ver=26
) ELSE (
IF %FileDate% EQU 02/11/2006 (
SET RC_Ver=27
) ELSE (
SET RC_Ver=%FileDate:~-4%%FileDate:~3,2%
)))

ECHO RoboCopy version %RC_Ver%
EXIT /B %RC_Ver%



Here is two example output of the script:

Output of the script called without parameter:
E:\Test>rcver
Starting RCVer . . .
Calling GetRCVer . . .
Calling GetRCVer RoboCopy.exe . . .
Checking RoboCopy.exe
RoboCopy.exe Not found
RoboCopy Not Found
Check for RoboCopy in Path
Calling GetRCVer RoboCopy.exe /Path . . .
Calling GetRCVer "C:\Programs\Windows Resource Kits\Tools\robocopy.exe" . .
..
Checking "C:\Programs\Windows Resource Kits\Tools\robocopy.exe"
Date/Time of robocopy.exe is 18/04/2003 17:06
RoboCopy version 10
Found RoboCopy Version 0
E:\Test>
The last line say Version 0 but the value of RC_Ver should be 10 instead of
0.

Now if I ask just after exiting the value of RC_Ver I get the expected value
of 10:
E:\Test>set rc_ver
RC_Ver=10



Output of the script called with the /Path parameter:
E:\Test>rcver /path
Starting RCVer /path . . .
Calling GetRCVer /path . . .
Calling GetRCVer RoboCopy.exe /Path . . .
Calling GetRCVer "C:\Programs\Windows Resource Kits\Tools\robocopy.exe" . .
..
Checking "C:\Programs\Windows Resource Kits\Tools\robocopy.exe"
Date/Time of robocopy.exe is 18/04/2003 17:06
RoboCopy version 10
Found RoboCopy Version 10
E:\Test>
The last line say Version 10 which was the expected value...



Re: Command Script variable value lost during execution
"Pegasus \(MVP\)" <I.can[ at ]fly.com.oz> 11/5/2008 12:05:21 PM

"Alain" <valain[ at ]live.com> wrote in message
news:0608EA8C-A61C-42F9-9DE5-7AAB94A8C51B[ at ]microsoft.com...
[Quoted Text]
>I made a Command Script to determine the version of RoboCopy.exe found of a
>system but the value of the variable is lost during execution and it get
>back it's value when exiting the script like if I was using SETLOCAL but I
>don't use it...
>
> The problem occur only when I call the script with no parameter, when I
> use parameters I get the expected result.
>
> Here is the script:
>
> :: RCVer.cmd Version 8.11.3
> ::
> :: Check the version of RoboCopy from the Date
> ::
>
> [ at ]ECHO OFF
> ECHO Starting %~n0 %* . . .
>
> CALL :GetRCVer %*
>
> IF %RC_Ver% GTR 0 (
> ECHO Found RoboCopy Version %RC_Ver%
> ) ELSE (
> ECHO RoboCopy Not Found
> ECHO Check for RoboCopy in Path
> CALL :GetRCVer RoboCopy.exe /Path
> ECHO Found RoboCopy Version %RC_Ver%
> )
>
> GOTO :EOF
>
> :GetRCVer
> ECHO Calling GetRCVer %* . . .
>
> IF "%~1"=="" (
> CALL :GetRCVer RoboCopy.exe
> EXIT /B
> )
> IF /I "%~1"=="/Path" (
> CALL :GetRCVer RoboCopy.exe /Path
> EXIT /B
> )
> IF /I "%~2"=="/Path" (
> IF NOT EXIST %1 (
> IF NOT "%~$PATH:1"=="" (
> CALL :GetRCVer "%~$PATH:1"
> EXIT /B
> )
> )
> )
>
> SET RC_Ver=0
>
> ECHO Checking %1
>
> IF NOT EXIST %1 (
> ECHO %1 Not found
> EXIT /B 0
> )
>
> SET FileDateTime=%~t1
> SET FileDate=%FileDateTime:~,10%
> ECHO Date/Time of %~nx1 is %FileDateTime%
>
> IF %FileDate% EQU 18/04/2003 (
> SET RC_Ver=10
> ) ELSE (
> IF %FileDate% EQU 22/11/2005 (
> SET RC_Ver=26
> ) ELSE (
> IF %FileDate% EQU 02/11/2006 (
> SET RC_Ver=27
> ) ELSE (
> SET RC_Ver=%FileDate:~-4%%FileDate:~3,2%
> )))
>
> ECHO RoboCopy version %RC_Ver%
> EXIT /B %RC_Ver%
>
>
>
> Here is two example output of the script:
>
> Output of the script called without parameter:
> E:\Test>rcver
> Starting RCVer . . .
> Calling GetRCVer . . .
> Calling GetRCVer RoboCopy.exe . . .
> Checking RoboCopy.exe
> RoboCopy.exe Not found
> RoboCopy Not Found
> Check for RoboCopy in Path
> Calling GetRCVer RoboCopy.exe /Path . . .
> Calling GetRCVer "C:\Programs\Windows Resource Kits\Tools\robocopy.exe" .
> . .
> Checking "C:\Programs\Windows Resource Kits\Tools\robocopy.exe"
> Date/Time of robocopy.exe is 18/04/2003 17:06
> RoboCopy version 10
> Found RoboCopy Version 0
> E:\Test>
> The last line say Version 0 but the value of RC_Ver should be 10 instead
> of 0.
>
> Now if I ask just after exiting the value of RC_Ver I get the expected
> value of 10:
> E:\Test>set rc_ver
> RC_Ver=10
>
>
>
> Output of the script called with the /Path parameter:
> E:\Test>rcver /path
> Starting RCVer /path . . .
> Calling GetRCVer /path . . .
> Calling GetRCVer RoboCopy.exe /Path . . .
> Calling GetRCVer "C:\Programs\Windows Resource Kits\Tools\robocopy.exe" .
> . .
> Checking "C:\Programs\Windows Resource Kits\Tools\robocopy.exe"
> Date/Time of robocopy.exe is 18/04/2003 17:06
> RoboCopy version 10
> Found RoboCopy Version 10
> E:\Test>
> The last line say Version 10 which was the expected value...
>
The value is not lost - it doesn't get set. This probably happens because
some of your "if" statements do not execute as you expect, most likely
because of differences in the date format. In general I think that you're
forced to use a fairly convoluted logic in order to achieve your aim because
you use a batch file. The following batch/vbs solution would simplify things
considerably:

[ at ]echo off
for /F "delims=" %%a in ('cscript //nologo d:\tools\which.vbs robocopy') do
"%a" /? | find /i "version"

To make it work you need which.vbs. Some refinements will be necessary to
cater for the case where robocopy cannot be found.

Dim oWshShell, oArgs, oFSO
Dim sName, aExtensions, sExt, Elements, aPaths, p, e, aAux

Set oWshShell = WScript.CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oArgs = WScript.Arguments

aAux = Split(oArgs(0), ".") 'Split the parameter into sName & extension
sName = aAux(0)
sExt = LCase(oWshShell.ExpandEnvironmentStrings("%PathExt%"))
If UBound(aAux) > 0 Then sExt = "." & aAux(1) 'Extension is in parameter
aExtensions = Split(sExt, ";")

aPaths = Split(oWshShell.CurrentDirectory & ";" _
& oWshShell.ExpandEnvironmentStrings("%path%"), ";")

For p = 0 To UBound(aPaths)
if right(aPaths(p), 1) <> "\" then aPaths(p) = aPaths(p) & "\"
For e = 0 To UBound(aExtensions)
If oFSO.FileExists(aPaths(p) & sName & aExtensions(e)) Then
WScript.Echo aPaths(p) & sName & aExtensions(e)
WScript.Quit
End If
Next
Next

WScript.Echo("File not found")


Re: Command Script variable value lost during execution
"Alain" <valain[ at ]live.com> 11/5/2008 1:53:33 PM
The value is definitely SET or how do you explain that I get the value at
the command prompt after the script exit?
I also added all those ECHO to follow the logic and as you can see in the
example output the logic is followed correctly and a recognized version is
found as it ECHO "RoboCopy version 10"


"Pegasus (MVP)" <I.can[ at ]fly.com.oz> wrote in message
news:#admI7zPJHA.496[ at ]TK2MSFTNGP05.phx.gbl...
[Quoted Text]
>
> "Alain" <valain[ at ]live.com> wrote in message
> news:0608EA8C-A61C-42F9-9DE5-7AAB94A8C51B[ at ]microsoft.com...
>>I made a Command Script to determine the version of RoboCopy.exe found of
>>a system but the value of the variable is lost during execution and it get
>>back it's value when exiting the script like if I was using SETLOCAL but I
>>don't use it...
>>
>> The problem occur only when I call the script with no parameter, when I
>> use parameters I get the expected result.
>>
>> Here is the script:
>>
>> :: RCVer.cmd Version 8.11.3
>> ::
>> :: Check the version of RoboCopy from the Date
>> ::
>>
>> [ at ]ECHO OFF
>> ECHO Starting %~n0 %* . . .
>>
>> CALL :GetRCVer %*
>>
>> IF %RC_Ver% GTR 0 (
>> ECHO Found RoboCopy Version %RC_Ver%
>> ) ELSE (
>> ECHO RoboCopy Not Found
>> ECHO Check for RoboCopy in Path
>> CALL :GetRCVer RoboCopy.exe /Path
>> ECHO Found RoboCopy Version %RC_Ver%
>> )
>>
>> GOTO :EOF
>>
>> :GetRCVer
>> ECHO Calling GetRCVer %* . . .
>>
>> IF "%~1"=="" (
>> CALL :GetRCVer RoboCopy.exe
>> EXIT /B
>> )
>> IF /I "%~1"=="/Path" (
>> CALL :GetRCVer RoboCopy.exe /Path
>> EXIT /B
>> )
>> IF /I "%~2"=="/Path" (
>> IF NOT EXIST %1 (
>> IF NOT "%~$PATH:1"=="" (
>> CALL :GetRCVer "%~$PATH:1"
>> EXIT /B
>> )
>> )
>> )
>>
>> SET RC_Ver=0
>>
>> ECHO Checking %1
>>
>> IF NOT EXIST %1 (
>> ECHO %1 Not found
>> EXIT /B 0
>> )
>>
>> SET FileDateTime=%~t1
>> SET FileDate=%FileDateTime:~,10%
>> ECHO Date/Time of %~nx1 is %FileDateTime%
>>
>> IF %FileDate% EQU 18/04/2003 (
>> SET RC_Ver=10
>> ) ELSE (
>> IF %FileDate% EQU 22/11/2005 (
>> SET RC_Ver=26
>> ) ELSE (
>> IF %FileDate% EQU 02/11/2006 (
>> SET RC_Ver=27
>> ) ELSE (
>> SET RC_Ver=%FileDate:~-4%%FileDate:~3,2%
>> )))
>>
>> ECHO RoboCopy version %RC_Ver%
>> EXIT /B %RC_Ver%
>>
>>
>>
>> Here is two example output of the script:
>>
>> Output of the script called without parameter:
>> E:\Test>rcver
>> Starting RCVer . . .
>> Calling GetRCVer . . .
>> Calling GetRCVer RoboCopy.exe . . .
>> Checking RoboCopy.exe
>> RoboCopy.exe Not found
>> RoboCopy Not Found
>> Check for RoboCopy in Path
>> Calling GetRCVer RoboCopy.exe /Path . . .
>> Calling GetRCVer "C:\Programs\Windows Resource Kits\Tools\robocopy.exe" .
>> . .
>> Checking "C:\Programs\Windows Resource Kits\Tools\robocopy.exe"
>> Date/Time of robocopy.exe is 18/04/2003 17:06
>> RoboCopy version 10
>> Found RoboCopy Version 0
>> E:\Test>
>> The last line say Version 0 but the value of RC_Ver should be 10 instead
>> of 0.
>>
>> Now if I ask just after exiting the value of RC_Ver I get the expected
>> value of 10:
>> E:\Test>set rc_ver
>> RC_Ver=10
>>
>>
>>
>> Output of the script called with the /Path parameter:
>> E:\Test>rcver /path
>> Starting RCVer /path . . .
>> Calling GetRCVer /path . . .
>> Calling GetRCVer RoboCopy.exe /Path . . .
>> Calling GetRCVer "C:\Programs\Windows Resource Kits\Tools\robocopy.exe" .
>> . .
>> Checking "C:\Programs\Windows Resource Kits\Tools\robocopy.exe"
>> Date/Time of robocopy.exe is 18/04/2003 17:06
>> RoboCopy version 10
>> Found RoboCopy Version 10
>> E:\Test>
>> The last line say Version 10 which was the expected value...
>>
> The value is not lost - it doesn't get set. This probably happens because
> some of your "if" statements do not execute as you expect, most likely
> because of differences in the date format. In general I think that you're
> forced to use a fairly convoluted logic in order to achieve your aim
> because you use a batch file. The following batch/vbs solution would
> simplify things considerably:
>
> [ at ]echo off
> for /F "delims=" %%a in ('cscript //nologo d:\tools\which.vbs robocopy')
> do "%a" /? | find /i "version"
>
> To make it work you need which.vbs. Some refinements will be necessary to
> cater for the case where robocopy cannot be found.
>
> Dim oWshShell, oArgs, oFSO
> Dim sName, aExtensions, sExt, Elements, aPaths, p, e, aAux
>
> Set oWshShell = WScript.CreateObject("WScript.Shell")
> Set oFSO = CreateObject("Scripting.FileSystemObject")
> Set oArgs = WScript.Arguments
>
> aAux = Split(oArgs(0), ".") 'Split the parameter into sName & extension
> sName = aAux(0)
> sExt = LCase(oWshShell.ExpandEnvironmentStrings("%PathExt%"))
> If UBound(aAux) > 0 Then sExt = "." & aAux(1) 'Extension is in parameter
> aExtensions = Split(sExt, ";")
>
> aPaths = Split(oWshShell.CurrentDirectory & ";" _
> & oWshShell.ExpandEnvironmentStrings("%path%"), ";")
>
> For p = 0 To UBound(aPaths)
> if right(aPaths(p), 1) <> "\" then aPaths(p) = aPaths(p) & "\"
> For e = 0 To UBound(aExtensions)
> If oFSO.FileExists(aPaths(p) & sName & aExtensions(e)) Then
> WScript.Echo aPaths(p) & sName & aExtensions(e)
> WScript.Quit
> End If
> Next
> Next
>
> WScript.Echo("File not found")
>
>
Re: Command Script variable value lost during execution
"Pegasus \(MVP\)" <I.can[ at ]fly.com.oz> 11/5/2008 2:55:10 PM
Sorry, I am unable to debug your script. My %FileDate% variable returns a
value with embedded spaces which causes this line

If %FileDate% EQU 22/11/2005 (

to fail for obvious reasons. While I can easily fix it with surrounding
quotes, it will not be the same environment as yours, thus making my
debugging effort pure guesswork. It is because of the variety of date
formats returned at the Console that I avoid date arithmetic in batch files.
VB Script files are much more robust in this regard.

One possible reason for your problem could be that %RC_VER% is not numeric -
perhaps because it has some leading or trailing spaces. You can make this
visible by changing this line
ECHO RoboCopy version %RC_Ver%
to
ECHO RoboCopy version xxx%RC_Ver%yyy

"Alain" <valain[ at ]live.com> wrote in message
news:383E509F-68D0-4890-B226-F9F3BD64F12F[ at ]microsoft.com...
[Quoted Text]
> The value is definitely SET or how do you explain that I get the value at
> the command prompt after the script exit?
> I also added all those ECHO to follow the logic and as you can see in the
> example output the logic is followed correctly and a recognized version is
> found as it ECHO "RoboCopy version 10"
>
>
> "Pegasus (MVP)" <I.can[ at ]fly.com.oz> wrote in message
> news:#admI7zPJHA.496[ at ]TK2MSFTNGP05.phx.gbl...
>>
>> "Alain" <valain[ at ]live.com> wrote in message
>> news:0608EA8C-A61C-42F9-9DE5-7AAB94A8C51B[ at ]microsoft.com...
>>>I made a Command Script to determine the version of RoboCopy.exe found of
>>>a system but the value of the variable is lost during execution and it
>>>get back it's value when exiting the script like if I was using SETLOCAL
>>>but I don't use it...
>>>
>>> The problem occur only when I call the script with no parameter, when I
>>> use parameters I get the expected result.
>>>
>>> Here is the script:
>>>
>>> :: RCVer.cmd Version 8.11.3
>>> ::
>>> :: Check the version of RoboCopy from the Date
>>> ::
>>>
>>> [ at ]ECHO OFF
>>> ECHO Starting %~n0 %* . . .
>>>
>>> CALL :GetRCVer %*
>>>
>>> IF %RC_Ver% GTR 0 (
>>> ECHO Found RoboCopy Version %RC_Ver%
>>> ) ELSE (
>>> ECHO RoboCopy Not Found
>>> ECHO Check for RoboCopy in Path
>>> CALL :GetRCVer RoboCopy.exe /Path
>>> ECHO Found RoboCopy Version %RC_Ver%
>>> )
>>>
>>> GOTO :EOF
>>>
>>> :GetRCVer
>>> ECHO Calling GetRCVer %* . . .
>>>
>>> IF "%~1"=="" (
>>> CALL :GetRCVer RoboCopy.exe
>>> EXIT /B
>>> )
>>> IF /I "%~1"=="/Path" (
>>> CALL :GetRCVer RoboCopy.exe /Path
>>> EXIT /B
>>> )
>>> IF /I "%~2"=="/Path" (
>>> IF NOT EXIST %1 (
>>> IF NOT "%~$PATH:1"=="" (
>>> CALL :GetRCVer "%~$PATH:1"
>>> EXIT /B
>>> )
>>> )
>>> )
>>>
>>> SET RC_Ver=0
>>>
>>> ECHO Checking %1
>>>
>>> IF NOT EXIST %1 (
>>> ECHO %1 Not found
>>> EXIT /B 0
>>> )
>>>
>>> SET FileDateTime=%~t1
>>> SET FileDate=%FileDateTime:~,10%
>>> ECHO Date/Time of %~nx1 is %FileDateTime%
>>>
>>> IF %FileDate% EQU 18/04/2003 (
>>> SET RC_Ver=10
>>> ) ELSE (
>>> IF %FileDate% EQU 22/11/2005 (
>>> SET RC_Ver=26
>>> ) ELSE (
>>> IF %FileDate% EQU 02/11/2006 (
>>> SET RC_Ver=27
>>> ) ELSE (
>>> SET RC_Ver=%FileDate:~-4%%FileDate:~3,2%
>>> )))
>>>
>>> ECHO RoboCopy version %RC_Ver%
>>> EXIT /B %RC_Ver%
>>>
>>>
>>>
>>> Here is two example output of the script:
>>>
>>> Output of the script called without parameter:
>>> E:\Test>rcver
>>> Starting RCVer . . .
>>> Calling GetRCVer . . .
>>> Calling GetRCVer RoboCopy.exe . . .
>>> Checking RoboCopy.exe
>>> RoboCopy.exe Not found
>>> RoboCopy Not Found
>>> Check for RoboCopy in Path
>>> Calling GetRCVer RoboCopy.exe /Path . . .
>>> Calling GetRCVer "C:\Programs\Windows Resource Kits\Tools\robocopy.exe"
>>> . . .
>>> Checking "C:\Programs\Windows Resource Kits\Tools\robocopy.exe"
>>> Date/Time of robocopy.exe is 18/04/2003 17:06
>>> RoboCopy version 10
>>> Found RoboCopy Version 0
>>> E:\Test>
>>> The last line say Version 0 but the value of RC_Ver should be 10 instead
>>> of 0.
>>>
>>> Now if I ask just after exiting the value of RC_Ver I get the expected
>>> value of 10:
>>> E:\Test>set rc_ver
>>> RC_Ver=10
>>>
>>>
>>>
>>> Output of the script called with the /Path parameter:
>>> E:\Test>rcver /path
>>> Starting RCVer /path . . .
>>> Calling GetRCVer /path . . .
>>> Calling GetRCVer RoboCopy.exe /Path . . .
>>> Calling GetRCVer "C:\Programs\Windows Resource Kits\Tools\robocopy.exe"
>>> . . .
>>> Checking "C:\Programs\Windows Resource Kits\Tools\robocopy.exe"
>>> Date/Time of robocopy.exe is 18/04/2003 17:06
>>> RoboCopy version 10
>>> Found RoboCopy Version 10
>>> E:\Test>
>>> The last line say Version 10 which was the expected value...
>>>
>> The value is not lost - it doesn't get set. This probably happens because
>> some of your "if" statements do not execute as you expect, most likely
>> because of differences in the date format. In general I think that you're
>> forced to use a fairly convoluted logic in order to achieve your aim
>> because you use a batch file. The following batch/vbs solution would
>> simplify things considerably:
>>
>> [ at ]echo off
>> for /F "delims=" %%a in ('cscript //nologo d:\tools\which.vbs robocopy')
>> do "%a" /? | find /i "version"
>>
>> To make it work you need which.vbs. Some refinements will be necessary to
>> cater for the case where robocopy cannot be found.
>>
>> Dim oWshShell, oArgs, oFSO
>> Dim sName, aExtensions, sExt, Elements, aPaths, p, e, aAux
>>
>> Set oWshShell = WScript.CreateObject("WScript.Shell")
>> Set oFSO = CreateObject("Scripting.FileSystemObject")
>> Set oArgs = WScript.Arguments
>>
>> aAux = Split(oArgs(0), ".") 'Split the parameter into sName & extension
>> sName = aAux(0)
>> sExt = LCase(oWshShell.ExpandEnvironmentStrings("%PathExt%"))
>> If UBound(aAux) > 0 Then sExt = "." & aAux(1) 'Extension is in parameter
>> aExtensions = Split(sExt, ";")
>>
>> aPaths = Split(oWshShell.CurrentDirectory & ";" _
>> & oWshShell.ExpandEnvironmentStrings("%path%"), ";")
>>
>> For p = 0 To UBound(aPaths)
>> if right(aPaths(p), 1) <> "\" then aPaths(p) = aPaths(p) & "\"
>> For e = 0 To UBound(aExtensions)
>> If oFSO.FileExists(aPaths(p) & sName & aExtensions(e)) Then
>> WScript.Echo aPaths(p) & sName & aExtensions(e)
>> WScript.Quit
>> End If
>> Next
>> Next
>>
>> WScript.Echo("File not found")
>>
>>


Re: Command Script variable value lost during execution
"Alain" <valain[ at ]live.com> 11/5/2008 3:51:05 PM
I didn't thought of Regional Settings but it could be an issue when running
it on other systems... The script should still run properly on my system
though and I'm very curious about finding what the problem is...

The value of RC_ver is numeric but ECHO would return it numeric or not...
It would only be a problem for the exit code that must be numeric...
I checked ERRORLEVEL at the end of the script and it was 0 but again
checking ERRORLEVEL at the command prompt returned the expected
value of 10


"Pegasus (MVP)" <I.can[ at ]fly.com.oz> wrote in message
news:OPGRCa1PJHA.3876[ at ]TK2MSFTNGP04.phx.gbl...
[Quoted Text]
> Sorry, I am unable to debug your script. My %FileDate% variable returns a
> value with embedded spaces which causes this line
>
> If %FileDate% EQU 22/11/2005 (
>
> to fail for obvious reasons. While I can easily fix it with surrounding
> quotes, it will not be the same environment as yours, thus making my
> debugging effort pure guesswork. It is because of the variety of date
> formats returned at the Console that I avoid date arithmetic in batch
> files. VB Script files are much more robust in this regard.
>
> One possible reason for your problem could be that %RC_VER% is not
> numeric - perhaps because it has some leading or trailing spaces. You can
> make this visible by changing this line
> ECHO RoboCopy version %RC_Ver%
> to
> ECHO RoboCopy version xxx%RC_Ver%yyy
>
> "Alain" <valain[ at ]live.com> wrote in message
> news:383E509F-68D0-4890-B226-F9F3BD64F12F[ at ]microsoft.com...
>> The value is definitely SET or how do you explain that I get the value at
>> the command prompt after the script exit?
>> I also added all those ECHO to follow the logic and as you can see in the
>> example output the logic is followed correctly and a recognized version
>> is
>> found as it ECHO "RoboCopy version 10"
>>
>>
>> "Pegasus (MVP)" <I.can[ at ]fly.com.oz> wrote in message
>> news:#admI7zPJHA.496[ at ]TK2MSFTNGP05.phx.gbl...
>>>
>>> "Alain" <valain[ at ]live.com> wrote in message
>>> news:0608EA8C-A61C-42F9-9DE5-7AAB94A8C51B[ at ]microsoft.com...
>>>>I made a Command Script to determine the version of RoboCopy.exe found
>>>>of a system but the value of the variable is lost during execution and
>>>>it get back it's value when exiting the script like if I was using
>>>>SETLOCAL but I don't use it...
>>>>
>>>> The problem occur only when I call the script with no parameter, when I
>>>> use parameters I get the expected result.
>>>>
>>>> Here is the script:
>>>>
>>>> :: RCVer.cmd Version 8.11.3
>>>> ::
>>>> :: Check the version of RoboCopy from the Date
>>>> ::
>>>>
>>>> [ at ]ECHO OFF
>>>> ECHO Starting %~n0 %* . . .
>>>>
>>>> CALL :GetRCVer %*
>>>>
>>>> IF %RC_Ver% GTR 0 (
>>>> ECHO Found RoboCopy Version %RC_Ver%
>>>> ) ELSE (
>>>> ECHO RoboCopy Not Found
>>>> ECHO Check for RoboCopy in Path
>>>> CALL :GetRCVer RoboCopy.exe /Path
>>>> ECHO Found RoboCopy Version %RC_Ver%
>>>> )
>>>>
>>>> GOTO :EOF
>>>>
>>>> :GetRCVer
>>>> ECHO Calling GetRCVer %* . . .
>>>>
>>>> IF "%~1"=="" (
>>>> CALL :GetRCVer RoboCopy.exe
>>>> EXIT /B
>>>> )
>>>> IF /I "%~1"=="/Path" (
>>>> CALL :GetRCVer RoboCopy.exe /Path
>>>> EXIT /B
>>>> )
>>>> IF /I "%~2"=="/Path" (
>>>> IF NOT EXIST %1 (
>>>> IF NOT "%~$PATH:1"=="" (
>>>> CALL :GetRCVer "%~$PATH:1"
>>>> EXIT /B
>>>> )
>>>> )
>>>> )
>>>>
>>>> SET RC_Ver=0
>>>>
>>>> ECHO Checking %1
>>>>
>>>> IF NOT EXIST %1 (
>>>> ECHO %1 Not found
>>>> EXIT /B 0
>>>> )
>>>>
>>>> SET FileDateTime=%~t1
>>>> SET FileDate=%FileDateTime:~,10%
>>>> ECHO Date/Time of %~nx1 is %FileDateTime%
>>>>
>>>> IF %FileDate% EQU 18/04/2003 (
>>>> SET RC_Ver=10
>>>> ) ELSE (
>>>> IF %FileDate% EQU 22/11/2005 (
>>>> SET RC_Ver=26
>>>> ) ELSE (
>>>> IF %FileDate% EQU 02/11/2006 (
>>>> SET RC_Ver=27
>>>> ) ELSE (
>>>> SET RC_Ver=%FileDate:~-4%%FileDate:~3,2%
>>>> )))
>>>>
>>>> ECHO RoboCopy version %RC_Ver%
>>>> EXIT /B %RC_Ver%
>>>>
>>>>
>>>>
>>>> Here is two example output of the script:
>>>>
>>>> Output of the script called without parameter:
>>>> E:\Test>rcver
>>>> Starting RCVer . . .
>>>> Calling GetRCVer . . .
>>>> Calling GetRCVer RoboCopy.exe . . .
>>>> Checking RoboCopy.exe
>>>> RoboCopy.exe Not found
>>>> RoboCopy Not Found
>>>> Check for RoboCopy in Path
>>>> Calling GetRCVer RoboCopy.exe /Path . . .
>>>> Calling GetRCVer "C:\Programs\Windows Resource Kits\Tools\robocopy.exe"
>>>> . . .
>>>> Checking "C:\Programs\Windows Resource Kits\Tools\robocopy.exe"
>>>> Date/Time of robocopy.exe is 18/04/2003 17:06
>>>> RoboCopy version 10
>>>> Found RoboCopy Version 0
>>>> E:\Test>
>>>> The last line say Version 0 but the value of RC_Ver should be 10
>>>> instead of 0.
>>>>
>>>> Now if I ask just after exiting the value of RC_Ver I get the expected
>>>> value of 10:
>>>> E:\Test>set rc_ver
>>>> RC_Ver=10
>>>>
>>>>
>>>>
>>>> Output of the script called with the /Path parameter:
>>>> E:\Test>rcver /path
>>>> Starting RCVer /path . . .
>>>> Calling GetRCVer /path . . .
>>>> Calling GetRCVer RoboCopy.exe /Path . . .
>>>> Calling GetRCVer "C:\Programs\Windows Resource Kits\Tools\robocopy.exe"
>>>> . . .
>>>> Checking "C:\Programs\Windows Resource Kits\Tools\robocopy.exe"
>>>> Date/Time of robocopy.exe is 18/04/2003 17:06
>>>> RoboCopy version 10
>>>> Found RoboCopy Version 10
>>>> E:\Test>
>>>> The last line say Version 10 which was the expected value...
>>>>
>>> The value is not lost - it doesn't get set. This probably happens
>>> because some of your "if" statements do not execute as you expect, most
>>> likely because of differences in the date format. In general I think
>>> that you're forced to use a fairly convoluted logic in order to achieve
>>> your aim because you use a batch file. The following batch/vbs solution
>>> would simplify things considerably:
>>>
>>> [ at ]echo off
>>> for /F "delims=" %%a in ('cscript //nologo d:\tools\which.vbs robocopy')
>>> do "%a" /? | find /i "version"
>>>
>>> To make it work you need which.vbs. Some refinements will be necessary
>>> to cater for the case where robocopy cannot be found.
>>>
>>> Dim oWshShell, oArgs, oFSO
>>> Dim sName, aExtensions, sExt, Elements, aPaths, p, e, aAux
>>>
>>> Set oWshShell = WScript.CreateObject("WScript.Shell")
>>> Set oFSO = CreateObject("Scripting.FileSystemObject")
>>> Set oArgs = WScript.Arguments
>>>
>>> aAux = Split(oArgs(0), ".") 'Split the parameter into sName & extension
>>> sName = aAux(0)
>>> sExt = LCase(oWshShell.ExpandEnvironmentStrings("%PathExt%"))
>>> If UBound(aAux) > 0 Then sExt = "." & aAux(1) 'Extension is in parameter
>>> aExtensions = Split(sExt, ";")
>>>
>>> aPaths = Split(oWshShell.CurrentDirectory & ";" _
>>> & oWshShell.ExpandEnvironmentStrings("%path%"), ";")
>>>
>>> For p = 0 To UBound(aPaths)
>>> if right(aPaths(p), 1) <> "\" then aPaths(p) = aPaths(p) & "\"
>>> For e = 0 To UBound(aExtensions)
>>> If oFSO.FileExists(aPaths(p) & sName & aExtensions(e)) Then
>>> WScript.Echo aPaths(p) & sName & aExtensions(e)
>>> WScript.Quit
>>> End If
>>> Next
>>> Next
>>>
>>> WScript.Echo("File not found")
>>>
>>>
>
>
Re: Command Script variable value lost during execution
"Alain" <valain[ at ]live.com> 11/6/2008 8:08:35 PM
I just noticed something very weird, if I make a call inside an IF block the
values are not passed back to the IF block after exit so if inside that
block I check the values after the call I will not have the right values, I
will only have them outside the IF block...

So this is why RC_Ver is 0 inside the ELSE part of my IF block, if I check
the value again right after the end of the IF block I will have a value of
10 as expected...


"Alain" <valain[ at ]live.com> wrote in message
news:0608EA8C-A61C-42F9-9DE5-7AAB94A8C51B[ at ]microsoft.com...
[Quoted Text]
> I made a Command Script to determine the version of RoboCopy.exe found of
> a system but the value of the variable is lost during execution and it get
> back it's value when exiting the script like if I was using SETLOCAL but I
> don't use it...
>
> The problem occur only when I call the script with no parameter, when I
> use parameters I get the expected result.
>
> Here is the script:
>
> :: RCVer.cmd Version 8.11.3
> ::
> :: Check the version of RoboCopy from the Date
> ::
>
> [ at ]ECHO OFF
> ECHO Starting %~n0 %* . . .
>
> CALL :GetRCVer %*
>
> IF %RC_Ver% GTR 0 (
> ECHO Found RoboCopy Version %RC_Ver%
> ) ELSE (
> ECHO RoboCopy Not Found
> ECHO Check for RoboCopy in Path
> CALL :GetRCVer RoboCopy.exe /Path
> ECHO Found RoboCopy Version %RC_Ver%
> )
>
> GOTO :EOF
>
> :GetRCVer
> ECHO Calling GetRCVer %* . . .
>
> IF "%~1"=="" (
> CALL :GetRCVer RoboCopy.exe
> EXIT /B
> )
> IF /I "%~1"=="/Path" (
> CALL :GetRCVer RoboCopy.exe /Path
> EXIT /B
> )
> IF /I "%~2"=="/Path" (
> IF NOT EXIST %1 (
> IF NOT "%~$PATH:1"=="" (
> CALL :GetRCVer "%~$PATH:1"
> EXIT /B
> )
> )
> )
>
> SET RC_Ver=0
>
> ECHO Checking %1
>
> IF NOT EXIST %1 (
> ECHO %1 Not found
> EXIT /B 0
> )
>
> SET FileDateTime=%~t1
> SET FileDate=%FileDateTime:~,10%
> ECHO Date/Time of %~nx1 is %FileDateTime%
>
> IF %FileDate% EQU 18/04/2003 (
> SET RC_Ver=10
> ) ELSE (
> IF %FileDate% EQU 22/11/2005 (
> SET RC_Ver=26
> ) ELSE (
> IF %FileDate% EQU 02/11/2006 (
> SET RC_Ver=27
> ) ELSE (
> SET RC_Ver=%FileDate:~-4%%FileDate:~3,2%
> )))
>
> ECHO RoboCopy version %RC_Ver%
> EXIT /B %RC_Ver%
>
>
>
> Here is two example output of the script:
>
> Output of the script called without parameter:
> E:\Test>rcver
> Starting RCVer . . .
> Calling GetRCVer . . .
> Calling GetRCVer RoboCopy.exe . . .
> Checking RoboCopy.exe
> RoboCopy.exe Not found
> RoboCopy Not Found
> Check for RoboCopy in Path
> Calling GetRCVer RoboCopy.exe /Path . . .
> Calling GetRCVer "C:\Programs\Windows Resource Kits\Tools\robocopy.exe" .
> . .
> Checking "C:\Programs\Windows Resource Kits\Tools\robocopy.exe"
> Date/Time of robocopy.exe is 18/04/2003 17:06
> RoboCopy version 10
> Found RoboCopy Version 0
> E:\Test>
> The last line say Version 0 but the value of RC_Ver should be 10 instead
> of 0.
>
> Now if I ask just after exiting the value of RC_Ver I get the expected
> value of 10:
> E:\Test>set rc_ver
> RC_Ver=10
>
>
>
> Output of the script called with the /Path parameter:
> E:\Test>rcver /path
> Starting RCVer /path . . .
> Calling GetRCVer /path . . .
> Calling GetRCVer RoboCopy.exe /Path . . .
> Calling GetRCVer "C:\Programs\Windows Resource Kits\Tools\robocopy.exe" .
> . .
> Checking "C:\Programs\Windows Resource Kits\Tools\robocopy.exe"
> Date/Time of robocopy.exe is 18/04/2003 17:06
> RoboCopy version 10
> Found RoboCopy Version 10
> E:\Test>
> The last line say Version 10 which was the expected value...
>
>
>
Re: Command Script variable value lost during execution
"Pegasus \(MVP\)" <I.can[ at ]fly.com.oz> 11/6/2008 8:40:01 PM

"Alain" <valain[ at ]live.com> wrote in message
news:186B2532-9957-49A3-ACE1-5AB6AD25F6EE[ at ]microsoft.com...
[Quoted Text]
>I just noticed something very weird, if I make a call inside an IF block
>the values are not passed back to the IF block after exit so if inside that
>block I check the values after the call I will not have the right values, I
>will only have them outside the IF block...
>
> So this is why RC_Ver is 0 inside the ELSE part of my IF block, if I check
> the value again right after the end of the IF block I will have a value of
> 10 as expected...

This is probably the old trap of scanning command lines. Consider the
following code fragments:

Sample 1
======
[ at ]echo off
set name=Alain
set mode=one
if %mode%==one (
set name=Angus
echo Name=%name%
)
echo Name=%name%

Sample2
[ at ]echo off
setlocal EnableDelayedExpansion
set name=Alain
set mode=one
if %mode%==one (
set name=Angus
echo Name=!name!
)
echo Name=%name%

What's going on? Very simple: %variables% in batch files are resolved one
line at a time and only once. The lines surrounded by parenthesis are
treated as one single line, hence %name% in Sample 1 is set to Alain because
that's its value when the line is first scanned.

In Sample 2 we force each line to be scanned at execution time if it
contains a !variable!. The instruction in Line 2 invokes this mode.

In summary: If you set a variable in an "if" block then you have two choices
to interrogate this variable:
a) Outside the "if" block, or
b) Inside the "if" block if you use delayed expansion and surround the
variable with exclamation marks.


Home | Search | Terms | Imprint Contact
Newsgroups Reader - provided by WiredBox.Net
Suche nach Orten, Städten, Postleitzahlen, Vorwahlen, Kfz-Kennzeichen