Passing a comma (,) into the command line - shell

I have a string which I am passing into the command, the string contains multiple codes which are split up with a comma. When I pass this string through to the command line, it basically stops displaying text after the comma.
domainCodeList = "101, 102, 103, 104, 105"
'Now that we have populated the string with the possible domain codes, we now execute the batch file
strBatchName = SystemData.AppPath + "DetailedContacts.bat" & " " & domainCodeList
Shell strBatchName
When this is ran, the output I get in the command like is:
The code for my batch file is:
#echo off
echo %1
Any help on how I can pass commas to the command line would be great! Thanks
pause

strBatchName = SystemData.AppPath + "DetailedContacts.bat" & " " & """" & domainCodeList & """"
Or
strBatchName = SystemData.AppPath + "DetailedContacts.bat" & " " & Chr(34) & domainCodeList & Chr(34)
Parameters with spaces or special characters in it need to be quoted.
From your batch file use %1 to retrieve the argument with quotes or use %~1 to retrieve the argument without quotes.

Related

using VB6 executing a batch passing two arguments, two variables seperated by a space

I am attempting to execute a batch file with two arguments from vb6.
Shell ("c:\tftp\Createplkstart.txt.bat " & strMsg & " " & cfgSaved & switchIP)
however it seems to drop the second and third arguments.
If I do a msgbox :
MsgBox "\c:\tftp\Createplkstart.txt.bat " & strstart & " " & cfgSaved
it returns two lines with the space " ".
thanks for any advice

WScript.Shell seems to be ignoring escaped quotes [duplicate]

This question already has an answer here:
Filename string space issue in VBScript
(1 answer)
Closed 5 years ago.
In the following script:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "%comspec% /k" & _
" ""C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\TF.exe"" " & _
"move ""C:\Automation\Custom_UiPath_Activities\NuPackages\*.nupkg"" ""C:\Automation\Custom_UiPath_Activities\NuPackages\Old""", 1, True
it gives the error
'C:\Program' is not recognized as an internal or external command.
Seemingly because the escaped quotes around the first argument is ignored.
I've tried logging the string to a text file, and when copying the outputted string into CMD it works as intended.
I cannot see what I have done wrong.
If you want to daisy-chain commands in CMD you need to put an ampersand (&) between them as part of the commandline. Your code is using the ampersand just as the VBScript string concatenation operator. Also, for running multiple commands via cmd /k (or cmd /c) you need to put an additional set of double quotes around them. Also, using variables and a quoting function helps with keeping your code readable.
Function qq(s) : qq = """" & s & """" : End Function
tf = "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\TF.exe"
src = "C:\Automation\Custom_UiPath_Activities\NuPackages"
dst = "C:\Automation\Custom_UiPath_Activities\NuPackages\Old"
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "%comspec% /k """ & qq(tf) & " & " & _
"move " & qq(src & "\*.nupkg") & " " & qq(dst) & """", 1, True

passing arguments(containing white space) in vbs through silk

I'm writing to call a VBScript from Silk bdh and passing arguments from Silk.
My first problem is there are more than 1 argument (total 4 arguments).
My second problem is these arguments contain white space.
Below is the program:
sCmdLine := "cscript.exe";
//sParms := "C:\\QK\\test1_old.vbs \" \"" +string(error_counter)+"\" \"" +error_timer ;
sParms := "C:\\QK\\test1.vbs \" 2\string(error_counter)+
error_timer+error_details+error_time;
hProcessId := ProcessInitialize(sCmdLine, PROCESS_PIPED, sParms,"C:\\WINDOWS\\System32\\");
ProcessSetOutputBuffer(hProcessId, reportedTo, STRING_COMPLETE);
ProcessStart(hProcessId);
StrSearchDelimited(reportedTo,STRING_COMPLETE,reportedTo,"reserved.",1,NULL,1,STR_SEARCH_FIRST);
print("reportedTo*****"+reportedTo);
VBS program is:
dim captcha
errorcounter=Wscript.Arguments(0)
errortimer=Wscript.Arguments(1)
errordetails=Wscript.Arguments(2)
errortime=Wscript.Arguments(3)
text= "Level : " & errorcounter
text= text & vbNewline
text = text & "Page : " & errortimer
text= text & vbNewline
text = text & "Error : " & errordetails
text= text & vbNewline
text = text & "Error Time : " & errortime
reportedto=inputbox( text,"ReportedTo")
You always quote parameters with spaces. This is basic Windows and was introduced 19 years ago. With the exception of chdir and notepad, all other commands and code that parses command lines expect things containing spaces to be quoted.
dir "c:\some folder\some file.txt" /a
In vbs we would write the above string to execute as (chr(34) is a quote char)
"dir " & chr(34) & "c:\some folder\some file.txt" & chr(34) & " /a"

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