应用场合:如图片服务器,用C语言写的程序,进行图片管理。
如视频服务器,用C语言写的程序,进行视频管理,视频查询。
代码:
void FindFile(const char* pFilePath)
{
WIN32_FIND_DATA findFileData;
HANDLE hFind = INVALID_HANDLE_VALUE;
char DirSpec[MAX_PATH + 1]; //指定路径
DWORD dwError;
strncpy(DirSpec, pFilePath, strlen(pFilePath) + 1);
strncat(DirSpec, "\*", strlen("\*"));
hFind = FindFirstFile(DirSpec, &findFileData);
if(INVALID_HANDLE_VALUE == hFind)
{
cout << "Fail to findFirstFile" << GetLastError()<< endl;
return;
}
if(findFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)
{
printf("FileName:%s", findFileData.cFileName);
}
else
{
if(0 != strncmp(findFileData.cFileName, "..", strlen("..")) && (0 != strncmp(findFileData.cFileName, ".", strlen("."))))
{
cout << findFileData.cFileName << endl;
}
else
{
cout << findFileData.cFileName << endl;
}
}
while(FindNextFile(hFind, &findFileData))
{
if(findFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)
{
printf("FileName:%s\n", findFileData.cFileName);
}
else
{
if(0 != strncmp(findFileData.cFileName, "..", strlen("..")) && (0 != strncmp(findFileData.cFileName, ".", strlen("."))))
{
cout << findFileData.cFileName << endl;
}
else
{
cout << findFileData.cFileName << endl;
}
}
}
FindClose(hFind);
}