VS2010控制台:
#include <Windows.h>
#include <stdio.h>
#include <string.h>
#include <locale.h>
void Traversal(const TCHAR *lpszPath, BOOL bRecursive)
{
WIN32_FIND_DATA ffd;
LARGE_INTEGER filesize;
HANDLE hFind = INVALID_HANDLE_VALUE;
TCHAR szCurPath[MAX_PATH]={0};
TCHAR szSearchPath[MAX_PATH]={0};
wcscat(szCurPath, lpszPath);
wcscat(szSearchPath, szCurPath);
wcscat(szSearchPath, L"\\*");
hFind = FindFirstFile(szSearchPath, &ffd);
if (INVALID_HANDLE_VALUE == hFind) {
wprintf(L"%s\t<DIR>\n", ffd.cFileName);
return;
}
do {
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
if (wcscmp(ffd.cFileName, L".") == 0 || wcscmp(ffd.cFileName, L"..") == 0) {
continue;
}
wprintf(L"%s\t<DIR>\n", ffd.cFileName);
if (bRecursive) {
TCHAR szSubDir[MAX_PATH] = {0};
wcscat(szSubDir, szCurPath);
wcscat(szSubDir, L"\\");
wcscat(szSubDir, ffd.cFileName);
Traversal(szSubDir, bRecursive);
}
} else {
filesize.LowPart = ffd.nFileSizeLow;
filesize.HighPart = ffd.nFileSizeHigh;
TCHAR szShowPath[MAX_PATH]={0};
wcscat(szShowPath, szCurPath);
wcscat(szShowPath, L"\\");
wcscat(szShowPath, ffd.cFileName);
wprintf(L"%s\t\t%ld bytes\n", szShowPath, filesize.QuadPart);
}
} while (FindNextFile(hFind, &ffd));
FindClose(hFind);
}
int main()
{
setlocale(LC_ALL, "");
TCHAR szPath[MAX_PATH] = L"F:\\movie";
Traversal(szPath, TRUE);
return 0;
}
把TRUE改为FALSE,就只是遍历一层目录。