There is a really simple solution to this given by Microsoft: https://docs.microsoft.com/en-us/cpp/c-language/expanding-wildcard-arguments?view=msvc-170
Basically, you just link with one of two libraries that will expand wildcards into the argv[] list fed to your program.
To clarify by example, say you have the code:
// ExampleFileExpansion.cpp : This outputs the cmd line arguments
//
#include <iostream>
#include <tchar.h>
using namespace std;
int wmain(int argc, _TCHAR* argv[]) {
for (int i = 0; i < argc; i++) {
wcout << i << "\t" << argv[i] << endl;
}
}
Which normally gives output like:
C:\x64\Debug>ExampleFileExpansion *.*
0 ExampleFileExpansion
1 *.*
Within Visual Studio, navigate to the project properties page and choose the linker input options | Additional Dependencies. Then, add the library (in this case "wsetargv.obj") and apply the change. Then, Build the project again, from the main menu.
After the change, running the same code gives something like:
C:\x64\Debug>ExampleFileExpansion *.*
0 ExampleFileExpansion
1 ExampleF.a660785c.tlog
2 ExampleFileExpansion.exe
3 ExampleFileExpansion.exe.recipe
4 ExampleFileExpansion.ilk
5 ExampleFileExpansion.log
6 ExampleFileExpansion.obj
7 ExampleFileExpansion.pdb
8 ExampleFileExpansion.vcxproj.FileListAbsolute.txt
9 vc143.idb
10 vc143.pdb
Here are some screen shots from VS 2022 to illustrate the steps:
Visual Studio Project Properties Menu
Choose Linker input options, then Edit to get the dialog
Enter the appropriate obj file name either wsetargv.obj or setargv.obj depending on whether wmain or main function is declared
I suspect the answer will be no, but has anyone dealt with this or come up with any way to make this easier?
Actually, I’m having the opposite problem, I am trying to figure out a way to get the command-interpreter to treat its list as strings and prevent it from inreptreting them as wildcards. For example,for %i in (foobar baz really?) do @echo %i
will treat the last item (really?
) as a filename wildcard, and skip it if there are no files namedreally1
,reallyz
, etc. ☹?
is not a legal character for filenames in the Windows filesystem. The character can only be interpreted as a wildcard. See Naming Files, Paths, and Namespaces on MSDN and Using wildcard characters in TechNet.