0

I'm quite new to VBS and I want to start a file from a VBS script. But I've got a problem getting the shell.run to work when I use a String variable.

Set objShell = CreateObject("WScript.Shell")
sNewestFile = "D:\Path with spaces\calendar with spaces.ics"
objShell.Run sNewestFile

If I change it to the actual String instead of the Variable, it works.

Set objShell = CreateObject("WScript.Shell") 
objShell.Run """D:\Path with spaces\calendar with spaces.ics"""

I've tested several things with different combinations of "" around the variable but nothing seems to work. Either there is a failure that the System can't find the file or a Compiler error.

2
  • 2
    You need to enclose with double quotes: sNewestFile = """D:\Path with spaces\calendar with spaces.ics""" Commented Dec 19, 2016 at 13:26
  • It's pretty obvious that those two examples are not like for like, the second is correctly escape using "" while the first is not. Commented Dec 19, 2016 at 13:41

1 Answer 1

2

To quote your variable, i recommend you to use this function :

Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function

And your code looks like this one :

Set Ws = CreateObject("WScript.Shell")
sNewestFile = "D:\Path with spaces\calendar with spaces.ics"
Ws.Run DblQuote(sNewestFile),1,True
'****************************************
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'****************************************
Sign up to request clarification or add additional context in comments.

3 Comments

Why when sNewestFile = """D:\Path with spaces\calendar with spaces.ics""" works just as well? It's not the first time you've posted this either, why not at the very least write a comment and link to your previous answer?
Amazing how many times you have mentioned this one function - site:stackoverflow.com +"DblQuote"+"Hackoo"
@Lankymart WOWW I'm famous in google engine LOL :D Ok next time i will link it ;) instead of copy and paste this function I wish a nice day man ;) and thank for your remark !

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.