EDN Admin
Well-known member
I am not sure I am detecting files and directories correctly. I want to detect files (but not sparse files), and directories, even hidden ones. I am trying to get a system file and dir count. any help appreciated. I think I have this correct,
but no matter what I try, it crashes at level 16 (it goes as deep as level 18 or 19).
older attempts I have made at walking a dir tree where I have immediately recursed on finding a directory have failed miserably due to the way XP works.
<div style="color:black; background-color:white
<pre>uint64_t dircount=0, filecount=0;
<span style="color:green //no copyright notice given, but I am putting one here nonetheless.
<span style="color:green //function Copyright 2000 Michael D. Ober, modified by Jim Michaels
<span style="color:green //this function is for Windows, not for DOS.
<span style="color:blue void walkDirTree(<span style="color:blue const <span style="color:blue char *root, <span style="color:blue const <span style="color:blue char * filespec, uint64_t level) { <span style="color:green //windows
HANDLE hRoot;
WIN32_FIND_DATA fdRoot;
<span style="color:blue char fRoot[MAX_PATH+1];
<span style="color:blue char branch[MAX_PATH+1];
list<string> dirs;
list<string>::iterator it;
<span style="color:blue if (0 == STRCMP(filespec, <span style="color:#a31515 "..") || 0 == STRCMP(filespec, <span style="color:#a31515 ".")) {
printf(<span style="color:#a31515 "invalid filespec: %sn", filespec);
<span style="color:green //problem with filespec!
<span style="color:blue return;
}
<span style="color:blue if (level >= 16) {
cout<<<span style="color:#a31515 "{level="<<level<<<span style="color:#a31515 "}";
}
<span style="color:green //---------------------------------------------
<span style="color:green // now, find all specified files and dirs in the "root" specified directory.
strcpy(fRoot, root);
appendBackslashIfNone(fRoot);
strcat(fRoot, filespec);
#<span style="color:blue if defined(DEBUG)
printf(<span style="color:#a31515 "n");
#endif
hRoot = FindFirstFile(fRoot, &fdRoot);
<span style="color:blue if (INVALID_HANDLE_VALUE != hRoot) {
<span style="color:blue do {
<span style="color:green //get data from structure
strcpy(branch, root);
appendBackslashIfNone(branch);
strcat(branch, fdRoot.cFileName);
<span style="color:blue if (<span style="color:green //dir
fdRoot.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
) {
<span style="color:blue if (!(0 == STRCMP(fdRoot.cFileName, <span style="color:#a31515 ".") || 0 == STRCMP(fdRoot.cFileName, <span style="color:#a31515 ".."))) {
<span style="color:green // real subdir found. add the subdir to the list.
string s=branch;
dirs.push_back(s);
<span style="color:blue if (dirs.size()> 100000U) {
cout<<dirs.size()<<<span style="color:#a31515 ",";
}
dircount++;
}
} <span style="color:blue else <span style="color:blue if ( <span style="color:green //file
!(fdRoot.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
&& fdRoot.dwFileAttributes & FILE_ATTRIBUTE_DEVICE
&& fdRoot.dwFileAttributes & FILE_ATTRIBUTE_VIRTUAL
) &&
(fdRoot.dwFileAttributes & FILE_ATTRIBUTE_NORMAL
|| fdRoot.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY
|| fdRoot.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN
|| fdRoot.dwFileAttributes & FILE_ATTRIBUTE_READONLY
|| fdRoot.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE
<span style="color:green /*|| fdRoot.dwFileAttributes & FILE_ATTRIBUTE_ */
)
) {
<span style="color:blue if ((STRCMP(fdRoot.cFileName, <span style="color:#a31515 ".") != 0) && (STRCMP(fdRoot.cFileName, <span style="color:#a31515 "..") != 0)) {
filecount++;
}
}
} <span style="color:blue while (0 != FindNextFile(hRoot, &fdRoot));
FindClose(hRoot);
}
<span style="color:green //--------------------------------------------------------
<span style="color:green //process the directories we found. recurse into them.
<span style="color:blue for (it = dirs.begin(); it != dirs.end(); it++) {
walkDirTree(it->c_str(), filespec, level+1);
}
}
[/code]
<br/>
<br/>
View the full article
but no matter what I try, it crashes at level 16 (it goes as deep as level 18 or 19).
older attempts I have made at walking a dir tree where I have immediately recursed on finding a directory have failed miserably due to the way XP works.
<div style="color:black; background-color:white
<pre>uint64_t dircount=0, filecount=0;
<span style="color:green //no copyright notice given, but I am putting one here nonetheless.
<span style="color:green //function Copyright 2000 Michael D. Ober, modified by Jim Michaels
<span style="color:green //this function is for Windows, not for DOS.
<span style="color:blue void walkDirTree(<span style="color:blue const <span style="color:blue char *root, <span style="color:blue const <span style="color:blue char * filespec, uint64_t level) { <span style="color:green //windows
HANDLE hRoot;
WIN32_FIND_DATA fdRoot;
<span style="color:blue char fRoot[MAX_PATH+1];
<span style="color:blue char branch[MAX_PATH+1];
list<string> dirs;
list<string>::iterator it;
<span style="color:blue if (0 == STRCMP(filespec, <span style="color:#a31515 "..") || 0 == STRCMP(filespec, <span style="color:#a31515 ".")) {
printf(<span style="color:#a31515 "invalid filespec: %sn", filespec);
<span style="color:green //problem with filespec!
<span style="color:blue return;
}
<span style="color:blue if (level >= 16) {
cout<<<span style="color:#a31515 "{level="<<level<<<span style="color:#a31515 "}";
}
<span style="color:green //---------------------------------------------
<span style="color:green // now, find all specified files and dirs in the "root" specified directory.
strcpy(fRoot, root);
appendBackslashIfNone(fRoot);
strcat(fRoot, filespec);
#<span style="color:blue if defined(DEBUG)
printf(<span style="color:#a31515 "n");
#endif
hRoot = FindFirstFile(fRoot, &fdRoot);
<span style="color:blue if (INVALID_HANDLE_VALUE != hRoot) {
<span style="color:blue do {
<span style="color:green //get data from structure
strcpy(branch, root);
appendBackslashIfNone(branch);
strcat(branch, fdRoot.cFileName);
<span style="color:blue if (<span style="color:green //dir
fdRoot.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
) {
<span style="color:blue if (!(0 == STRCMP(fdRoot.cFileName, <span style="color:#a31515 ".") || 0 == STRCMP(fdRoot.cFileName, <span style="color:#a31515 ".."))) {
<span style="color:green // real subdir found. add the subdir to the list.
string s=branch;
dirs.push_back(s);
<span style="color:blue if (dirs.size()> 100000U) {
cout<<dirs.size()<<<span style="color:#a31515 ",";
}
dircount++;
}
} <span style="color:blue else <span style="color:blue if ( <span style="color:green //file
!(fdRoot.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
&& fdRoot.dwFileAttributes & FILE_ATTRIBUTE_DEVICE
&& fdRoot.dwFileAttributes & FILE_ATTRIBUTE_VIRTUAL
) &&
(fdRoot.dwFileAttributes & FILE_ATTRIBUTE_NORMAL
|| fdRoot.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY
|| fdRoot.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN
|| fdRoot.dwFileAttributes & FILE_ATTRIBUTE_READONLY
|| fdRoot.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE
<span style="color:green /*|| fdRoot.dwFileAttributes & FILE_ATTRIBUTE_ */
)
) {
<span style="color:blue if ((STRCMP(fdRoot.cFileName, <span style="color:#a31515 ".") != 0) && (STRCMP(fdRoot.cFileName, <span style="color:#a31515 "..") != 0)) {
filecount++;
}
}
} <span style="color:blue while (0 != FindNextFile(hRoot, &fdRoot));
FindClose(hRoot);
}
<span style="color:green //--------------------------------------------------------
<span style="color:green //process the directories we found. recurse into them.
<span style="color:blue for (it = dirs.begin(); it != dirs.end(); it++) {
walkDirTree(it->c_str(), filespec, level+1);
}
}
[/code]
<br/>
<br/>
View the full article