bool FFBreadth(TCHAR *szPath)//输入只能以C:\xx或C:\xx\*为格式,不能以‘\’作为路径结尾。详见MSDN的FindFirstFile.
{
if(_taccess(szPath,0)==-1)
{
cout<<"路径不存在"<<endl;
return false;
}
wstring wsPath(szPath);
if(wsPath.back()!=_T('*'))
wsPath=wsPath+_T("\\*");
HANDLE hd=INVALID_HANDLE_VALUE;
WIN32_FIND_DATA fd;
::memset(&fd,0,sizeof(fd));
LARGE_INTEGER li;
queue<wstring> q;
wofstream wof(_T("f:\\tra-data2.txt"));
hd = FindFirstFile(wsPath.c_str(),&fd);
if(hd == INVALID_HANDLE_VALUE)
{
cout<<"INVALID_HANDLE_VALUE"<<endl;
wof.close();
return false;
}
wstring wstemp(szPath);
do
{
if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if(fd.cFileName[0]!=_T('.'))
q.push(wstemp+_T("\\")+fd.cFileName+_T("\\*"));
}
else
{
li.HighPart = fd.nFileSizeHigh;
li.LowPart = fd.nFileSizeLow;
wcout<<fd.cFileName<<_T(" ")<<li.QuadPart<<_T("bytes")<<endl;
wof<<fd.cFileName<<_T(" ")<<li.QuadPart<<_T("bytes")<<endl;
}
}while(FindNextFile(hd,&fd));//FindNextFile正常终止时,用GetLastError可返回ERROR_NO_MORE_FILES
FindClose(hd);//不能用CloseHandle
while(!q.empty())
{
wstemp = q.front();
q.pop();
hd = FindFirstFile(wstemp.c_str(),&fd);
if(hd == INVALID_HANDLE_VALUE)
{
cout<<"返回无效句柄"<<endl;
wof.flush();
wof.close();
return false;
}
wstring wst(wstemp);
wst.pop_back();//去*
do
{
if(fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
{
if(fd.cFileName[0]!=_T('.'))
q.push(wst+fd.cFileName+_T("\\*"));
}
else
{
li.HighPart = fd.nFileSizeHigh;
li.LowPart = fd.nFileSizeLow;
wcout<<fd.cFileName<<_T(" ")<<li.QuadPart<<_T("bytes")<<endl;
wof<<fd.cFileName<<_T(" ")<<li.QuadPart<<_T("bytes")<<endl;
}
}while(FindNextFile(hd,&fd));
}
FindClose(hd);
wof.flush();
wof.close();
return true;
}