CFileFind::FindFile virtual BOOL FindFile( LPCTSTR pstrName = NULL, DWORD dwUnused = 0 );Return ValueNonzero if successful; otherwise 0. To get extended error information, call the Win32 functionGetLastError.ParameterspstrNameA pointer to a string containing the name of the file to find. If you pass NULL for pstrName, FindFile does a wildcard (*.*) search.dwUnusedReserved to make FindFile polymorphic with derived classes. Must be 0.ResCall this member function to open a file search.After calling FindFile to begin the file search, call FindNextFile to retrieve subsequent files. You must call FindNextFile at least once before calling any of the following attribute member functions: GetCreationTime GetFileName GetFileTitle GetFilePath GetFileURL GetLastAccessTime GetLastWriteTime GetLength GetRoot CFileFind Overview | Class Members | Hierarchy ChartSee Also CFileFind::FindNextFile
创佳目录,包含子目录 MakeSureDirectoryPathExists()判断目录是否存在的 DWORD GetFileAttributes( LPCTSTR lpFileName // name of file or directory );
int _access( const char *path, int mode );Each of these functions returns 0 if the file has the given mode. The function returns –1 if the named file does not exist or is not accessible in the given mode;
DWORD GetFileAttributes( LPCTSTR lpFileName // name of file or directory );如不存在就返回0XFFFFFFFF
virtual BOOL FindFile( LPCTSTR pstrName = NULL, DWORD dwUnused = 0 );Return ValueNonzero if successful; otherwise 0. To get extended error information, call the Win32 functionGetLastError.ParameterspstrNameA pointer to a string containing the name of the file to find. If you pass NULL for pstrName, FindFile does a wildcard (*.*) search.dwUnusedReserved to make FindFile polymorphic with derived classes. Must be 0.ResCall this member function to open a file search.After calling FindFile to begin the file search, call FindNextFile to retrieve subsequent files. You must call FindNextFile at least once before calling any of the following attribute member functions: GetCreationTime
GetFileName
GetFileTitle
GetFilePath
GetFileURL
GetLastAccessTime
GetLastWriteTime
GetLength
GetRoot
CFileFind Overview | Class Members | Hierarchy ChartSee Also CFileFind::FindNextFile
if (finder.FindFile(Myfile))
{}
文件名CString strFileName;CString strPathName=strPath+'\\'+strFileName;
CFile fileTest;BOOL isExisted=fileTest.Open(strPathName,CFile::modeRead);
if (isExisted)
fileTest.Close();
{
// TODO: Add your control notification handler code here
char szFilename[80];
strFilePath = "";
strcpy(szFilename,"新建 文本文档.txt");
_chdir("d:\\");
Search_Directory(szFilename);
// MessageBox("");
CString strCount;
strCount.Format("查找文件完毕\n发现%d个文件\n查找的文件为:%s",count_File,strFilePath);
AfxMessageBox(strCount);
}void CTestVesionDlg::Search_Directory(char *szFilename)
{
long handle;
struct _finddata_t filestruct;
char path_search[_MAX_PATH];
handle = _findfirst("*", &filestruct);
if((handle == -1)) return; if( ::GetFileAttributes(filestruct.name) & FILE_ATTRIBUTE_DIRECTORY )
{
if( filestruct.name[0] != '.' )
{
_chdir(filestruct.name);
Search_Directory(szFilename);
_chdir("..");
}
}
else
{
count_File++;
if( !stricmp(filestruct.name, szFilename) )
{
_getcwd(path_search,_MAX_PATH);
strcat(path_search,"\\");
strcat(path_search,filestruct.name);
strFilePath = path_search;
//MessageBox(path_search);
}
} while(!(_findnext(handle,&filestruct)))
{
if( ::GetFileAttributes(filestruct.name) & FILE_ATTRIBUTE_DIRECTORY )
{
if(*filestruct.name != '.')
{
_chdir(filestruct.name);
Search_Directory(szFilename);
_chdir("..");
}
}
else
{
count_File++;
if(!stricmp(filestruct.name,szFilename))
{
_getcwd(path_search,_MAX_PATH);
strcat(path_search,"\\");
strcat(path_search,filestruct.name);
strFilePath = path_search;
//MessageBox(path_search);
}
}
}
_findclose(handle);
}
if(::OpenFile(path, NULL, OF_EXIST) == HFILE_ERROR)
{
//文件不存在
}
1、
CFileFind f;
if(f.FindFile("c:\\temp"))
AfxMessageBox("存在");
//Directory exist.
else
AfxMessageBox("不存在");
//Not exist.2、
if(_access("c:\\temp", 0) == 0){
AfxMessageBox("存在");
}
else{
AfxMessageBox("不存在");
}
if(f.FindFile("d:\\xx\dd.xx"))
MessageBox("存在");
else
MessageBox("不存在");
具体可参考MSDN中的CFileFind类
MakeSureDirectoryPathExists()判断目录是否存在的
DWORD GetFileAttributes(
LPCTSTR lpFileName // name of file or directory
);
LPCTSTR lpFileName // name of file or directory
);如不存在就返回0XFFFFFFFF