objShell.Run Error: File Not Found

1.7k views Asked by At

I'm writing a HTA script "Opslag.hta", from which I want to open another HTA script "update.hta".

I'm trying to open "update.hta", which is located in a subfolder (include) to the primary script:

Set objShell = CreateObject("WScript.Shell")
UpdatePath = "include\update.hta"
objShell.Run (chr(34) & UpdatePath & chr(34))

However I'm getting the error "File Not Found" and when I'm prompted if I want to continue running scripts and press "Yes" the "update.hta" actually opens.

I have tried to move the "update.hta" script to different locations and even tried to specify the full path for "update.hta" in "UpdatePath". I have tried with vbQuote, double backslashes (since the "Opslag.hta" is located in a path with spaces) and every possible solution I could find, without succes.

This thread: File not found when using objshell.run -- vbscript, did not help me unfortunately.

I'm running on a Windows 7, 64 bit - don't know if that has anything to do with it.

I really hope someone here has the answer as I'm about to got nuts! =)

Thank you in advance!

2

There are 2 answers

1
MC ND On BEST ANSWER

Note: I'm not sure this is really an answer, but as requested... this is how i saw it:

Reading the code

Set objShell = CreateObject("WScript.Shell")
UpdatePath = "include\update.hta"
objShell.Run (chr(34) & UpdatePath & chr(34))

there are two alternatives

  1. update.hta can not be found. In this case there will be an error and the hta file will not be opened
  2. update.hta can be found. In this case there will be no error and the hta file will be opened

But none of these alternatives match the observed behaviour. The hta file is opened, so it is found. But a error is shown. Since the code in the caller does not include anything that could case the error, the source of the error should be in the called file.

0
Stephen Quan On

Hmmm, it seems you already got the answer to the problem.

It's probably worthwhile to ask the relevant commenter to post their answer so you can mark it as answered.

Whenever I work with paths, I get paranoid about using relative paths. As a general rule, try to use absolute path names instead of relative paths.

For your specific example, we should attempt to derive the absolute path of update.hta from Opslag.hta. We can do this by using document.location from the HTML Dom. For instance, on my computer, I have Opslag.hta under C:\Temp\HTA\Opslag.hta:

  1. document.href returns "file:///C:/Temp/HTA/Opslag.hta"
  2. So, some string manipulation is required to turn it into "C:\Temp\HTA\Opslag.hta"
  3. Then we can use the FileSystemObject to extract the "C:\Temp\HTA" folder.

Another tip is Chr(34) is the same as """". Generally, if you want to escape a double quotes in VBScript you use a pair of double quotes. e.g. If you want John says: "Hello!" as a VBScript string, it's "John says: ""Hello!""".

Here's an example code illustrating the result:

<!DOCTYPE html>
<head>
<title>Opslag.hta test</title>
</head>
<body>
</body>
<script language="VBScript">
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
htaPath = Replace(Replace(document.location, "file:///", ""), "/", "\")
htaFolder = objFSO.GetParentFolderName(htaPath)
updatePath = htaFolder + "\include\update.hta"
MsgBox updatePath
objShell.Run """" & updatePath & """"
</script>
</html>

BTW, this is just an example. I would not normally put large scripts <script> after the <body> however, it's generally good practice if your <script> works with the HTML Document that you wait for the entire DOM to load, i.e. place it after <body>.