文件和目录

#include
#include
#include
#include 
//
//判断目录或文件是否存在
bool IsFileDirExist(string path)
{
 if(_access(path.c_str(),0)!=-1)
 {
  //cout<<path.c_str()<<" exist!"<<endl;
  return true;
 }
 else
  return false;
}



//说明:
//[1] windows下 参数为 "" 或 / 获得的目录是当前的盘符
//[2] 绝对路径中的/可以多个重复,windows识别为一个/ ;
//    如: DeleteFile("F:/here/file.txt");等同于DeleteFile("F://here//file.txt");
//[3] 该函数删除指定目录下的所有文件,不递归删除子文件夹的内容

bool DeleteFileInDir(string rootDir)
{  
 if (strcmp(rootDir.c_str(),"")==0
  ||strcmp(rootDir.c_str(),"//")==0) {
  cout<<"非法路径!"<<endl;
  return false;
 }
    WIN32_FIND_DATA fd;
    ZeroMemory(&fd, sizeof(WIN32_FIND_DATA)); 
 
    HANDLE hSearch; 
    string filePathName; 
    string tmpPath;  
 
    filePathName=rootDir;
 
    BOOL bSearchFinished = FALSE;
 
    if( filePathName.data()[filePathName.length()-1] != '//' )  
    {  
  filePathName+="//"; 
    }  
 
    filePathName+="*";
 
    hSearch = FindFirstFile(filePathName.c_str(), &fd);

 if (hSearch==INVALID_HANDLE_VALUE) {
  cout<<"Directory or file name is invalid!"<<endl;
  return false;
 }
 
    while( !bSearchFinished )  
    {  
  if( FindNextFile(hSearch, &fd) )   
  {   
   if( (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)    
    && strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") )     
   { 
    tmpPath = rootDir;
    tmpPath+=fd.cFileName;
    cout<<tmpPath.c_str()<< " is a directory! Not deleted."<<endl;
   }    
   else if( strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") )    
   {
    tmpPath = rootDir;
    if (tmpPath.data()[tmpPath.length()-1]!='//') {
     tmpPath+="//";
    }
    tmpPath+=fd.cFileName;
    if (hSearch==INVALID_HANDLE_VALUE) {
     cout<<"Directory or file name is invalid!"<<endl;
     return false;
    }    
    if(DeleteFile(tmpPath.c_str()))
    {
     cout<<"File "<<tmpPath.c_str()<<" is deleted!"<<endl;
    }
    else
    {
     cout<<"File "<<tmpPath.c_str()<<" is not deleted!"<<endl;
     return false;
    }
   }   
  }  
  else   
  {   
   if( GetLastError() == ERROR_NO_MORE_FILES )          //Normal Finished    
   {    
    bSearchFinished = TRUE;    
   }
   else    
    bSearchFinished = TRUE;     //Terminate Search   
  }  
    } 
    FindClose(hSearch);
 return true;
}



//说明:
//[1] windows下 参数为 "" 或 / 获得的目录是当前的盘符
//[2] 绝对路径中的/可以多个重复,windows识别为一个/ ;
//    如: DeleteFile("F:/here/file.txt");等同于DeleteFile("F://here//file.txt");
//[3] 该函数删除指定目录下的所有文件,并递归删除子文件夹的内容

bool DeleteFileDir(string rootDir)
{  
 if (strcmp(rootDir.c_str(),"")==0
  ||strcmp(rootDir.c_str(),"//")==0) {
  cout<<"非法路径!"<<endl;
  return false;
 }
    WIN32_FIND_DATA fd;
    ZeroMemory(&fd, sizeof(WIN32_FIND_DATA)); 
 
    HANDLE hSearch; 
    string filePathName; 
    string tmpPath;  
 
    filePathName=rootDir;
 
    BOOL bSearchFinished = FALSE;
 
    if( filePathName.data()[filePathName.length()-1] != '//' )  
    {  
  filePathName+="//"; 
    }  
 
    filePathName+="*";
 
    hSearch = FindFirstFile(filePathName.c_str(), &fd);
 
 if (hSearch==INVALID_HANDLE_VALUE) {
  cout<<"Directory or file name is invalid!"<<endl;
  return false;
 }
 
    while( !bSearchFinished )  
    {  
  if( FindNextFile(hSearch, &fd) )   
  {   
   if( (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)    
    && strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") )     
   { 
    tmpPath = rootDir;
    tmpPath+=fd.cFileName;
    //cout<<tmpPath.c_str()<< " is a directory! Not deleted."<<endl;
    if(!DeleteFileDir(tmpPath))
    {
     cout<<"Delete "<<tmpPath.c_str()<<" failed!"<<endl;
     return false;
    }
    if(rmdir(tmpPath.c_str())==0)
    {
     cout<<"Directory "<<tmpPath.c_str()<<" is deleted!"<<endl;
    }
   }    
   else if( strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") )    
   {
    tmpPath = rootDir;
    if (tmpPath.data()[tmpPath.length()-1]!='//') {
     tmpPath+="//";
    }
    tmpPath+=fd.cFileName;
    if (hSearch==INVALID_HANDLE_VALUE) {
     cout<<"Directory or file name is invalid!"<<endl;
     return false;
    }    
    if(DeleteFile(tmpPath.c_str()))
    {
     cout<<"File "<<tmpPath.c_str()<<" is deleted!"<<endl;
    }
    else
    {
     cout<<"File "<<tmpPath.c_str()<<" is not deleted!"<<endl;
     return false;
    }
   }   
  }  
  else   
  {   
   if( GetLastError() == ERROR_NO_MORE_FILES )          //Normal Finished    
   {    
    bSearchFinished = TRUE;    
   }
   else    
    bSearchFinished = TRUE;     //Terminate Search   
  }  
    } 
    FindClose(hSearch);
 return true;
}


//说明:
//[1] windows下 参数为 "" 或 / 获得的目录是当前的盘符
//[2] 绝对路径中的/可以多个重复,windows识别为一个/ ;
//    如: CopyFile("F:/here/file.txt","I:");等同于CopyFile("F://here//file.txt","I://");
//[3] 该函数拷贝指定目录下的所有文件到指定目标目录, 不递归拷贝子文件夹的内容
//[4] fileType : *.* , *.txt , *.doc 等等形式

bool CopyFileInDir(string rootDir,string destDir,string fileType)
{
 if (strcmp(rootDir.c_str(),"")==0
  ||strcmp(rootDir.c_str(),"//")==0) {
  cout<<"非法源路径!"<<endl;
  return false;
 }

 if (strcmp(destDir.c_str(),"")==0
  ||strcmp(destDir.c_str(),"//")==0) {
  cout<<"非法目标路径!"<<endl;
  return false;
 }
 
 if (!IsFileDirExist(destDir)) {
  CreateDirectory(destDir.c_str(),NULL);
 }
    
    WIN32_FIND_DATA fd; 
    ZeroMemory(&fd, sizeof(WIN32_FIND_DATA)); 
 
    HANDLE hSearch; 
    string filePathName; 
    string tmpPath; 
 string tmpDestPath;
     
    filePathName=rootDir; 
 
    BOOL bSearchFinished = FALSE; 
 
    if( filePathName.data()[filePathName.length()-1] != '//' )  
    {  
  filePathName+="//";
    }
 
    filePathName+= fileType; 
    hSearch = FindFirstFile(filePathName.c_str(), &fd);

 if (hSearch==INVALID_HANDLE_VALUE) {
  cout<<"无效文件名"<<endl;
  return false;
 }
 
    while( !bSearchFinished )  
    {  
  if( FindNextFile(hSearch, &fd) )   
  {   
   if( (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)    
    && strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") )     
   {    
    tmpPath = rootDir;
    if( tmpPath.data()[tmpPath.length() -1] != '//' )  
    {  
     tmpPath+="//";  
    }
    tmpPath+= fd.cFileName;
    cout<<tmpPath.c_str()<<" is a directory!"<<endl;    
   }    
   else if( strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") )    
   {    
    tmpPath = rootDir;
    if( tmpPath.data()[tmpPath.length() -1] != '//' )  
    {  
     tmpPath+="//";  
    }
    tmpPath+= fd.cFileName;    
    tmpDestPath = destDir; 
    if( tmpDestPath.data()[tmpDestPath.length() -1] != '//' )  
    {  
     tmpDestPath+= "//";  
    }   
    tmpDestPath+=fd.cFileName;    
    if(CopyFile(tmpPath.c_str(),tmpDestPath.c_str(),FALSE))
     cout<<"Copy file From "<<tmpPath.c_str()<<" To "
     <<tmpDestPath.c_str()<<" successfully!"<<endl;
    else
     return false;
   }   
  }  
  else   
  {   
   if( GetLastError() == ERROR_NO_MORE_FILES )          //Normal Finished    
   {    
    bSearchFinished = TRUE;    
   }
   else    
    bSearchFinished = TRUE;     //Terminate Search   
  }  
    } 
    FindClose(hSearch); 
 return true;
}


//说明:
//[1] windows下 参数为 "" 或 / 获得的目录是当前的盘符
//[2] 绝对路径中的/可以多个重复,windows识别为一个/ ;
//    如: CopyFile("F:/here/file.txt","I:");等同于CopyFile("F://here//file.txt","I://");
//[3] 该函数拷贝指定目录下的所有文件,并递归拷贝子文件夹的内容到指定目标目录
//[4] fileType : *.* , *.txt , *.doc 等等形式

bool CopyFileDir(string rootDir,string destDir,string fileType)
{
 if (strcmp(rootDir.c_str(),"")==0
  ||strcmp(rootDir.c_str(),"//")==0) {
  cout<<"非法源路径!"<<endl;
  return false;
 }

 if (strcmp(destDir.c_str(),"")==0
  ||strcmp(destDir.c_str(),"//")==0) {
  cout<<"非法目标路径!"<<endl;
  return false;
 }
 if (!IsFileDirExist(destDir)) {
  if(CreateDirectory(destDir.c_str(),NULL))
  {
   cout<<"Create Directory "<<destDir.c_str()<<endl;
  }
 } 
    
    WIN32_FIND_DATA fd; 
    ZeroMemory(&fd, sizeof(WIN32_FIND_DATA)); 
 
    HANDLE hSearch; 
    string filePathName; 
    string tmpPath; 
 string tmpDestPath;
     
    filePathName=rootDir; 
 
    BOOL bSearchFinished = FALSE; 
 
    if( filePathName.data()[filePathName.length()-1] != '//' )  
    {  
  filePathName+="//";
    }
 
    filePathName+= fileType; 
    hSearch = FindFirstFile(filePathName.c_str(), &fd);

 if (hSearch==INVALID_HANDLE_VALUE) {
  cout<<"无效文件名"<<endl;
  return false;
 }
 
    while( !bSearchFinished )  
    {  
  if( FindNextFile(hSearch, &fd) )   
  {   
   if( (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)    
    && strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") )     
   {    
    tmpPath = rootDir;
    if( tmpPath.data()[tmpPath.length() -1] != '//' )  
    {  
     tmpPath+="//";  
    }
    tmpPath+= fd.cFileName;    
    tmpDestPath = destDir; 
    if( tmpDestPath.data()[tmpDestPath.length() -1] != '//' )  
    {  
     tmpDestPath+= "//";  
    }   
    tmpDestPath+=fd.cFileName;        
    if(!CopyFileDir(tmpPath,tmpDestPath,fileType))
    {
     return false;
    }
   }    
   else if( strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") )    
   {    
    tmpPath = rootDir;
    if( tmpPath.data()[tmpPath.length() -1] != '//' )  
    {  
     tmpPath+="//";  
    }
    tmpPath+= fd.cFileName;    
    tmpDestPath = destDir; 
    if( tmpDestPath.data()[tmpDestPath.length() -1] != '//' )  
    {  
     tmpDestPath+= "//";  
    }   
    tmpDestPath+=fd.cFileName;    
    if(CopyFile(tmpPath.c_str(),tmpDestPath.c_str(),FALSE))
     cout<<"Copy file From "<<tmpPath.c_str()<<" To "
     <<tmpDestPath.c_str()<<" successfully!"<<endl;
    else
     return false;
   }   
  }  
  else   
  {   
   if( GetLastError() == ERROR_NO_MORE_FILES )          //Normal Finished    
   {    
    bSearchFinished = TRUE;    
   }
   else    
    bSearchFinished = TRUE;     //Terminate Search   
  }  
    } 
    FindClose(hSearch); 
 return true;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值