PDA

View Full Version : How can I manually expand wildcards into filenames in C?



Poobah
June 26th, 2006, 06:41
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
June 26th, 2006, 10:08
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?

Poobah
June 26th, 2006, 10:31
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:
program *.exethen Windows would automatically expand the "*.exe" into matching file names in the current directory. If I called any of the following examples:
program "*.exe"
program ..\*.exe
program *\program.exethen 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.

Doomulation
June 26th, 2006, 10:36
If all else fails, you could use FindFirstFile/FindNextFile since they expand wildcards and returns the matched files.

NoeOM
June 26th, 2006, 11:16
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)

Poobah
June 26th, 2006, 14:09
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.


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.

smcd
June 26th, 2006, 20:25
Not tested these, but: http://code.dreamincode.net/snippet84.htm , http://www.nsftools.com/tips/CTips.htm , http://user.cs.tu-berlin.de/~schintke/references/wildcards/ , http://www.codeproject.com/string/wildcmp.asp

Doomulation
June 27th, 2006, 01:25
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.

Poobah
June 27th, 2006, 08:08
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.

Doomulation
June 27th, 2006, 14:37
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.