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.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Thorlus
  • 23
  • 4

1 Answers1

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
'****************************************
Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • 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? – user692942 Dec 19 '16 at 15:33
  • 2
    Amazing how many times you have mentioned this one function - [`site:stackoverflow.com +"DblQuote"+"Hackoo"`](https://www.google.com/search?q=site%3Astackoverflow.com&oq=site%3Astackoverflow.com&aqs=chrome..69i57j69i58j69i59l3j69i60.12846j0j7&sourceid=chrome&ie=UTF-8&q=+#q=site:stackoverflow.com+%2B%22DblQuote%22%2B%22Hackoo%22&safe=off&start=20&pws=1) – user692942 Dec 19 '16 at 15:44
  • @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 ! – Hackoo Dec 19 '16 at 15:56