| Home | Register | Members | Search | Windows Vista Tips | File Database | Links |
![]() |
| Thread Tools | Display Modes |
|
|
|
| |
|
Pegasus \(MVP\)
Guest
Posts: n/a
|
"Alain" <> wrote in message news:0608EA8C-A61C-42F9-9DE5-... >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") |
|
|
|
|
|||
|
|||
|
Alain
Guest
Posts: n/a
|
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)" <> wrote in message news:#... > > "Alain" <> wrote in message > news:0608EA8C-A61C-42F9-9DE5-... >>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") > > |
|
|
|
|
|||
|
|||
|
Pegasus \(MVP\)
Guest
Posts: n/a
|
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" <> wrote in message news:383E509F-68D0-4890-B226-... > 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)" <> wrote in message > news:#... >> >> "Alain" <> wrote in message >> news:0608EA8C-A61C-42F9-9DE5-... >>>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") >> >> |
|
|
|
|
|||
|
|||
|
Alain
Guest
Posts: n/a
|
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)" <> wrote in message news:... > 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" <> wrote in message > news:383E509F-68D0-4890-B226-... >> 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)" <> wrote in message >> news:#... >>> >>> "Alain" <> wrote in message >>> news:0608EA8C-A61C-42F9-9DE5-... >>>>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") >>> >>> > > |
|
|
|
|
|||
|
|||
|
Alain
Guest
Posts: n/a
|
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" <> wrote in message news:0608EA8C-A61C-42F9-9DE5-... > 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... > > > |
|
|
|
|
|||
|
|||
|
Pegasus \(MVP\)
Guest
Posts: n/a
|
"Alain" <> wrote in message news:186B2532-9957-49A3-ACE1-... >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 ====== @echo off set name=Alain set mode=one if %mode%==one ( set name=Angus echo Name=%name% ) echo Name=%name% Sample2 @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. |
|
|
|
|
|||
|
|||
|
|
|
| |
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to add a variable to a shell command in a vbscript | Dee | Scripting | 3 | 04-28-2008 03:43 PM |
| (msh) get-command does not work in variable provider | /\\/\\o\\/\\/ | Scripting | 3 | 04-21-2006 11:18 PM |
| Re: Can I use a variable in a netsh command | Thomas Lee | Scripting | 3 | 10-05-2005 06:48 PM |
| Re: Can I use a variable in a netsh command | /\\/\\o\\/\\/ | Scripting | 0 | 10-05-2005 06:06 PM |
| Passing Variable to Command Line | Ernie | Scripting | 1 | 08-15-2003 09:03 PM |
Forum Software Powered by vBulletin®, Copyright Jelsoft Enterprises Ltd.
SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc. |



Linear Mode

