VC 判断目录/文件是否存在 创建目录 求目录/文件大小

原文链接:http://blog.sina.com.cn/s/blog_5f432e6a0100o0aj.html


目录是否存在检查:

 

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))
    {
       rValue = TRUE;
    }
   FindClose(hFind);
    returnrValue;
}

 

文件存在性检查:
注意,该函数是检查当前目录下是否有该文件
如果想检查其他目录下是否有该文件,则在参数中输入该文件的完整路径即可

 

BOOL FileExist(CString strFileName)
{
    CFileFindfFind;
    returnfFind.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);
}

 

 



文件大小:
DWORDGetFileSize(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);
    returnfilesize;
}

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


文件夹大小
DWORDCVCTestDlg::GetDirSize(CString strDirPath)
{
    CStringstrFilePath;
   DWORD   dwDirSize = 0;
   
    strFilePath+= strDirPath;
    strFilePath+= "\\*.*";
   
    CFileFindfinder;
    BOOL bFind =finder.FindFile(strFilePath);
    while(bFind)
    {
       bFind =finder.FindNextFile();
       if(!finder.IsDots())
       {
          CStringstrTempPath = finder.GetFilePath();
          if(!finder.IsDirectory())
          {
             dwDirSize +=finder.GetLength();
          }
          else
          {
             dwDirSize +=GetDirSize(strTempPath);
          }
       }
    }
   finder.Close();
    returndwDirSize;

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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值