VBS call CMD with Quote in Argument - vbscript

I am trying to pass a snipit of code as a command line argument to a program running on WinXP.
I have not been able to pass the entire code snipit as one argument.
It gets split by SPACE and QUOTE characters (as expected).
Quoting the whole thing and escaping QUOTE chars with QUOTE, SLASH or CARAT also fails...
Const TheCode = "main(""Literal String Argument"", 123, true)"
Set oShell = WScript.CreateObject ("WScript.Shell")
oShell.Run """c:\Folder with space\ArgTest.vbs"" """ & TheCode & """"
I run the following vbs script to echo the arguments back to test what is recieved.
ArgTest.vbs:
Dim ArgumentIndex
For ArgumentIndex = 0 to Wscript.Arguments.Count - 1
Call Msgbox(Wscript.Arguments(ArgumentIndex), vbOKOnly+vbInformation, "Argument " & ArgumentIndex)
Next
Edit:
It turns out that it is the VBS Echo script that stips out the QUOTES. Use QUOTE to escape.

There is no standard in Windows for escaping quotes. It is entirely up to the discretion of the developer of the app how quotes should be escaped. If you don't have documentation for your app that explains how to do it, then you will have to discover it on your own (assuming the app even supports escaped quotes)
It sounds like you tried back slash:
Const TheCode = "main(\""Literal String Argument\"", 123, true)"
You also say you tried caret:
Const TheCode = "main(^""Literal String Argument^"", 123, true)"
Have you tried doubling up the quotes?
Const TheCode = "main(""""Literal String Argument"""", 123, true)"

Related

Weird problem with running file in visual basic 6 [duplicate]

I have a very basic doubt in vb scripting:
Msgbox "This is myName" ' This works fine
Msgbox "This is "myName"" ' This gives an error
Msgbox "This is ""myName""" 'This works fine
My question is if I need to save (in a variable) or display string with double quotes why I need to use a doble quotes twice for the word or phrase. Does using a common double quotes doesn't mean I want to display the entire thing or could be saved as string in variable?
In VBScript, string literals are surrounded by double quotes ("). This is what your first example shows:
Msgbox "This is myName" ' This works fine
However, if you want to include a double quote character inside of your string literal, you've got a problem, because VBScript is going to interpret the second double quote character it finds as signifying the end of the string literal. This is what your second example shows:
Msgbox "This is "myName"" ' This gives an error
^ ' because it prematurely terminates the string here
' and doesn't know what to do with the trailing "
Fortunately, there's an escape hatch. It involves escaping the double quote character with another character, indicating that VBScript should process it as a literal double quote character, rather than a magical "end-of-string-literal" character. It just so happens that the escape character VBScript uses is a double quote character. This is what your second example shows:
Msgbox "This is ""myName""" 'This works fine
You begin the string with a single double-quote, indicating the start of a string literal.
Then you want to have an embedded double quote character, so you use two of them. This is where the escaping starts: you escape the double quote character with another double quote character.
Then you do that escaping thing again.
Finally, you terminate the entire string literal with another double quote character.
Other languages often use a backslash (\) as the escape character. That might make things easier to see. Assuming VBScript used a backslash as the escape character rather than a double quote, your code would look like this:
Msgbox "This is \"myName\"" ' doesn't work in VBScript; example only
If this syntax bothers you, you can declare a constant for the double quote and use that each time:
Const Quote = """"
' ... later in the code ...
Msgbox "This is " & Quote & "myName" & Quote
Each language has its own escape character. By chance or not, in VB/VBS it is double quote-mark. And also by chance or not, we can embed only double quote in literal string. We cannot embed other special characters as Tab for example.
However, using VB/VBS escape character simplify our coding.
str = """D:\path\to\xyz.exe"" ""arg 1"" ""arg 2"""
WScript.Echo str ' "D:\path\to\xyz.exe" "arg 1" "arg 2"
str = Chr(34) & "D:\path\to\xyz.exe" & Chr(34) & " " _
& Chr(34) & "arg 1" & Chr(34) & " " & Chr(34) & "arg 2" & Chr(34)
WScript.Echo str ' "D:\path\to\xyz.exe" "arg 1" "arg 2"
str = Join(Array("", "D:\path\to\xyz.exe", " ", "arg 1", " ", "arg 2", ""), Chr(34))
WScript.Echo str ' "D:\path\to\xyz.exe" "arg 1" "arg 2"
But personally I prefer using Replace as it make my code more readable.
str = Replace("'D:\path\to\xyz.exe' 'arg 1' 'arg 2'", Chr(39), Chr(34))
WScript.Echo str ' "D:\path\to\xyz.exe" "arg 1" "arg 2"
You can use Replace (if that convenient to you) and for the rest special characters.
str = Replace(Replace("A|B|C!1|2|3", "!", vbNewLine), "|", vbTab)
WScript.Echo str
'A B C
'1 2 3
String literals have to be delimited to separate what you what in the string from the outside. The string delimiter in VBScript is " (double quote). Other languages use " too, some have ' (single quote) as an alternative or a delimiter with a slightly different semantic.
To contain the delimiter in a string literal it has to be escaped (marked as not meaning 'end' or 'begin' of the string. The escape marker for " in string literals is " . giving "" - in VBscript.
Other languages use \" to escape a double quote.
So
Msgbox "This is ""myName""" 'This works fine
x = "This is ""myName"""
is correct VBScript if you what to display (or store) This is "myName".
Double quotes in VBScript enclose a string. If you insert a single double qoute inside a string it terminates the string prematurely and the remainder causes an error. Because of that you must escape double quotes inside a string, which can be done by doubling them. That's why your 3rd command works while your 2nd doesn't.

Getting folder path with space error - CreateObject ("Scripting.FileSystemObject") [duplicate]

I have a very basic doubt in vb scripting:
Msgbox "This is myName" ' This works fine
Msgbox "This is "myName"" ' This gives an error
Msgbox "This is ""myName""" 'This works fine
My question is if I need to save (in a variable) or display string with double quotes why I need to use a doble quotes twice for the word or phrase. Does using a common double quotes doesn't mean I want to display the entire thing or could be saved as string in variable?
In VBScript, string literals are surrounded by double quotes ("). This is what your first example shows:
Msgbox "This is myName" ' This works fine
However, if you want to include a double quote character inside of your string literal, you've got a problem, because VBScript is going to interpret the second double quote character it finds as signifying the end of the string literal. This is what your second example shows:
Msgbox "This is "myName"" ' This gives an error
^ ' because it prematurely terminates the string here
' and doesn't know what to do with the trailing "
Fortunately, there's an escape hatch. It involves escaping the double quote character with another character, indicating that VBScript should process it as a literal double quote character, rather than a magical "end-of-string-literal" character. It just so happens that the escape character VBScript uses is a double quote character. This is what your second example shows:
Msgbox "This is ""myName""" 'This works fine
You begin the string with a single double-quote, indicating the start of a string literal.
Then you want to have an embedded double quote character, so you use two of them. This is where the escaping starts: you escape the double quote character with another double quote character.
Then you do that escaping thing again.
Finally, you terminate the entire string literal with another double quote character.
Other languages often use a backslash (\) as the escape character. That might make things easier to see. Assuming VBScript used a backslash as the escape character rather than a double quote, your code would look like this:
Msgbox "This is \"myName\"" ' doesn't work in VBScript; example only
If this syntax bothers you, you can declare a constant for the double quote and use that each time:
Const Quote = """"
' ... later in the code ...
Msgbox "This is " & Quote & "myName" & Quote
Each language has its own escape character. By chance or not, in VB/VBS it is double quote-mark. And also by chance or not, we can embed only double quote in literal string. We cannot embed other special characters as Tab for example.
However, using VB/VBS escape character simplify our coding.
str = """D:\path\to\xyz.exe"" ""arg 1"" ""arg 2"""
WScript.Echo str ' "D:\path\to\xyz.exe" "arg 1" "arg 2"
str = Chr(34) & "D:\path\to\xyz.exe" & Chr(34) & " " _
& Chr(34) & "arg 1" & Chr(34) & " " & Chr(34) & "arg 2" & Chr(34)
WScript.Echo str ' "D:\path\to\xyz.exe" "arg 1" "arg 2"
str = Join(Array("", "D:\path\to\xyz.exe", " ", "arg 1", " ", "arg 2", ""), Chr(34))
WScript.Echo str ' "D:\path\to\xyz.exe" "arg 1" "arg 2"
But personally I prefer using Replace as it make my code more readable.
str = Replace("'D:\path\to\xyz.exe' 'arg 1' 'arg 2'", Chr(39), Chr(34))
WScript.Echo str ' "D:\path\to\xyz.exe" "arg 1" "arg 2"
You can use Replace (if that convenient to you) and for the rest special characters.
str = Replace(Replace("A|B|C!1|2|3", "!", vbNewLine), "|", vbTab)
WScript.Echo str
'A B C
'1 2 3
String literals have to be delimited to separate what you what in the string from the outside. The string delimiter in VBScript is " (double quote). Other languages use " too, some have ' (single quote) as an alternative or a delimiter with a slightly different semantic.
To contain the delimiter in a string literal it has to be escaped (marked as not meaning 'end' or 'begin' of the string. The escape marker for " in string literals is " . giving "" - in VBscript.
Other languages use \" to escape a double quote.
So
Msgbox "This is ""myName""" 'This works fine
x = "This is ""myName"""
is correct VBScript if you what to display (or store) This is "myName".
Double quotes in VBScript enclose a string. If you insert a single double qoute inside a string it terminates the string prematurely and the remainder causes an error. Because of that you must escape double quotes inside a string, which can be done by doubling them. That's why your 3rd command works while your 2nd doesn't.

redirecting "svn info" output to file in Windows Script Host created an empty file [duplicate]

I have a very basic doubt in vb scripting:
Msgbox "This is myName" ' This works fine
Msgbox "This is "myName"" ' This gives an error
Msgbox "This is ""myName""" 'This works fine
My question is if I need to save (in a variable) or display string with double quotes why I need to use a doble quotes twice for the word or phrase. Does using a common double quotes doesn't mean I want to display the entire thing or could be saved as string in variable?
In VBScript, string literals are surrounded by double quotes ("). This is what your first example shows:
Msgbox "This is myName" ' This works fine
However, if you want to include a double quote character inside of your string literal, you've got a problem, because VBScript is going to interpret the second double quote character it finds as signifying the end of the string literal. This is what your second example shows:
Msgbox "This is "myName"" ' This gives an error
^ ' because it prematurely terminates the string here
' and doesn't know what to do with the trailing "
Fortunately, there's an escape hatch. It involves escaping the double quote character with another character, indicating that VBScript should process it as a literal double quote character, rather than a magical "end-of-string-literal" character. It just so happens that the escape character VBScript uses is a double quote character. This is what your second example shows:
Msgbox "This is ""myName""" 'This works fine
You begin the string with a single double-quote, indicating the start of a string literal.
Then you want to have an embedded double quote character, so you use two of them. This is where the escaping starts: you escape the double quote character with another double quote character.
Then you do that escaping thing again.
Finally, you terminate the entire string literal with another double quote character.
Other languages often use a backslash (\) as the escape character. That might make things easier to see. Assuming VBScript used a backslash as the escape character rather than a double quote, your code would look like this:
Msgbox "This is \"myName\"" ' doesn't work in VBScript; example only
If this syntax bothers you, you can declare a constant for the double quote and use that each time:
Const Quote = """"
' ... later in the code ...
Msgbox "This is " & Quote & "myName" & Quote
Each language has its own escape character. By chance or not, in VB/VBS it is double quote-mark. And also by chance or not, we can embed only double quote in literal string. We cannot embed other special characters as Tab for example.
However, using VB/VBS escape character simplify our coding.
str = """D:\path\to\xyz.exe"" ""arg 1"" ""arg 2"""
WScript.Echo str ' "D:\path\to\xyz.exe" "arg 1" "arg 2"
str = Chr(34) & "D:\path\to\xyz.exe" & Chr(34) & " " _
& Chr(34) & "arg 1" & Chr(34) & " " & Chr(34) & "arg 2" & Chr(34)
WScript.Echo str ' "D:\path\to\xyz.exe" "arg 1" "arg 2"
str = Join(Array("", "D:\path\to\xyz.exe", " ", "arg 1", " ", "arg 2", ""), Chr(34))
WScript.Echo str ' "D:\path\to\xyz.exe" "arg 1" "arg 2"
But personally I prefer using Replace as it make my code more readable.
str = Replace("'D:\path\to\xyz.exe' 'arg 1' 'arg 2'", Chr(39), Chr(34))
WScript.Echo str ' "D:\path\to\xyz.exe" "arg 1" "arg 2"
You can use Replace (if that convenient to you) and for the rest special characters.
str = Replace(Replace("A|B|C!1|2|3", "!", vbNewLine), "|", vbTab)
WScript.Echo str
'A B C
'1 2 3
String literals have to be delimited to separate what you what in the string from the outside. The string delimiter in VBScript is " (double quote). Other languages use " too, some have ' (single quote) as an alternative or a delimiter with a slightly different semantic.
To contain the delimiter in a string literal it has to be escaped (marked as not meaning 'end' or 'begin' of the string. The escape marker for " in string literals is " . giving "" - in VBscript.
Other languages use \" to escape a double quote.
So
Msgbox "This is ""myName""" 'This works fine
x = "This is ""myName"""
is correct VBScript if you what to display (or store) This is "myName".
Double quotes in VBScript enclose a string. If you insert a single double qoute inside a string it terminates the string prematurely and the remainder causes an error. Because of that you must escape double quotes inside a string, which can be done by doubling them. That's why your 3rd command works while your 2nd doesn't.

How to execute shell command without double Quotes

I need to execute the below command in command prompt using vb6,
quser /server:machinename
So my code is,
mycommand = "quser / server: & strString" 'where strstring will be my machine name..
Shell "cmd.exe" & mycommand
The problem is it is executing in command prompt with the double quotes. But if double quotes is there, I wont get the expected results. I need to execute the above without double quotes.
Please let me know your comments.
You can use double double quotes to encode a double quote in a vb6 string.
e.g.
Dim str as String
str = "This is some text with a " & """double quote""" & "in it."
MsgBox str
Will show a message: This is some text with a "double quote" in it.
This is what I think you want:
mycommand = """quser / server:" & strString & """"
Shell "cmd.exe " & mycommand
Note that you did not have a space after cmd.exe
The first problem I can see is that your last double quote is in the wrong place and so
mycommand is just the string "quser / server: & strString".
To append the value of a variable called strString to the end of the string you would use
mycommand = "quser / server:" & strString
The next problem is that there needs to be a space between cmd.exe and the parameters string.
Either replace "quser... with " quser... or replace "cmd.exe" with "cmd.exe "

Passing Caret to command line using VBScript

Having a hard time figuring this one out. I have a vbscript that asks for username and password. Then the script runs PSExec.exe passing those to the command line like this.
strUser = Inputbox("Username:","Username")
strPassword = Inputbox("Password:","Password")
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "%comspec% /K psexec.exe \\workstation -u " & struser _
& " -p " & strPassword & " -d calc.exe", 1, false
This works fine if the password doesn't have a caret in it. BUT if it does have one, for example if the password is "Pass)(*&^%$##!" I get the following error from psexec.
'%$##!' is not recognized as an internal or external command,
operable program or batch file.
The caret is being read by the command line as a space so it thinks it's trying to run this command.
psexec.exe \\workstation64 -u User -p Pass)(*& %$##! -d Calc.exe
As you can see there is a space instead of a caret so psexec see's %$##! as the command.
I've seen an example for passing it when using a batch file but it doesn't work in this case. It said adding an extra ^ like this ^^ works in a bat file.
How can I pass a caret to the command line????
UPDATE......
I worked on this for about 45 minutes today at work and couldn't get it to work. First thing I tried was to add quotes to the password and it still didn't work. I just tested it here at the house and it worked fine....... OS at work is Windows XP and at home I run Windows 7. I will try again in the morning to make sure I didn't mistype something. Here is what I did to make it work at home on Windows 7.
strUser = Inputbox("Username:","Username")
strPassword = Inputbox("Password:","Password")
strPassword = chr(34) & strPassword & chr(34)
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "%comspec% /K psexec.exe \\workstation -u " & struser _
& " -p " & strPassword & " -d calc.exe", 1, false
The problem lays in using of ampersand, not caret: single ampersand is used as a command separator, so rest of the line after &-sign considered to be a next command by cmd interpreter.
In next example I have used SET command instead of PSExec, as I will not experiment with PSExec.
All goes well with SET command, even if escaped all characters:), but I'm not sure about percentage sign, which is said "must be doubled to use literally".
Dim strPssw: strPssw = "/Pass"")=(*&^%$##!"
Dim ii, strChar, strEscape
Dim strPassword: strPassword = ""
For ii = 1 To Len( strPssw) 'cannot use Replace() function
strChar = Mid( strPssw, ii, 1)
Select Case strChar
Case "&", """", "^", "%", "=", _
"#", "(", ")", "!", "*", "?", _
".", "\", "|", ">", "<", "/"
strEscape = "^"
Case Else
strEscape = "" 'all goes well even if strEscape = "^" here
End Select
strPassword = strPassword & strEscape & strChar
Next
Dim objShell: Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "%comspec% /C set varia=" & strPassword & "&set varia&pause&set varia=", 1, false
' # - At Symbol: be less verbose
' & - Single Ampersand: used as a command separator
' ^ - Caret: general escape character in batch
' " - Double Quote: surrounding a string in double quotes escapes all of the characters contained within it
' () - Parentheses: used to make "code blocks" of grouped commands
' % - Percentage Sign: are used to mark some variables, must be doubled to use literally
' ! - Exclamation Mark: to mark delayed expansion environment variables !variable!
' * - Asterisk: wildcard matches any number or any characters
' ? - Question Mark: matches any single character
' . - Single dot: represents the current directory
' \ - Backslash: represent the root directory of a drive dir ^\
' | - Single Pipe: redirects the std.output of one command into the std.input of another
' > - Single Greater Than: redirects output to either a file or file like device
' < - Less Than: redirect the contents of a file to the std.input of a command
''
' && - Double Ampersand: conditional command separator (if errorlevel 0)
' .. - Double dot: represents the parent directory of the current directory
' || - Double Pipe: conditional command separator (if errorlevel > 0)
' :: - Double Colon: alternative to "rem" for comments outside of code blocks
' >> - Double Greater than: output will be added to the very end of the file
' thanks to http://judago.webs.com/batchoperators.htm
'

Resources