It fails only at the system volumine.
#ifndef UNICODE
#define UNICODE
#endif
#include <stdio.h>
#include <Windows.h>
void EndWithBackslash(TCHAR* string)
{
if(string[wcslen(string)-1] != TEXT('\\')) wcscat(string,TEXT("\\"));
}
void Browse(const TCHAR* curdir)
{
HANDLE hFoundFile;
WIN32_FIND_DATA foundFileData;
TCHAR buffer[MAX_PATH];
wcscpy(buffer,curdir);
EndWithBackslash(buffer);
SetCurrentDirectory(buffer);
hFoundFile = FindFirstFileEx(TEXT("*"),FINDEX_INFO_LEVELS::FindExInfoBasic,&foundFileData ,FINDEX_SEARCH_OPS::FindExSearchLimitToDirectories ,NULL , NULL);
if(hFoundFile != INVALID_HANDLE_VALUE)
{
do
{
if ((foundFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && wcscmp(foundFileData.cFileName,TEXT(".")) && wcscmp(foundFileData.cFileName,TEXT("..")) )
{
TCHAR pszItemPath[MAX_PATH];
wcscpy(pszItemPath, buffer);
EndWithBackslash(pszItemPath);
wcscat(pszItemPath,foundFileData.cFileName);
wprintf(TEXT("%s\n"),pszItemPath);
Browse(pszItemPath);
}
}
while(FindNextFile(hFoundFile,&foundFileData));
}
FindClose(hFoundFile);
}
int main(void)
{
_wsystem(TEXT("chcp 65001"));
Browse(TEXT("C:\\"));
_wsystem(TEXT("Pause"));
return 0;
}
The last lines of output, just before run-time error:
C:\$Recycle.Bin\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5
\S-1-5-20
C:\$Recycle.Bin\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5
\S-1-5-20
C:\$Recycle.Bin\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5-20\S-1-5
\S-1-5-20
Is there any elegant way to fix it? I would like to avoid deleting this pseudo-directory(Super Users have explained it very well) or setting name filters inside my algorithm.
Te crash is caused by a stack overflow error because the
SetCurrentDirectoryfailed to change the current directory. As Roman R. stated, you’ll need to check the result ofSetCurrentDirectory. Moreover, the.and..subdirectories shouldn’t be processed as they refer to the current directory, and the parent directory. Including them in the process would also cause a stack overflow error.Here’s the fixed
Browsefunction.