How to parse a command-line in the same way that VBScript does

vbscript command line arguments example
how to debug vbscript in command prompt
how to run a vbs script in command prompt
how to call vbscript function from command line
vbscript run command line
vbscript function arguments
passing parameters to vbscript from batch file
vbscript user input command line

I have a simple C++ program that lists the arguments it gets:

#include <iostream>

int main(int argc, char* argv[]) {
    for (int i = 1; i < argc; i++) {
        std::cout << argv[i] << std::endl;
    }
    return 0;
}

I'm just testing/figuring this out but the ultimate intention is for this to accept the names of some files and directories.

So I pass it these arguments:

"\\server\directory\file.ext" "C:\trailing\backslash\" "file.txt"

But this is what's printed:

\\server\directory\file.ext 
C:\trailing\backslash" file.txt

i.e. the trailing slash on the second argument makes it think the closing quote is escaped.

I'm setting the arguments in the property pages of VS2017, but I get the same output when:

  • I call the exe from PowerShell.
  • I allow an external tool to pass my exe arguments that it has built (which will be how this is ultimately used).

How can I get my program to understand that the quoted path with a trailing slash is one argument?

EDIT Section 4 of this article describes exactly my problem. Basically I want to make my C++ program interpret arguments in the same way that a batch file or VBScript does. I cannot change how the arguments come in to my program

EDIT I will simplify the question:

The C++ program above behaves like this:

I want it to behave like this:

What do I have to do to the program or compiler in order for that to happen?

Windows 7 Resource Kit, As soon as you move from the command line to a script, such options In this example, as long as you are not at the end of the stream, you will continue to read the is used in the same way that the WhileWend statement is used in VBScript. Passing an argument that starts with “//” to a VBscript, will by default be taken as options for the windows script host itself (cscript or wscript). To avoid this pass a double slash "//" to end the argument parsingof cscript/wscript. Alternatively use a named argument as below.

I tried compiling this with an extra slash to the second argument like below and was able to produce the desired output !

"\\server\directory\file.ext" "C:\trailing\backslash\\" "file.txt"

This was the output

/home/a.out                                                                                                                          
\server\directory\file.ext                                                                                                           
C:\trailing\backslash\                                                                                                               
file.txt 

Upvote my post if it helps you :D

Command line arguments - VBScript, How-to: VBScript command line arguments into a VBScript, those values can then be picked up within the script by reading the script is called all of the arguments must be present and passed in the same order. If Passing an argument that starts with “//” to a VBscript, will by default be taken as options for the windows  I'm sure there's a way to do it with only one script, and I'm not using ping or trying to get an IP address of a hostname. So anyway, now I'm trying to apply the same logic in VBScript, but I'm not sure how to retrieve only the "token" I need out of these results, not knowing the value of the token of course.

If you are not happy with the command line parsing you can do it by your own:

Just use GetCommandLine and parse your command line as you want it.

Other post already described how the command line is/should be treated (see blog post of Raymond Chen).

VBScript Programmer's Reference, Adrian Kingsley-Hughes, Kathie Kingsley-Hughes, Daniel Read If a specific Web site is not specified at the command line, the default will be MSDN. Then, we check to see if the first element of the array is the same as the commandLine  In VBScript you can use an underscore to signal that a statement will continue on the next line. For example: MsgBox "This is a message" Can be replaced by: MsgBox _ "This is a message" If the line break occurs in the middle of a string you can use the concatenation character & to append the string together so it will still appear on one line.

How Command Line Parameters Are Parsed, The Command Prompt Command Line Parameter Parsing Rules (cmd.exe) a Batch File from the Command Line; a VBScript, JScript, or WSH script from the Command Line as well as the GNOME desktop (ubuntu-desktop) on the same machine. 3. How does a C/C++ program on Windows get argv[]? Typically, one prints in VBscript using a Wscript.echo("Hello, world!") line or some variant thereof. If you do not invoke with a cscript hello-world.vbs, you get a GUI/pop-up MsgBox, which I wish to avoid. I just want something to go right to the command line for output, without putting cscript at the beginning.

Managing Windows with VBScript and WMI, If you'd like to learn more about StdIn and command-line text input, refer to the you use every day are command-line utilities, such as ipconfig. ping, and tracert. The scripting engine includes a built-in parameter-parsing object named  In my case, testing with the above VBScript, I consistently encountered the following error, but only when the VBScript was run by NowSMS as a 2-way command: c:\temp\2way.vbs(20, 2) Microsoft Excel: Microsoft Excel cannot access the file 'c:\temp\test.xlsx'. There are several possible reasons: - The file name or path does not exist.

Microsoft PowerShell, VBScript and JScript Bible, same as calling: Get-WmiObject –class Win32_QuickfixEngineering –Server Srv1 parameters on a command line with commas, but this can cause unexpected parser in PowerShell will treat a list separated by commas as a single array,  Unlike the Wscript.Echo command, Stdout.Write allows you to write multiple separate items to the same line, use .WriteBlankLines(n) to add newlines. “A truth that's told with bad intent, Beats all the lies you can invent” - William Blake. Related:.Echo Echo, popup - .Popup MsgBox - Display a dialogue box message.

Comments
  • The article Everyone quotes command line arguments the wrong way presents function ArgvQuote() that you can use to quote arguments correctly (whether or not it has a trailing backslash). This is something the caller of your program has to respect. Otherwise it's a bug in their program!
  • Both of those articles are about how to correctly quote and escape characters in a command line that you're creating. I'm talking about the resulting content inside argv being rubbish when my program is given a pretty standard Windows style quoted argument that many programs can deal with (a path with a trailing slash with quotes around it)
  • There is a de facto standard of how command-line arguments are parsed that is defined by the Microsoft C runtime and CommandLineToArgvV. This de facto standard requires to double a backslash, if it is followed by a double-quotation mark. If the programs you are talking about can deal with unescaped backslash, they are parsing command-lines in a non-standard way (e. g. by calling GetCommandLine() and doing manual parsing.
  • Looking at the source code for an open source project (7-Zip as it happens) I can see non standard parsing going on if compiled on WIN32. How disappointing. SplitCommandLine gets called from main... Oh well... github.com/kornelski/7z/blob/master/CPP/Common/…
  • I took the liberty to clarify your question title to make it clear from the beginning, what you want.
  • Here is a detailed explanation of C++ command-line parameters.
  • I agree, but what if I'm not in control of how the arguments are built?
  • @Jobbo "but what if I'm not in control of how the arguments are built?" You can always check if the command line parameter has a trailing backslash and consider that situation in your program code, easy like that.
  • Can you elaborate on how I'd do that in C++?
  • @πάνταῥεῖ It's not that easy to deal with such bad input. Consider "C:\path with spaces\". The C runtime will split this into these arguments: C:\path, with, spaces". It's not clear how to "fix" such input, if possible at all. It's the job of the caller to fix their program by quoting properly.