【程序】
// ExploreFolder.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <Windows.h>
#define echo(str) WriteConsole(hConsoleOutput, str, _tcslen(str), NULL, NULL)
#define echo_t(str) echo(TEXT(str))
HANDLE hConsoleOutput;
BOOL explore(LPTSTR szFolder, UINT indent = 0)
{
TCHAR szFileName[MAX_PATH];
TCHAR szNextFolder[MAX_PATH];
_tcscpy_s(szFileName, szFolder);
_tcscat_s(szFileName, TEXT("\\*"));
WIN32_FIND_DATA ffd;
HANDLE hFindFile = FindFirstFile(szFileName, &ffd);
if (hFindFile == INVALID_HANDLE_VALUE)
return FALSE;
UINT i;
do
{
// 忽略.和..
if (ffd.cFileName[0] == '.')
continue;
// 缩进
i = indent;
while (i--)
echo_t("--");
// 输出文件或文件夹的名称
echo(ffd.cFileName);
echo_t("\r\n");
// 递归遍历子文件夹
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
_tcscpy_s(szNextFolder, szFolder);
_tcscat_s(szNextFolder, TEXT("\\"));
_tcscat_s(szNextFolder, ffd.cFileName);
explore(szNextFolder, indent + 1);
}
} while (FindNextFile(hFindFile, &ffd));
FindClose(hFindFile);
return TRUE;
}
int _tmain(int argc, _TCHAR* argv[])
{
hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
explore(TEXT("C:\\Program Files\\Windows Media Player"));
return 0;
}