文章类型: VC&C++
关键词: attributes,null,security,access,delete
内容摘要: 判断文件是否存在,判断是否为文件夹

判断文件是否存在,判断是否为文件夹

2016/12/6 10:03:08    来源:apple    阅读:

判断路径(文件或文件夹)是否存在

BOOL FileExist(CString strPath)  
{  
    WIN32_FIND_DATA wfd;  
    BOOL rValue = FALSE;  
    HANDLE hFind = FindFirstFile(strPath, &wfd);  
    if ( hFind!=INVALID_HANDLE_VALUE )  
    {  
        rValue = TRUE;  
    }  
    FindClose(hFind);  
    return rValue;  
}

 

判断路径是否为文件夹

#pragma comment(lib,"shlwapi.lib")  

#include <shlwapi.h>  

  

BOOL PathIsDirectory(strPath)  

 

 

判断文件是否存在。

 

  1._access函数,在io.h中。

  原型:int _access(const char *filename, int amode);

  参数amode(好象有5种模式)

  0:检查文件是否存在

  1:检查文件是否可运行

  2:检查文件是否可写访问

  4:检查文件是否可读访问

if ( _access(file,0) )  

{  

  //文件不存在  

}  

 

  2.CFileCFileStatus

  CFile的静态函数GetStatus如果返回FALSE表示文件不存在

 

CFileStatus fs;  

if ( !CFile::GetStatus(strFileName,fs) )  

  {  

  //文件不存在  

  }  

 

  3.CFileFind

  直接使用该类的成员函数FindFile进行判断

 

CFileFind ff;  

 if ( !ff.FindFile(strFileName) )  

 {

  //文件不存在  

 }  

  ff.Close();  

 

  3.判断文件夹是否存在

 

DirExists(sPath);  

 

VC中判断目录,文件是否存在,创建目录,求目录或文件大小的方法

目录是否存在检查:

BOOL FolderExist(CString strPath)  
{  
   WIN32_FIND_DATA wfd;  
    BOOL rValue= FALSE;  
    HANDLE hFind= FindFirstFile(strPath, &wfd);  
    if((hFind!=INVALID_HANDLE_VALUE)&&  
        (wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))  
    {//改成if (hFind!=INVALID_HANDLE_VALUE),才是判断文件或文件夹是否存在
       rValue = TRUE;  
    }  
   FindClose(hFind);  
    return rValue;  
}

 

文件存在性检查:

注意,该函数是检查当前目录下是否有该文件

如果想检查其他目录下是否有该文件,则在参数中输入该文件的完整路径即可

BOOL FileExist(CString strFileName)  
{  
    CFileFind fFind;  
    return fFind.FindFile(strFileName);  
}

 

创建目录:

BOOL CreateFolder(CString strPath)  
{  
   SECURITY_ATTRIBUTES attrib;  
   attrib.bInheritHandle = FALSE;  
   attrib.lpSecurityDescriptor = NULL;  
   attrib.nLength = sizeof(SECURITY_ATTRIBUTES);  
   //上面定义的属性可以省略  
    //直接使用return::CreateDirectory(path, NULL);即可  
    return::CreateDirectory(strPath, &attrib);  
}

 

文件大小:

DWORD GetFileSize(CString filepath)  
{  
   WIN32_FIND_DATA fileInfo;  
    HANDLEhFind;  
    DWORDfileSize;  
    CStringfilename;  
    filename =filepath;  
    hFind =FindFirstFile(filename,&fileInfo);  
    if(hFind !=INVALID_HANDLE_VALUE)  
       fileSize =fileInfo.nFileSizeLow;  
     
   FindClose(hFind);  
    return filesize;  
}

 

 

当然在CFileFind里面有GetLength()函数,也可以求得。

文件夹大小

DWORD CVCTestDlg::GetDirSize(CString strDirPath)  
{  
    CString strFilePath;  
   DWORD   dwDirSize = 0;  
     
    strFilePath+= strDirPath;  
    strFilePath+= "\\*.*";  
     
    CFileFind finder;  
    BOOL bFind =finder.FindFile(strFilePath);  
    while(bFind)  
    {  
       bFind =finder.FindNextFile();  
       if(!finder.IsDots())  
       {  
          CString strTempPath = finder.GetFilePath();  
          if(!finder.IsDirectory())  
          {  
             dwDirSize +=finder.GetLength();  
          }  
          else  
          {  
             dwDirSize +=GetDirSize(strTempPath);  
          }  
       }  
    }  
   finder.Close();  
   return dwDirSize;  
}


VC删除文件夹下所有文件的代码

//删除文件夹目录(非空)

bool DeleteDirectory(char* sDirName)   
{   
    CFileFind tempFind;   
    char sTempFileFind[200] ;  
      
    sprintf(sTempFileFind,"%s\*.*",sDirName);   
    BOOL IsFinded = tempFind.FindFile(sTempFileFind);   
    while (IsFinded)   
    {   
        IsFinded = tempFind.FindNextFile();   
          
        if (!tempFind.IsDots())   
        {   
            char sFoundFileName[200];   
            strcpy(sFoundFileName,tempFind.GetFileName().GetBuffer(200));   
              
            if (tempFind.IsDirectory())   
            {   
                char sTempDir[200];   
                sprintf(sTempDir,"%s\%s",sDirName,sFoundFileName);   
                DeleteDirectory(sTempDir);   
            }   
            else   
            {   
                char sTempFileName[200];   
                sprintf(sTempFileName,"%s\%s",sDirName,sFoundFileName);   
                DeleteFile(sTempFileName);   
            }   
        }   
    }   
    tempFind.Close();   
    if(!RemoveDirectory(sDirName))   
    {   
         return FALSE;   
    }   
    return TRUE;   
}

  


/////////////////////////////////////////

//下面是应用,CString m_strDir 是一个文件夹路径,如:d:downloadpic

BOOL DelAll()  
{  
    if(PathFileExists(m_strDir))       
        DeleteDirectory((LPSTR)(LPCTSTR)m_strDir);  
    return 1;  
}

 

 

如果不进行递归删除。你可以使用API函数SHFileOperation,它可以一次删除目录及其下面的子目录和文件。

示例代码:

BOOL DelTree(LPCTSTR lpszPath)  
{  
    SHFILEOPSTRUCT FileOp;  
     FileOp.fFlags = FOF_NOCONFIRMATION;  
    FileOp.hNameMappings = NULL;  
    FileOp.hwnd = NULL;  
     FileOp.lpszProgressTitle = NULL;  
     FileOp.pFrom = lpszPath;  
     FileOp.pTo = NULL;  
     FileOp.wFunc = FO_DELETE;  
      returnSHFileOperation(&FileOp) == 0;
}
↑ 上一篇文章:VC++中windows下的文件复制、删除、重命名操作 关键词:VC++中windows下的文件复制、删除、重命名操作 发布日期:2016/12/6 9:49:15
↓ 下一篇文章:vc 创建动态菜单及其响应函数 关键词:vc,动态菜单,菜单响应,动态菜单响应,动态按钮响应 发布日期:2016/12/6 15:59:46
相关文章:
VC动态链接数据库类ADOConn 关键词:数据库,microsoft,database,permissions,null,access 发布日期:2016-09-19 15:50
数据库表及字段命名规范 关键词:数据库表及字段命名规范,数据库,脚本,存储,insert,null,delete 发布日期:2016-09-09 15:40
vc++文件目录的删除 关键词:vc++,VC,path,null,delete,api,c,C++ 发布日期:2017-08-30 13:41
相关目录:.NETVC&C++软件开发
我要评论
正在加载评论信息......