Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 347
  • Last Modified:

Listing files


I have a need to retrieve a list of files matching a search regular expression. The platform is either Visual C++ 6.0 on win32 or gcc 3.3 on Solaris 8. I understand that the solution may differ between the platforms.

Currently I'm using the system() function call to call "dir /B searchpattern > tmp.dir" or "ls -1 searchpattern > tmp.ls" and then read and delete the resulting file (e.g.: "dir /B ..\wnp*.log > tmp.dir"). However, I'm not at all satisfied with:
a: having to create a temporary file;
b: having to perform file access to get my system question anwered.

Does there exist some other mean, maybe even something like:

GetFileList(pattern, &resultContainer);

Regards,
/Bronk
0
bronkbronk
Asked:
bronkbronk
2 Solutions
 
AlexFMCommented:
In windows you can use the following functions:

FindFirstFile
FindNextFile
FindClose

0
 
nonubikCommented:
0
 
stefan73Commented:
Hi bronkbronk,
> I understand that the solution may differ between the platforms.
Depends. If you use cygwin (http://www.cygwin.com), you'll have executables on Windows which will (almost) completely use the same source. You can use gcc, awk, sed, bash... and tons of other tools.

Regarding the Solaris/cygwin solution for the directory, this would be done with opendir():

#include <sys/types.h>
#include <dirent.h>

int main(){
    DIR* dir;
    struct dirent *curr_ent;

    dir=opendir("your_dir");
    if(!dir) [error...]

    while(curr_ent=readdir(dir)){
        printf("Offset: %lld Inode: %ld File Name: %s\n",curr_ent->d_off,curr_ent->d_ino,curr_ent->d_name);
    }

    closedir(dir);
}


Cheers,
Stefan
0
 
akalmaniCommented:
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now