Jump to content

VBS objShell.run trouble


Recommended Posts

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 this

objShell.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 this

If objFSO.FileExists(objShell.currentDirectory & "\Install\Install_DotNET2.exe") Then
MsgBox(".NET is installed")
End If

And it worked

I can't understand it. An if statement can find it, but a run statement can't?? :unsure:

So if anybody could help me out here It'd be much appreciated :)

Link to comment
Share on other sites


Try this

Dim 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

Link to comment
Share on other sites

Try this
Dim 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

Hi, unfortunetly no.

Again I encounter the same "does not exist" (or whatever) error as before. Vista is perfectly happy with it

Bizzare

Link to comment
Share on other sites

Try this one then

Dim 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,True

I 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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 work

It 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 is

actualPath = Replace(wscript.scriptfullname,wscript.scriptname, "")
quotedPath = """" & path

IF objFSO.FileExists(actualPath & "Install\Install_DotNET2.exe") Then
objShell.Run (quotedPath & "Install\Install_DotNET2.exe""")
Else
MsgBox("Can Not Find This File")
End If

I'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 ;)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...