如何编程实现判断一个文件已经在某个目录下存在

解决方案 »

  1.   

    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
      

  2.   

    CFileFind finder;
        if (finder.FindFile(Myfile))
         {}
      

  3.   

    打开这个文件,如果成功则说明存在路径CString strPath;
    文件名CString strFileName;CString strPathName=strPath+'\\'+strFileName;
    CFile fileTest;BOOL isExisted=fileTest.Open(strPathName,CFile::modeRead);
    if (isExisted)
       fileTest.Close();
      

  4.   

    给你个思路,不过这个是寻找一个目录下总共有多少个文件的你可以很容易的修改一下就可以用!void CTestVesionDlg::OnButton15() 
    {
    // 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);
    }
      

  5.   

    u can use the function CFileFind::IsDots to judge whether file or directory u found .... 
      

  6.   

    CString path;
    if(::OpenFile(path, NULL, OF_EXIST) == HFILE_ERROR)
    {
      //文件不存在
    }
      

  7.   

    两个办法:
    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("不存在");
        }
      

  8.   

    CFileFind f;
        if(f.FindFile("d:\\xx\dd.xx"))
            MessageBox("存在");
        else
            MessageBox("不存在");
            
    具体可参考MSDN中的CFileFind类
      

  9.   

    创佳目录,包含子目录
    MakeSureDirectoryPathExists()判断目录是否存在的
    DWORD GetFileAttributes(
      LPCTSTR lpFileName   // name of file or directory
    );
      

  10.   

    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;
      

  11.   

    DWORD GetFileAttributes(
      LPCTSTR lpFileName   // name of file or directory
    );如不存在就返回0XFFFFFFFF