-1

Using the WshShell Run Method using a variable..

Very Inexplicable result, Here is my file: Read the Comments. Using Windows 11 on Surface Pro 8

Dim Sexy,WshShell3,fil

Set WshShell3 = WScript.CreateObject("WScript.Shell")

'WshShell3.Run """.\Src and Dst.txt"""
'Above Line Works

fil = ".\Src and Dst.txt"
Sexy = chr(34)+chr(34)+chr(34) + fil +chr(34)+chr(34)+chr(34)

Wscript.Echo Sexy

WshShell3.Run Sexy
'Above line Opens up the "This PC" Explorer Window

Set WshShell3 = Nothing


Wscript.Quit
Étienne Laneville
  • 4,697
  • 5
  • 13
  • 29
  • The doubled up quotes allow for a literal to contain quotes. Your variable has too many quotes. To see for yourself, change your line `Wscript.Echo Sexy` to `Wscript.Echo Sexy & VBCRLF & """.\Src and Dst.txt"""` and note the difference. – LesFerch May 21 '23 at 14:36
  • 1
    Just write `Sexy = """" & fil & """"`, it is absolutely possible. – user692942 May 22 '23 at 06:49
  • 1
    It's the difference between how VBScript defines strings and how literal quotes are used in strings. In your example, you are ending up with `""".\Src and Dst.txt"""` as a literal string, which when put through `Run` will not behave as you expect. To add literal quotes to the string you could have done `Sexy = """" & fil & """"` or `Sexy = Chr(34) & fil & Chr(34)` both give the same result which would be `".\Src and Dst.txt"` (the quotes here are outputted). You only need to escape quotes (by doubling them) when you want to use them as literals, this doesn't apply to `Chr(34)`. – user692942 May 22 '23 at 07:37
  • user692942 You seem to be right. I really do not understand why. I will have to ponder this. The run method works with """.Src and Dst.txt""" hard coded (not placing into a variable and then trying to use the run method on the variable). The three quotes are necessary in the hard coded Run, or it will not open the file. Somehow placing it in a variable with a single set of quotes Works, which makes no sense. And the Sexy = """" & fil & """"" showing a single set of quotes also when I echo Sexy (and it works) also baffles me ! Thanks for the Response ! – user21932362 May 22 '23 at 20:23
  • I just do not understand: when I construct a variable that by echo shows it is exactly what works when I hard code it into the .run line does not work when it is in a variable. If I write the variable to a cmd file, and run the cmd file: it works. I would really like to understand this better. – user21932362 May 23 '23 at 00:15
  • 1
    Here's your statement that's causing you confusion: `"The three quotes are necessary in the hard coded Run"`. There are NOT three quotes in the final string that's passed to the Run method. The outermost quote defines the literal. The two inner quotes define a single quote because doubled-up quotes are how you escape a quote character in VBScript. In the end, your literal becomes a string surrounded by one set of quotes, exactly the same as the string in the variable. – LesFerch May 23 '23 at 01:04
  • 1
    @user21932362 the duplicate question covers it. – user692942 May 23 '23 at 11:13

1 Answers1

-4

I do not believe you can use the Run Method and run cmd/c in any way with a variable. My variable reproduced the exact Line of code that worked, it does not have too many quotes . I solved my issue by writing the appropriate command line for a cmd file. Then ran the cmd file invisibly. My get around works flawlessly. It gets around the inefficiencies of the WshShell Run Method....

  • I assure you, a variable can be used just fine. See my earlier comment. – LesFerch May 22 '23 at 02:11
  • 1
    It absolutely is possible, you’re just over complicating it with the `Chr(34)`, see [my comment](https://stackoverflow.com/questions/76296780/how-can-i-use-the-wshshell-run-method-with-a-variable-in-vbscript-on-windows-11#comment134554928_76296780) on the question. – user692942 May 22 '23 at 06:50
  • @user21932362 I did in [the comments](https://stackoverflow.com/questions/76296780/how-can-i-use-the-wshshell-run-method-with-a-variable-in-vbscript-on-windows-11#comment134555451_76296780). – user692942 May 23 '23 at 08:03