Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a string containing the * (asterisk) character as a command line argument in Windows Shell

I am looking for a way to pass a string containg the "*" character to an executable via the command line.

  command.exe 3*2

I want to pass the string 3*2. What Windows does instead is search the current directory for files matching the file mask "3*2", and passes any files it found to command.exe

Putting "3*2" between double quotes does not help, still the same problem.

I also tried '3*2' (between single quotes), but then this whole string (including the single quotes) is passed, which is not what I need.

Is there any way to pass the string 3*2 (without any quotes) to the command?

like image 384
Mike Warner Avatar asked Mar 03 '10 20:03

Mike Warner


People also ask

What does * do in command line?

The wildcard * selects all of the files in the current directory. The above example will copy all of the files in the current directory to the directory called satire.

How do I pass a command line argument in Windows?

For example, entering C:\abc.exe /W /F on a command line would run a program called abc.exe and pass two command line arguments to it: /W and /F. The abc.exe program would see those arguments and handle them internally.

How do you type special characters in CMD?

In Windows, you can type any character you want by holding down the ALT key, typing a sequence of numbers, then releasing the ALT key.

How do I escape special characters in CMD?

Windows Command Prompt The Windows command-line interpreter uses a caret character ( ^ ) to escape reserved characters that have special meanings (in particular: & , | , ( , ) , < , > , ^ ).


2 Answers

In Windows command shells, the command you execute is responsible for expanding any wildcards present in the parameters. This behaviour is different to Unix and friends, where wildcard expansion is usually done by the shell. 

A simple example demonstrates this.

Windows (Windows 7):

C:\Users\Frank>echo *
*

As you can see, the command outputs the parameter exactly as passed in by the command line.

Linux (bash on CentOS Linux 5):

> echo *
centos-release-5-0.0.el5.centos.2.i386.rpm centos-release-notes-5.0.0-2.i386.rpm glibc-2.5-12.i386.rpm glibc-common-2.5-12.i386.rpm virtualmin-install.log

Here the wild card parameter is substituted by the shell to a list of file/directories in the current directory.

So if your executable handles wildcard characters by expanding them, there is not much you can do about it. The concrete behaviour depends on your command.

If you provide more details about your command and what you want to achieve, we might be able to give some more help.

like image 156
Frank Bollack Avatar answered Sep 29 '22 15:09

Frank Bollack


Windows actually passes the entire, raw command line as a single string to a program; see GetCommandLine. When you write main(int argc, char **argv), the C runtime library that your program links with is responsible for splitting up the command line into words in your argv.

So we need more information: what is your shell, how are you invoking your command, and what C runtime library are you using? The issue you're seeing definitely isn't Windows itself, and I can't reproduce it here with cmd.exe and MSVC's CRT.

C:>type CON > test.c
#include <stdio.h>
int main(int argc, char **argv) {
    int i;
    printf("%d\n", argc);
    for (i = 0; i < argc; i++)
        printf("[%d] <%s>\n", i, argv[i]);
    return 0;
}
^Z

C:\>cl test.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

test.c
Microsoft (R) Incremental Linker Version 9.00.30729.01
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:test.exe
test.obj

C:\>test.exe *
2
[0] <test.exe>
[1] <*>

C:\>

If I use Cygwin's CRT, and run from Cygwin's Bash shell, then I don't see a problem either.

C:\>C:\cygwin\bin\bash -l
$ cd /cygdrive/c
$ cc test.c
$ ./a.exe '*'
2
[0] <./a>
[1] <*>
$

It's only if I try to mix them — run a program using Cygwin's CRT from cmd.exe — where I potentially see your problem

$ exit

C:\>a.exe *
19
[0] <a>
[1] <AUTOEXEC.BAT>
[2] <BOOT.INI>
[3] <CONFIG.SYS>
[4] <Documents and Settings>
[5] <IO.SYS>
[6] <MSDOS.SYS>
[7] <NTDETECT.COM>
[8] <NTLDR>
[9] <Program Files>
[10] <RECYCLER>
[11] <Recycled>
[12] <System Volume Information>
[13] <WINDOWS>
[14] <cygwin>
[15] <hiberfil.sys>
[16] <pagefile.sys>
[17] <temp>
[18] <a.exe>

C:\>

but I don't see a problem with double quotes.

C:\>a.exe "*"
2
[0] <test.exe>
[1] <*>

C:\>
like image 25
ephemient Avatar answered Sep 29 '22 15:09

ephemient