#include
#include
using namespace std;
#ifdef UNICODE
#undef UNICODE
#endif
#include
BOOL MyFindFile(LPCSTR sFindPath, LPCSTR sFindFileName, ULONGLONG &uCountFolder, ULONGLONG &uCountFile)
{
char sPath[MAX_PATH];
char sFormatFileName[MAX_PATH+2] = "*";
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
BOOL fFinished = FALSE;
lstrcpy(sFormatFileName, sFindPath);///该函数复制一个字符串到缓冲区
lstrcat(sFormatFileName, "//*");///该函数将一个字符串附加在另一个字符串后面
lstrcat(sFormatFileName, sFindFileName);
lstrcat(sFormatFileName, "*");
hFind = FindFirstFile(sFormatFileName, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
return FALSE;
}
else
{
while (!fFinished)
{
lstrcpy(sPath, sFindPath);
lstrcat(sPath, "//");
lstrcat(sPath, FindFileData.cFileName);
if (FILE_ATTRIBUTE_DIRECTORY & FindFileData.dwFileAttributes)
{
if (0 != lstrcmp(FindFileData.cFileName,".") && 0 != lstrcmp(FindFileData.cFileName,".."))
cout << " Folder " << ++uCountFolder << ". - " << sPath <<""<< endl;
}
else
cout << " File " << ++uCountFile << ". - " << sPath <<""<< endl;
if (!FindNextFile(hFind, &FindFileData))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
{
fFinished = TRUE;
}
else
{
break;
}
}
}
FindClose(hFind);
}
return TRUE;
}
BOOL MyFindFolder(LPCSTR sPath, LPCSTR sFindFileName, ULONGLONG &uCountFolder, ULONGLONG &uCountFile)
{
char sTemp[MAX_PATH];
char sFormatFileName[MAX_PATH];
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
BOOL fFinished = FALSE;
MyFindFile(sPath, sFindFileName, uCountFolder, uCountFile);
lstrcpy(sFormatFileName, sPath);
lstrcat(sFormatFileName, "//*");
hFind = FindFirstFile(sFormatFileName, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
return FALSE;
}
else
{
while (!fFinished)
{
if (FILE_ATTRIBUTE_DIRECTORY & FindFileData.dwFileAttributes)
{
if (0 != lstrcmp(FindFileData.cFileName,".") && 0 != lstrcmp(FindFileData.cFileName,".."))
{
lstrcpy(sTemp, sPath);
lstrcat(sTemp, "//");
lstrcat(sTemp, FindFileData.cFileName);
MyFindFolder(sTemp, sFindFileName, uCountFolder, uCountFile);
}
}
if (!FindNextFile(hFind, &FindFileData))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
{
return TRUE;
}
else
{
return FALSE;
}
}
}
FindClose(hFind);
}
return TRUE;
}
void GetFileName(LPCSTR sFullPath, LPSTR sFilePath, LPSTR sFileName)
{
LPSTR p = (LPSTR)(sFullPath + lstrlen(sFullPath) - 1);
bool Flag(false);
while (p != sFullPath)
{
if ('//' == *p || '/' == *p)
{
Flag = true;
break;
}
p--;
}
if (Flag)
{
lstrcpy(sFileName, p + 1);
lstrcpy(sFilePath, sFullPath);
sFilePath[p-sFullPath] = '/0';
}
else
{
lstrcpy(sFileName, sFullPath);
GetFullPathName(".", MAX_PATH, sFilePath, NULL);///从一指定文件得到文件的路径
}
}
ULONGLONG MyStartFind(LPCSTR sFindFileName)
{
char sPath[MAX_PATH];
char sFileName[MAX_PATH];
ULONGLONG uCountFolder(0);
ULONGLONG uCountFile(0);
GetFileName(sFindFileName, sPath, sFileName);
MyFindFolder(sPath, sFileName, uCountFolder, uCountFile);
if (uCountFolder + uCountFile)
{
cout << "------------------------------------------------------------"<< endl;
cout << "Total Folders:"<< uCountFolder << "" << endl;
cout << "Total Files: "<< uCountFile << "" << endl;
}
else
cout << "Couldn't Find File." << endl;
return uCountFolder + uCountFile;
}
int main(int argc, char *argv[])
{
char sFindFileName[MAX_PATH];
if (argc < 2)
{
cout << "Enter Find File Name:";
cin >> sFindFileName;
}
else
{
lstrcpy(sFindFileName, argv[1]);
}
MyStartFind(sFindFileName);
#ifdef _DEBUG
system("pause");
#else
if (argc < 2)
system("pause");
#endif
return (0);
}