TargetPath contains quotes, which cannot be written into a shortcut [duplicate] - vbscript

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.

Related

Macro streamline command error , does not identify " " as string [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.

unable to run VBscript [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.

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.

About using Double quotes in Vbscript

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.

Resources