Question:
c++ Explore Directory and Sub-directories?
Meola_Attack
2010-09-10 13:01:57 UTC
Hi all, guys, I need help with a program in c++. I need to explore a directory and all its sub-directories. My problem is the following one: when I search a file, the results contain files that don't exist, like "FOUND.00" , that compromise the good outcome... I tried to exclude them with this algorithm "if(strstr("FOUND.",buffer)!=0)..."but I don't obtain any result. Can you help me? I post here my algorithm:

void ExplorePC(char * directory, FILE * file)
{
WIN32_FIND_DATA data;
HANDLE hFind;
char slash[]="\\\\"; // directory + slash + point = directory\\*.*
char point[]="*.*";
char string[]="\\\\*.*";
strcat(directory,string);
hFind = FindFirstFile(directory,&data);
if(hFind == INVALID_HANDLE_VALUE)
{
if(strstr(string,directory)==0)
{
int characters = strlen(directory)-strlen(string);
directory[characters]='\0';
}
fprintf(file,"Impossible to explore the directory \"%s\".",directory);
return;
}
else
{
if(strstr(string,directory)==0)
{
int characters = strlen(directory)-strlen(string);
directory[characters]='\0';
}
fprintf(file,"The files that have been found in \"%s\" are:\n",directory);
strcat(directory,slash);// Remove \\\\*.* from directory to printf the original directory in the file. Then add slash to directory that is ready to be pasted to the new files.
while(FindNextFile(hFind,&data)!=0)
{
char buffer[MAX_PATH];
strcpy(buffer,data.cFileName);
if(GetFileAttributes(data.cFileName) & FILE_ATTRIBUTE_DIRECTORY)
{

if(strstr("FOUND.",buffer)!=0)
{
fprintf(file,"%s\n",buffer);
strcat(directory,buffer);
cout<Sleep(700);
ExplorePC(directory,file);
}
}

}
}
return;
}

Ten points...Sorry for my bed English, I'm Italian
Five answers:
husoski
2010-09-10 13:43:19 UTC
Those "FOUND." files do exist...but are flagged as "hidden" so you don't normally see them when viewing files in the Windows Explorer. Your code would work if you switched the order of the strstr() arguments. The call is strstr(haystack, needle) if you are looking for a needle in a haystack.



But, there's an easier way.



To skip hidden files yourself, just like Explorer does by default, you need to look at the attributes returned by FindFirstFile and FindNextFile. By the way, you don't need to call GetFileAttributes(). The attributes are in data.dwFileAttributes already. So, to test for directories, hidden files, system files, use



if (data.dwAttributes & FILE_ATTRIBUTE_DIRECTORY)

{ do directory stuff }



if (data.dwAttributes & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM))

{ skip over hidden/system files }
Ralph 124c41
2010-09-10 13:09:37 UTC
Some (most) operating systems do not delete a directory entry when the file is deleted - they just flag the entry as deleted and/or available. You need to not search deleted branches of the directory tree.

So check that no inappropriate bits of the file attributes are set.
2016-10-05 14:27:45 UTC
in the beginning, whilst a record is deleted on a working laptop or workstation it ought to or won't nevertheless be on the troublesome tension. whilst data are written to the troublesome tension, the information is written there besides as a checklist the place the record is placed. think of of once you bypass to an workplace in a huge construction. you recognize the guy's call yet you haven't any longer any theory what floor or workplace selection, so which you look it up interior the construction listing to ascertain. workstation data on the troublesome tension and the thank you to define them is in a listing (grasp record table, as an occasion). Now once you delete a record, that is taken out of the MFT, yet data continues to be there without tips that ought to it. So, that is there yet no longer there. Sophiscated courses can test the troublesome tension without applying the MFT and discover data that hasn't been overwriten via yet another software. See, whilst the policies have been deleted, the area the place your record grew to become into is obtainable, and the subsequent saved data will in all risk overwrite (reuse) that area. additionally, courses that create content cloth (be conscious, photograph progs, etc) leave tracks in each single place on your workstation, that an expert can in all risk discover.
n4vjot
2010-09-10 13:10:16 UTC
That is very hard to read, try using codepad.com and selecting c++ , and post a link to your codepad file.
frayser
2010-09-10 13:09:39 UTC
char *strstr(const char *haystack, const char *needle);



if(strstr("FOUND.",buffer)!=0) => if (sttrstr(buffer, "FOUND)



reversed!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...