#include <tchar.h>
#include <Windows.h>
#include <string>
#include <strsafe.h>#define BUFSIZE 4096
HANDLE hFile;
WIN32_FIND_DATA Filedata;TCHAR * Recurse(TCHAR* strDir);
BOOL FindFile(std::wstring strDir);int _tmain()
{
Recurse(_T("D:\\Torque\\TGEA_1_7_1\\engine\\"));
return 0;
}TCHAR * Recurse(TCHAR* strDir)
{
TCHAR strPathName[BUFSIZE];
TCHAR strTempPath[BUFSIZE];
TCHAR strTemp[256];
ZeroMemory(strPathName,sizeof(TCHAR)*BUFSIZE);
ZeroMemory(strTempPath,sizeof(TCHAR)*BUFSIZE);
ZeroMemory(strTemp,sizeof(TCHAR)*256);
ZeroMemory(&Filedata,sizeof(Filedata));
HRESULT hr;
BOOL bSearchFile = FALSE;
BOOL bFinished = FALSE;
wcscpy(strPathName,strDir);
if (strPathName[wcslen(strPathName)-1] != _T('\\'))
{
wcscat(strPathName,_T("\\"));
}
wcscat(strPathName,_T("*"));
bSearchFile = FindFile(strPathName);
if ((Filedata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && wcscmp(Filedata.cFileName,_T(".")) && wcscmp(Filedata.cFileName,_T("..")))
{
wcscpy(strTempPath,strDir);
wcscat(strTempPath,Filedata.cFileName);
wprintf(_T(" %s\n"),Filedata.cFileName);
Recurse(strTempPath);
}
else if (wcscmp(Filedata.cFileName,_T(".")) && wcscmp(Filedata.cFileName,_T("..")))
{
wprintf(_T(" %s\n"),Filedata.cFileName);
}
while (!bFinished)
{
if (FindNextFile(hFile,&Filedata))
{
if ((Filedata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && wcscmp(Filedata.cFileName,_T(".")) && wcscmp(Filedata.cFileName,_T("..")))
{
wcscpy(strTempPath,strDir);
wcscat(strTempPath,Filedata.cFileName);
wcscat(strTempPath,_T("\\"));
wprintf(_T(" %s\n"),Filedata.cFileName);
Recurse(strTempPath);
}
else if (wcscmp(Filedata.cFileName,_T(".")) && wcscmp(Filedata.cFileName,_T("..")))
{
wprintf(_T(" %s\n"),Filedata.cFileName);
}
}
else
{
if( GetLastError() == ERROR_NO_MORE_FILES )
{
bFinished = TRUE;
}
else
{
bFinished = TRUE;
}
}
}
//FindClose(hFile);
return strTempPath;
}BOOL FindFile(std::wstring strDir)
{
hFile = FindFirstFile(strDir.c_str(),&Filedata);
if (hFile == INVALID_HANDLE_VALUE)
{
printf ("Invalid File Handle. GetLastError reports %d\n",
GetLastError ());
return FALSE;
}
else
{
return TRUE;
}
}结果是只遍历了当前文件下的一个目录的所有列表,请问问题在哪?谢谢
解决方案 »
- CHtmlView 浏览器如何默认最大化
- 请教:GetProcessIoCounters的问题
- 局域网里面的ftp搜索引擎开发探讨
- 在ACCESS数据库中,大于2147483647(2的31次方),小于4294967296(2的32次方)的整数该用什么数据类型啊?
- 请问sFileData.SpanExcluding("\r\n")是何意思?
- 建立socket后 收完信息就死掉了 怎么办?
- 如何用区位码输入?
- 请高手指教,tcpip间的数据库访问?急急急。万分感谢!
- 启动一个新的进程,怎么取得该进程的identifier of the thread?
- 外壳组件如何注册
- wince中有Pow这个函数么,为什么没有智能感应呢
- MFC进度条与微调器用法举例
wcscat(strTempPath,TEXT("\\")); // <-加上这个
wcscat(strTempPath,Filedata.cFileName);
另有一个问题隐患:数组长度4096比较大,不宜用局部数组,特别是在递归函数中,容易引起栈溢出,应用new分配,函数返回前delete。
if ((Filedata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && wcscmp(Filedata.cFileName,_T(".")) && wcscmp(Filedata.cFileName,_T("..")))
{
wcscpy(strTempPath,strDir);
wcscat(strTempPath,_T("\\")); //加上这句
wcscat(strTempPath,Filedata.cFileName);
wcscat(strTempPath,_T("\\"));
wprintf(_T(" %s\n"),Filedata.cFileName);
Recurse(strTempPath);
}
#include <Windows.h>#define BUFSIZE 4096VOID Recurse(TCHAR* strDir);int _tmain()
{
Recurse(_T("D:\\Torque\\TGEA_1_7_1\\engine\\"));
return 0;
}VOID Recurse(TCHAR* strDir)
{
HANDLE hFile;
WIN32_FIND_DATA Filedata;
TCHAR *strPathName = new TCHAR[4096];
TCHAR *strTempPath = new TCHAR[4096];
ZeroMemory(&Filedata,sizeof(Filedata));
BOOL bSearchFile = FALSE;
BOOL bFinished = FALSE; wcscpy(strPathName,strDir);
if (strPathName[wcslen(strPathName)-1] != _T('\\'))
{
wcscat(strPathName,_T("\\"));
}
wcscat(strPathName,_T("*.*"));
bSearchFile = (INVALID_HANDLE_VALUE !=(hFile = FindFirstFile(strPathName,&Filedata)));
if (bSearchFile)
{
if (wcscmp(Filedata.cFileName,_T(".")) && wcscmp(Filedata.cFileName,_T("..")))
{
wprintf(_T(" %s\n"),Filedata.cFileName);
}
while (!bFinished)
{
if (FindNextFile(hFile,&Filedata))
{
if ((Filedata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && wcscmp(Filedata.cFileName,_T(".")) && wcscmp(Filedata.cFileName,_T("..")))
{
wcscpy(strTempPath,strDir);
wcscat(strTempPath,Filedata.cFileName);
wcscat(strTempPath,_T("\\"));
wprintf(_T(" %s\n"),Filedata.cFileName);
Recurse(strTempPath);
}
else if (wcscmp(Filedata.cFileName,_T(".")) && wcscmp(Filedata.cFileName,_T("..")))
{
wprintf(_T(" %s\n"),Filedata.cFileName);
}
}
else
{
if( GetLastError() == ERROR_NO_MORE_FILES )
{
bFinished = TRUE;
}
else
{
bFinished = TRUE;
}
}
}
}
else
{
wprintf (_T("Invalid File Handle. GetLastError reports %d\n"),
GetLastError ());
}
delete []strPathName;
delete []strTempPath;
FindClose(hFile);
}
OK?
#include <tchar.h>
#include <Windows.h>#define BUFSIZE 4096VOID Recurse(TCHAR* strDir);int _tmain()
{
Recurse(_T("D:\\Torque\\TGEA_1_7_1\\engine\\"));
return 0;
}VOID Recurse(TCHAR* strDir)
{
HANDLE hFile;
WIN32_FIND_DATA Filedata;
TCHAR *strPathName = new TCHAR[4096];
TCHAR *strTempPath = new TCHAR[4096];
ZeroMemory(&Filedata,sizeof(Filedata));
BOOL bSearchFile = FALSE;
BOOL bFinished = FALSE; wcscpy(strPathName,strDir);
if (strPathName[wcslen(strPathName)-1] != _T('\\'))
{
wcscat(strPathName,_T("\\"));
}
wcscat(strPathName,_T("*.*"));
bSearchFile = (INVALID_HANDLE_VALUE !=(hFile = FindFirstFile(strPathName,&Filedata)));
if (bSearchFile)
{
if (wcscmp(Filedata.cFileName,_T(".")) && wcscmp(Filedata.cFileName,_T("..")))
{
wprintf(_T(" %s\n"),Filedata.cFileName);
}
while (!bFinished)
{
if (FindNextFile(hFile,&Filedata))
{
if ((Filedata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && wcscmp(Filedata.cFileName,_T(".")) && wcscmp(Filedata.cFileName,_T("..")))
{
wcscpy(strTempPath,strDir);
wcscat(strTempPath,Filedata.cFileName);
wcscat(strTempPath,_T("\\"));
wprintf(_T(" %s\n"),Filedata.cFileName);
Recurse(strTempPath);
}
else if (wcscmp(Filedata.cFileName,_T(".")) && wcscmp(Filedata.cFileName,_T("..")))
{
wprintf(_T(" %s\n"),Filedata.cFileName);
}
}
else
{
if( GetLastError() == ERROR_NO_MORE_FILES )
{
bFinished = TRUE;
}
else
{
bFinished = TRUE;
}
}
}
}
else
{
wprintf (_T("Invalid File Handle. GetLastError reports %d\n"),
GetLastError ());
}
delete []strPathName;
delete []strTempPath;
FindClose(hFile);
}
#include <Windows.h>
#include <vector>
#include <string>#define BUFSIZE 4096
#define LINEMAX 125
#define FILE_VERSION _T("1.1")struct USER_DEFINE_FIND_DATA : public WIN32_FIND_DATA
{
std::wstring strFilePath;
std::wstring strFileVersin;
USER_DEFINE_FIND_DATA();
};USER_DEFINE_FIND_DATA::USER_DEFINE_FIND_DATA():
strFileVersin(FILE_VERSION)
{}
std::vector<std::wstring> vFileList;
std::vector<std::wstring> vDirectoryList;
std::vector<USER_DEFINE_FIND_DATA> vFileInfo;VOID Recurse(TCHAR* strDir);
BOOL WinWriteFile(HANDLE hHandle,LPCTSTR strBuffer,DWORD dwNumberOfBytesToWrite = LINEMAX,DWORD dwNumberOfBytesWritten = 0);int _tmain()
{
//Recurse(_T("D:\\Torque\\TGEA_1_7_1\\engine\\"));
Recurse(_T(".\\"));
return 0;
}VOID Recurse(TCHAR* strDir)
{
HANDLE hFindFile = INVALID_HANDLE_VALUE;
HANDLE hFileUpdateTxt = INVALID_HANDLE_VALUE;
USER_DEFINE_FIND_DATA Filedata;
ZeroMemory(&Filedata,sizeof(Filedata));
TCHAR *strPathName = new TCHAR[BUFSIZE];
TCHAR *strTempPath = new TCHAR[BUFSIZE];
BOOL bSearchFile = FALSE;
BOOL bFinished = FALSE;
DWORD dwByteWrite = 0;
hFileUpdateTxt = CreateFile(_T("c:\\update.txt"),GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if (INVALID_HANDLE_VALUE == hFileUpdateTxt)
{
wprintf(_T("CreateFile failed at %d"),GetLastError());
}
wcscpy(strPathName,strDir);
if (strPathName[wcslen(strPathName)-1] != _T('\\'))
{
wcscat(strPathName,_T("\\"));
}
wcscat(strPathName,_T("*.*"));
bSearchFile = (INVALID_HANDLE_VALUE !=(hFindFile = FindFirstFile(strPathName,&Filedata)));
if (bSearchFile)
{
if (wcscmp(Filedata.cFileName,_T(".")) && wcscmp(Filedata.cFileName,_T("..")))
{
wprintf(_T(" %s\n"),Filedata.cFileName);
}
while (!bFinished)
{
if (FindNextFile(hFindFile,&Filedata))
{
if ((Filedata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && wcscmp(Filedata.cFileName,_T(".")) && wcscmp(Filedata.cFileName,_T("..")))
{
wcscpy(strTempPath,strDir);
wcscat(strTempPath,Filedata.cFileName);
wcscat(strTempPath,_T("\\"));
wprintf(_T(" %s\n"),Filedata.cFileName);
vFileInfo.push_back(Filedata); WriteFile(hFileUpdateTxt,Filedata.cFileName,LINEMAX,&dwByteWrite,NULL);
Recurse(strTempPath);
}
else if (wcscmp(Filedata.cFileName,_T(".")) && wcscmp(Filedata.cFileName,_T("..")))
{
wprintf(_T(" %s\n"),Filedata.cFileName);
vFileInfo.push_back(Filedata);
WriteFile(hFileUpdateTxt,Filedata.cFileName,LINEMAX,&dwByteWrite,NULL);
}
}
else
{
if( GetLastError() == ERROR_NO_MORE_FILES )
{
bFinished = TRUE;
}
else
{
bFinished = TRUE;
}
}
}
}
else
{
wprintf (_T("Invalid File Handle. GetLastError reports %d\n"),
GetLastError ());
}
delete []strPathName;
delete []strTempPath;
FindClose(hFindFile);
}BOOL WinWriteFile(HANDLE hHandle,LPCTSTR strBuffer,DWORD dwNumberOfBytesToWrite,DWORD dwNumberOfBytesWritten )
{ if (!WriteFile(hHandle,strBuffer,dwNumberOfBytesToWrite,&dwNumberOfBytesToWrite,NULL))
{
wprintf (_T("WriteFile failed at %d\n"),GetLastError());
return FALSE;
}
return TRUE;
}