0

I'm trying to write a vbscript that will run two other vbscripts. Here's my current code:

Dim objShell, oFSO

Set objShell = Wscript.CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")

' Determine script location for VBScript
sScriptDir = oFSO.GetParentFolderName(WScript.ScriptFullName)

' Run scripts
objShell.Run oFSO.BuildPath(sScriptDir, "code\saveExcel.vbs") , 0, true

objShell.Run oFSO.BuildPath(sScriptDir, "code\launchScript.vbs") , 0, true

' close shell
Set objShell = Nothing

The folder is structured as follows:

enter image description here

When I run my script I get this error message: enter image description here

I have written dozens of scripts that run other scripts in the same format as the above script, but this is the only one that doesn't work and I have no clue why.

Chase Grimm
  • 417
  • 3
  • 14
  • What value are you getting in your `sScriptDir` variable. FSO has a limit of 255 characters for paths, and since this is possibly a network drive that might be buried a few folders deep, perhaps `GetParentFolderName` is getting the longer UNC path and blowing over the character limit. – JNevill Aug 05 '16 at 14:44
  • It's 57 character long, much less than the maximum length. It's also not pulling the full UNC path, but if it was it would still be less than 100 characters. – Chase Grimm Aug 05 '16 at 14:49
  • Do you get the same error if you hard code the path instead of using `BuildPath`? I'm at a loss here since I everything looks A-OK with your code. – JNevill Aug 05 '16 at 14:53
  • Yep, I get the same answer with a hard coded path as well. I too am at a loss. I have written 6 scripts this week that are exactly the same as this one (besides the filenames) and they all work. I've also tried moving all the files to my computer locally and off the network, but i still get the same error. – Chase Grimm Aug 05 '16 at 15:03
  • 1
    Last ditch effort: The `sScriptDir` has spaces in the file path. Try shoving double quotes into the string so that wscript.run doesn't try to parse the string delimitting on the space: `objShell.Run """" & oFSO.BuildPath(sScriptDir, "code\launchScript.vbs") & """" , 0, true ` – JNevill Aug 05 '16 at 15:06
  • 1
    I think that might be the solution, judging [by this answer](http://stackoverflow.com/questions/33389660/vbscript-the-system-cannot-find-the-file-specified). – JNevill Aug 05 '16 at 15:07
  • YES! Thank you so much! I didn't realize it until now, but all of the other scripts that do this are using a path that has no spaces. – Chase Grimm Aug 05 '16 at 15:08
  • Great! I'm going to post as an answer for the next poor soul that runs into this oddball FSO behavior. – JNevill Aug 05 '16 at 15:09

1 Answers1

1

fso.Run() requires that paths containing spaces be quoted. So... even though you are passing it a string containing the path, the string itself has to contain the double quotes.

So when calling objShell.Run() in your code:

objShell.Run """" & oFSO.BuildPath(sScriptDir, "code\saveExcel.vbs") & """", 0, true
JNevill
  • 46,980
  • 4
  • 38
  • 63