What's new

How can I manually expand wildcards into filenames in C?

Poobah

New member
I've realised the flaws of Windows' wildcard expansion, and am now wondering if there is an easy and convenient way of expanding wildcards into filenames in C, using a GCC-based compiler such as MinGW. Also, is there a switch or something that can be used when compiling a program that prevents the OS from doing its own wildcard expansion on that particular program?
 

NoeOM

Mankind Member
Please, be more specific

Hi,

Please, be more specific when asking. For the wildcards in C, i suggest the use of the strcmp function, for instance. If you are looking for files with the prototype fil*, then you can check the return value of a call to

strcmp("fil",fileName)

where fileName is the name of the file you are checking.
It's only a suggestion since I don't know what data types are you using, what kind of functionality do you want, etc...

And what do you mean by disable wildcard expansion for a program?
 
OP
Poobah

Poobah

New member
NoeOM said:
Hi,

Please, be more specific when asking. For the wildcards in C, i suggest the use of the strcmp function, for instance. If you are looking for files with the prototype fil*, then you can check the return value of a call to

strcmp("fil",fileName)

where fileName is the name of the file you are checking.
It's only a suggestion since I don't know what data types are you using, what kind of functionality do you want, etc...

And what do you mean by disable wildcard expansion for a program?
In Windows, if I called:
Code:
program *.exe
then Windows would automatically expand the "*.exe" into matching file names in the current directory. If I called any of the following examples:
Code:
program "*.exe"
Code:
program ..\*.exe
Code:
program *\program.exe
then Windows would completely ignore the wildcard. I think, however, that it is possible to disable Windows' expanding of wildcards passed in command-line arguments.

strcmp() doesn't consider two strings beginning with the same text equal, by the way.

What I'm mainly looking for is an easy and convenient way to manually expand wildcards to matching file names, since Windows' wildcard expanding is extremely limited. Glibc appears to have something like this, which it calls "globbing", but the library is quite bloated.
 
Last edited:

Doomulation

?????????????????????????
If all else fails, you could use FindFirstFile/FindNextFile since they expand wildcards and returns the matched files.
 

NoeOM

Mankind Member
Revision

Sorry, I was talking about strstr, not strcmp (my mistake).

And for the command line arguments expansion... I think that this is a task performed by the OS and not dependant on your code so, I don't think there is any option to disable it (in the code of your program, maybe the OS can be configured)
 
OP
Poobah

Poobah

New member
I just spent ages working out how to use glob() and finding the appropriate libraries, and now it's turned out that GnuWin32 hasn't even ported that particular function yet, despite the fact that the Glob.h file is still included with their libgw32c package.

Doomulation said:
If all else fails, you could use FindFirstFile/FindNextFile since they expand wildcards and returns the matched files.
Could you explain how to use those in C? I don't know a thing about Windows programming.
EDIT: Never mind that last question; I've worked it out.

EDIT 2: Is it just me, or does the WIN32_FIND_DATA structure only contain the file's name, and not its path? I understand that I can work around this by adding a path extracted from the original prototype to the file name, but all sorts of horrible things will happen if wildcards are in places other than the file name.
 
Last edited:

Doomulation

?????????????????????????
Poobah said:
EDIT 2: Is it just me, or does the WIN32_FIND_DATA structure only contain the file's name, and not its path? I understand that I can work around this by adding a path extracted from the original prototype to the file name, but all sorts of horrible things will happen if wildcards are in places other than the file name.
It works on a directory level. So searching for "C:\*" returns all files (and folders) in C. To make it recursive, just check for directories and search them as well.
 
OP
Poobah

Poobah

New member
Doomulation said:
It works on a directory level. So searching for "C:\*" returns all files (and folders) in C. To make it recursive, just check for directories and search them as well.
Yeah, I understand that, but that's not really what I meant. I asked the above question, because I noticed that the cFileName string in the WIN32_FIND_DATA structure always contained only a filename, even if the pattern contained a path, such as "C:\*". If I sent the file to fopen(), it wouldn't work unless the current directory was C:\.

It doesn't matter now, though, because I've written my own glob() function using FindFirstFile/FindNextFile, adding a directory extracted from the pattern to each returned file name. Thanks for the help, everyone.
 
Last edited:

Doomulation

?????????????????????????
Poobah said:
Yeah, I understand that, but that's not really what I meant. I asked the above question, because I noticed that the cFileName string in the WIN32_FIND_DATA structure always contained only a filename, even if the pattern contained a path, such as "C:\*". If I sent the file to fopen(), it wouldn't work unless the current directory was C:\.

It doesn't matter now, though, because I've written my own glob() function using FindFirstFile/FindNextFile, adding a directory extracted from the pattern to each returned file name. Thanks for the help, everyone.
Easily currected, however. Yes, it returns only the filenames of the directory where you expand wildcarrds. However, you can use _chdir to change the current directory to where you are willing to expand them.

Anyway, good thing you've managed to solve it.
 

Top