Hi i am writing an application where it has to get the list of all the files and folders under a given path...
Searching files i did with FindFirstFile and FindNextFile functions and is fine ....
But the problem is with searching the folders...
Can anyone tell me how to search for folders under a given path...
Are there any functions like i am using for 'file search' to get
the folder list...
Plz help...
I use VC++ 6.0 Compiler
C code help !!! Search for Folders in a given path !!!?
Try this
void FindFile(String* startingDir, String* fileName, ArrayList* foundFiles)
{
DirectoryInfo* dirInfo = new DirectoryInfo(startingDir);
if (dirInfo-%26gt;Exists) // make sure directory exists
{
FileInfo* files[] = dirInfo-%26gt;GetFiles();
for (int i = 0; i %26lt; files-%26gt;Length; i++)
{
String* currFileName = files[i]-%26gt;ToString();
if (0 == String::Compare(currFileName, fileName, true))
{
foundFiles-%26gt;Add(files[i]);
}
}
DirectoryInfo* subDirs[] = dirInfo-%26gt;GetDirectories();
for (int i = 0; i %26lt; subDirs-%26gt;Length; i++)
{
String* dirName = subDirs[i]-%26gt;FullName;
FindFile(dirName, fileName, foundFiles);
}
}
}
Reply:If you want/need to stick with C and not C++, see the example here:
http://www.siit.tu.ac.th/mdailey/class/2...
To only look at directories, in a portable way, you have to call stat() on each result and check with the S_ISDIR() macro e.g.:
char *fullpath = malloc(NAME_MAX+ 1);
while (NULL != (dirent = readdir(dir))) {
struct stat buf;
fullpath[0] = '\0';
if (wherelength + strlen(dirent-%26gt;d_name) + 1 %26gt; NAME_MAX) {
fprintf(stderr, "Path too long\n");
return 3;
}
strcpy(fullpath, where);
strcat(fullpath, "/");
strcat(fullpath, dirent-%26gt;d_name);
if(stat(fullpath, %26amp;buf)) {
fprintf(stderr, "stat: %d (%s)\n", errno, strerror(errno));
continue;
}
if (S_ISDIR(buf.st_mode)) {
printf("DIR: %s\n", dirent-%26gt;d_name);
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment