简体   繁体   English

C ++通过命令行使用*字符传递多个文件

[英]C++ Passing multiple files using * character via command line

Under linux, I do the following 在linux下,我执行以下操作

COMMAND /home/directory/*.txt 命令/home/directory/*.txt

and all the files in that directory get passed as separate parameters (20 files in the directory results in 20 parameters in the argv variable) 并且该目录中的所有文件都作为单独的参数传递(目录中的20个文件在argv变量中产生20个参数)

Under windows, the same command results in one parameter (that string exactly). 在Windows下,相同的命令将产生一个参数(即该字符串)。

Is this a compiler issue (VisualC++ 2008) or a windows thing or what? 这是编译器问题(VisualC ++ 2008)还是Windows问题?

In the past, I've written batch files to parse the files into multiple parameters, but I'm hoping there's a better way. 过去,我编写了批处理文件以将文件解析为多个参数,但我希望有更好的方法。

Any help would be appreciated 任何帮助,将不胜感激

It's somewhat more limited than most Unix shells, but VC++ includes a file named setargv.obj that you can link with to add globbing to your application. 它比大多数Unix shell受到的限制更多,但是VC ++包含一个名为setargv.obj的文件,您可以链接该文件以向应用程序添加glob。 It supports * and ? 它支持*? , which covers most of what most people care about. ,其中涵盖了大多数人关心的大部分内容。

To use it, just include setargv.obj when you link your file. 要使用它,只需在链接文件时包含setargv.obj In most cases, this just means adding the file name to the command line, something like this: 在大多数情况下,这仅意味着将文件名添加到命令行,如下所示:

cl myfile.c myotherfile.c setargv.obj

Indeed that is a shell globbing feature. 确实,这是一个外壳程序的功能。 In PowerShell you would handle wildcard expansion inside your function using Convert-Path (or Resolve-Path) eg: 在PowerShell中,您可以使用Convert-Path (或Resolve-Path)在函数内部处理通配符扩展,例如:

function ITakeWildcards([string]$Path) {
    $paths = Convert-Path $path
    foreach ($aPath in $paths) {
        "Processing path $aPath"
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM