0
Path = split(wscript.scriptFullName, wscript.scriptname)(0) 
CreateObject("wscript.shell").run(Path & "Name.txt")

The above script works fine if both the file path and file name contain no spaces.

If either contains a space, the result will be;

Error: The system cannot find the file specified.

How can I fix the error?

user692942
  • 16,398
  • 7
  • 76
  • 175
Matthew Wai
  • 962
  • 7
  • 14
  • 1
    Possible duplicate of [VBS with Space in File Path](https://stackoverflow.com/questions/14360599/vbs-with-space-in-file-path) – user692942 Apr 06 '19 at 07:41
  • You could also use the `BuildPath()` method of the `Scripting.FileSystemObject` to avoid this issue altogether. – user692942 Apr 06 '19 at 09:15

2 Answers2

2

The rules are fairly simple:

  1. All strings have to start and end with double quotes to be a valid string.

    Dim a
    a = "Hello World" 'Valid string.
    a = "Hello World  'Not valid and will produce an error.
    
  2. Any use of variables must use the String Concatenation character & to combine them with strings.

    Dim a: a = "Hello"
    Dim b
    b = a & " World" 'Valid concatenated string.
    b = a " World"   'Not valid and will produce an error.
    
  3. As double quotes are used to define a string, all instances of double quotes inside a string must be escaped by doubling the quotes "" but Rule 1. still applies.

    Dim a: a = "Hello"
    Dim b
    b = """" & a & " World""" 'Valid escaped string.
    b = """ & a & " World"""  'Not valid, start of string is not complete 
                              'after escaping the double quote 
                              'producing an error.
    

Follow these three rules and you won't go far wrong.

With those in mind the above line would need to be;

CreateObject("wscript.shell").run("""" & Path & "Name.txt""") 

to generate a string surrounded by literal double quotes.


Useful Links

user692942
  • 16,398
  • 7
  • 76
  • 175
  • 1
    IMO, this would have been better as an answer on the [potential dup-target](https://stackoverflow.com/questions/14360599/vbs-with-space-in-file-path) you proposed and closing this question as a duplicate. Having it here confuses the issue of which question should be the dup-target. – Makyen Apr 06 '19 at 20:32
-3
CreateObject("wscript.shell").run(""""Path & "Name.txt""")

is how.

Noodles
  • 264
  • 2
  • 3
  • Removed a quote. – Noodles Apr 06 '19 at 07:20
  • 1
    Think you mean `CreateObject("wscript.shell").run("""" & Path & "Name.txt""")`. You had it right the first time but didn't concatenate the `Path` variable afterwards. Its also been answered numerous times before, flag and move on. – user692942 Apr 06 '19 at 07:40
  • Please add some explanation to your code such that others can learn from it – Nico Haase Apr 06 '19 at 08:05
  • @Noodles You're right about that but still you're missing the `&` between `""""` and `Path`. Obviously not basic enough. – user692942 Apr 06 '19 at 08:12
  • Yes, there is much to learn even from such simple lines. If you had never used such lines before (cause you've just started to learn a new programming language), it's absolutely neccessary to see some explanation – Nico Haase Apr 06 '19 at 08:25