Re: Command Script variable value lost during execution



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@xxxxxxxxxx> wrote in message news:#admI7zPJHA.496@xxxxxxxxxxxxxxxxxxxxxxx

"Alain" <valain@xxxxxxxx> wrote in message news:0608EA8C-A61C-42F9-9DE5-7AAB94A8C51B@xxxxxxxxxxxxxxxx
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
::

@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:

@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")


.



Relevant Pages

  • Re: Command Script variable value lost during execution
    ... The value of RC_ver is numeric but ECHO would return it numeric or not... ... I checked ERRORLEVEL at the end of the script and it was 0 but again ... ECHO RoboCopy version xxx%RC_Ver%yyy ... CALL:GetRCVer RoboCopy.exe /Path ...
    (microsoft.public.windows.server.scripting)
  • Re: Command Script variable value lost during execution
    ... If %FileDate% EQU 22/11/2005 ( ... VB Script files are much more robust in this regard. ... ECHO RoboCopy version xxx%RC_Ver%yyy ...
    (microsoft.public.windows.server.scripting)
  • Re: Command Script variable value lost during execution
    ... The problem occur only when I call the script with no parameter, ... :: Check the version of RoboCopy from the Date ... ECHO Found RoboCopy Version %RC_Ver% ... CALL:GetRCVer RoboCopy.exe /Path ...
    (microsoft.public.windows.server.scripting)
  • Command Script variable value lost during execution
    ... 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... ... :: Check the version of RoboCopy from the Date ... ECHO Found RoboCopy Version %RC_Ver% ... CALL:GetRCVer RoboCopy.exe /Path ...
    (microsoft.public.windows.server.scripting)
  • Re: [SLE] Setting a script to automatically download a file from nai.com ftp site?
    ... You will need to put it in a script file and run it from cron. ... echo "Retrieving update list" ... echo "downloading $FTPDIR$DATNAME - please wait" ...
    (SuSE)

Loading