-1

Hoping somebody can help me. Feel like this should be really simple but cant understand why it wont work. Im trying to loop a folder and open each file. If i hardcode the filename into the command it will open but if i try to pass the filename, it says it cant find the file.

Set objFSO = Createobject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\temp\FCW")
For Each objFile in objFolder.Files
   If LCase(Right(objFile.Path, 4)) = ".fcw" Then   
    'CreateObject("WScript.Shell").Run("""C:\Temp\FCW\FCW Converted.fcw""")   
    CreateObject("WScript.Shell").Run(objFile.Path)
   End If
Next 

Top line within the If statement works and opens the doc but ive tried everything i can think of to pass the name of the file from the loop and cant get it to work. Any ideas what im doing wrong? (havent included all the other commands ive tried just used objFile.Path as an example to keep code down)

Toni
  • 23
  • 7
  • Nothing in your code looks wrong (except the weird tick marks around it - assume that was an attempt at formatting the post). I would try stepping through this code and checking the variables at runtime (or printing them out - if the filenames printed out is exactly the same as the filename "hardcoded", well there really should be no difference). – topsail Nov 03 '22 at 16:09
  • 1
    Are there any spaces in the file name? – LesFerch Nov 03 '22 at 16:20
  • vb.net and vbscript are completely different technologies. Only one of them should be tagged. – Craig Nov 03 '22 at 16:27
  • 1
    @Craig - i did actually know that yes, thanks for taking the time just to point that out. When i was putting the tags in I just typed 'VB' - hadnt realised it had changed it to something else. Removed it. – Toni Nov 03 '22 at 17:58
  • @LesFerch there is in that particular file example yeah. Its called 'FCW Converted'. There will be thousands more files with/without spaces in their name when I come to use the script in the actual folder. – Toni Nov 03 '22 at 18:00
  • @topsail Yeah the tick marks are from formatting into this post. I printed all the variables i've tried so far, they all look correct and match the filepath/name of the actual document thats why I cant work out why it wont work. Pulling my hair out over something so simple. – Toni Nov 03 '22 at 18:03
  • I think the problem is with your wscript run command - that is in essence a command line command, and on command lines you need to quote things like file names with spaces in them. So you should be doing that in your code to. Incidentally, this means that the filenames printed out is NOT the same as the hard coded file names - you need to look closely. One would be `my file.fcw` and the other would be `"my file.fcw"` – topsail Nov 03 '22 at 20:15
  • 1
    Does this answer your question? [VBS with Space in File Path](https://stackoverflow.com/a/14360807) – user692942 Nov 04 '22 at 09:19

1 Answers1

-1

When you execute the .Run command with your shell object, the command is just like a command you would run in a terminal/command line and in the case of file names with spaces, you need to quote these.

So, try this, quoting your file names...

Set objFSO = Createobject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\temp\FCW")
For Each objFile in objFolder.Files
   If LCase(Right(objFile.Path, 4)) = ".fcw" Then   
    'CreateObject("WScript.Shell").Run("""C:\Temp\FCW\FCW Converted.fcw""")   
    CreateObject("WScript.Shell").Run(chr(34) & objFile.Path & Chr(34))
   End If
Next 
topsail
  • 2,186
  • 3
  • 17
  • 17
  • 1
    What's wrong with `CreateObject("WScript.Shell").Run("""" & objFile.Path & """")`? See [VBS with Space in File Path](https://stackoverflow.com/a/51934611). – user692942 Nov 04 '22 at 09:20
  • OMG I owe you a pint! Thank you so much. Something so trivial had me so annoyed :D – Toni Nov 04 '22 at 09:33
  • @user692942 thank you too! I thought i had tried that syntax but just noticed you have 4 x " - i only tried with 3 each side – Toni Nov 04 '22 at 10:04
  • @Toni A string in VBScript begins and ends with a double quote, and any literal quotes inside a string have to doubled to escape them so for example `Dim test: test = """This is a new string"""` will output `"This is a new string"`. This is the same when concatenating (use `&` instead of `+` as you will have unexpected behaviour with integer concatenation) remember each part of a string starts and ends with a double quote, so something like `Dim test: test = """This is a " & """concatenated""" & " string """` will output `"This is a "concatenated" string"`. – user692942 Nov 04 '22 at 11:48
  • @Toni the problem with 3 on each side is you are creating the start of a string and escaping the double quote literal but not ending the string (when it comes to concatenation) so a syntax error will occur. – user692942 Nov 04 '22 at 11:51