Deman Posted May 11, 2007 Posted May 11, 2007 Hi there, I'm having the strangest problem in getting my vbscript to work. Basically what the script does is check if the .NET framework is installed, and if it isn't the script provides the option to install it.The script can run the executable provided it is situated in the same folder as the script itself. But since I like things to be tidy I want this (and other installs) to be in a separate folder which is one step above the current directory (be it C, D or whatever the CD rom happens to be at the time).The code I'm using is thisobjShell.Run (objShell.currentDirectory & "\Install\Install_DotNET2.exe")It runs in Vista, but refuses to run in XP resulting in "file not found" errors, but when I stuck an if statement to search for the install file, it found it fine??To give you a better idea of what I mean by that I did thisIf objFSO.FileExists(objShell.currentDirectory & "\Install\Install_DotNET2.exe") Then MsgBox(".NET is installed")End IfAnd it workedI can't understand it. An if statement can find it, but a run statement can't?? So if anybody could help me out here It'd be much appreciated
gunsmokingman Posted May 11, 2007 Posted May 11, 2007 Try thisDim Act :Set Act = CreateObject("Wscript.Shell")Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")Dim Net :Set Net = Fso.GetFile(Act.CurrentDirectory & "\Install\Install_DotNET2.exe") Act.Run(Net.Path),1,True
Deman Posted May 11, 2007 Author Posted May 11, 2007 Try thisDim Act :Set Act = CreateObject("Wscript.Shell")Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")Dim Net :Set Net = Fso.GetFile(Act.CurrentDirectory & "\Install\Install_DotNET2.exe") Act.Run(Net.Path),1,TrueHi, unfortunetly no. Again I encounter the same "does not exist" (or whatever) error as before. Vista is perfectly happy with itBizzare
gunsmokingman Posted May 11, 2007 Posted May 11, 2007 Try this one thenDim Act :Set Act = CreateObject("Wscript.Shell")Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")Dim Net :Set Net = Fso.GetFile(WScript.ScriptFullName) Act.Run(Net.Path & "\Install\Install_DotNET2.exe"),1,TrueI think it better to use a If statement because if it not there then you will get error, with the If statement you can either surpress the standard error message or you can add a custom error message. I try to always use the If statement.Dim Act :Set Act = CreateObject("Wscript.Shell")Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")Dim Loc :Set Loc = Fso.GetFile(WScript.ScriptFullName)Dim Net :Net = Loc.Path & "\Install\Install_DotNET2.exe" If Fso.FileExists(Net) Then '/-> Chr(34) = ", In Case There Is A Space In The Path Name Act.Run(Chr(34) & Net & Chr(34)),1,True Else Act.Popup "Can Not Find This File" & vbCrLf & Net, 10, "Missing", 4128 End If
Yzöwl Posted May 11, 2007 Posted May 11, 2007 in a separate folder which is one step above the current directoryI may have missed the point here but should you not be using ".." or "ParentDirectory"
IcemanND Posted May 11, 2007 Posted May 11, 2007 Your current path will be the path from which you are calling the script, not the path in which the script resides. So say the script is in d:\scripts and you were to open a command prompt to c:\ and run "wscript d:\scripts\installit.vbs" the current path would be C:\ not d:\scripts.if you want the actual path that the script resides in you could use:Replace(WScript.ScriptFullName, WScript.ScriptName, "")which will return the script path location with the trailing backslash.
Deman Posted May 11, 2007 Author Posted May 11, 2007 Thanks for your help guys, sorry gunsmokingman but your examples didn't work (though the If file exists is a good idea that never occurred to me)I switched to using Replace(WScript.ScriptFullName, WScript.ScriptName, "") and it still refused to workIt turns out that the run statement is sensitive to spaces in file names (with XP anyway). So after a bit of jiggling to get the right amount of "s it's working on XP! The code used isactualPath = Replace(wscript.scriptfullname,wscript.scriptname, "")quotedPath = """" & pathIF objFSO.FileExists(actualPath & "Install\Install_DotNET2.exe") Then objShell.Run (quotedPath & "Install\Install_DotNET2.exe""")Else MsgBox("Can Not Find This File")End IfI'm not too sure why I can't use the quotedPath variable in the FileExists statement in the same style as the Run (I'm not really familiar with Vbs) but it works. Again, cheers for the help ..Hopefully Vista will take the statement and not throw an error
IcemanND Posted May 11, 2007 Posted May 11, 2007 to make life a little easier with quotes you can also use either chr(34) or set a constant to be a quotation mark.vbQuote = chr(34)actualPath = Replace(wscript.scriptfullname,wscript.scriptname, "")IF objFSO.FileExists(actualPath & "Install\Install_DotNET2.exe") Then objShell.Run (vbQuote & actualPath & "Install\Install_DotNET2.exe" & vbQuote)Else MsgBox("Can Not Find This File")End If
gunsmokingman Posted May 11, 2007 Posted May 11, 2007 In my example I did add the Chr(34) for the quotes.'/-> Chr(34) = ", In Case There Is A Space In The Path Name Act.Run(Chr(34) & Net & Chr(34)),1,True
Deman Posted May 12, 2007 Author Posted May 12, 2007 Ahhh ok, sorry I didn't realise what that did. Cheers
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now