VC编程小技巧之文件操作

1.删除目录

// 删除目录及其所有内容
void CBaseDoc::RemoveFolder(const CString &strPathName)
{
    CString path = strPathName;
    if (path.Right(1) != _T("\\"))
        path += _T("\\");
    path += _T("*.*");

    CFileFind ff;
    BOOL res = ff.FindFile(path);
    while (res)
    {
        res = ff.FindNextFile();
        // 是文件时直接删除
        if (!ff.IsDots() && !ff.IsDirectory())
            DeleteFile(ff.GetFilePath());
        else if (ff.IsDots())
            continue;
        else if (ff.IsDirectory())
        {
            path = ff.GetFilePath();
            // 是目录时继续递归,删除该目录下的文件
            RemoveFolder(path);
            ::RemoveDirectory(path);
        }
    }
}

 

2.遍历整个目录查找文件

在应用程式的研发过程中,会遇见怎么查找某一文件以确定此文件路径的问题。利用CFileFind类能比较方便地在当前目录下进行文件查找,但却不能对其子目录中的文件进行搜寻。而实际应用中往往需要对某一整个目录树,甚至是整个C盘或D盘驱动器进行文件搜寻。通过实践,我们在Visual C++ 6.0中编程实现了怎么遍历任意目录树,以查找某一特定的文件。
  在下面的具体陈述中能看到,在确定要查找的文件名和要进行搜索的目录的名称后,将调用函数Search_Directory进行文件的查找。首先依次查找当前目录下的每一个实体(文件或是子目录),如果是某一子目录,则进入该子目录并递归调用函数Search_Dirctory进行查找,查找完毕之后, 再返回上一级目录;如果不是子目录而是某一文件,则判断其是否就是我们要查找的文件,如果是则输出其完整的文件路径。这样,通过Search_Directory函数的反复递归调用,就能实现对整个目录,包括子目录的遍历搜索。下面将举例周详讲述怎么在VC++中编程实目前整个目录树中的文件查找。
  1. 在Visual C++ 6.0VC++ 5.0和之类似)中用默认方式创建了一基于对话框的应用程式Search。在主窗口对话框上放置一命令按钮,其Caption“Search File”IDID_BUTTON_SEARCH。单击此按钮将完成文件的查找工作。
  2. 利用ClassWizard“Search File”按钮的BN_CLICKED 事件添加处理函数OnButtonSearch,代码如下:

include direct.h
include io.h
void CSearchDlg::OnButtonSearch()
{
   // TODO: Add your control notification handler code here
  
   char szFilename[80];
   // 字符串 szFilename 表示要查找的文件名

   strcpy(szFilename,"Mytext.txt");

   _chdir("d:\\"); // 进入要查找的路径(也可为某一具体的目录)
   // 查找文件, 如果查到则显示文件的路径全名
   Search_Directory(szFilename);
   // CSearchDlg类的一成员函数
   MessageBox(″查找文件完毕!″);
   // 显示查找完毕的信息
}


  3. CSearchDlg类中增加成员函数Search_Directory,他将完成具体的文件查找工作,代码如下:
void CSearchDlg::Search_Directory(char* szFilename)
{
   long handle;
   struct _finddata_t filestruct;
   //表示文件(或目录)的信息
   char path_search[_MAX_PATH];
   //表示查找到的路径结果
   // 开始查找工作, 找到当前目录下的第一个实体(文件或子目录)
   // "*"表示查找所有的文件或子目录, filestruct为查找结果
   handle = _findfirst("*", &filestruct);
   // 如果handle为-1, 表示当前目录为空, 则结束查找而返回
   if((handle == 1)) return;
   // 检查找到的第一个实体是否是个目录(filestruct.name为其名称)
   if( ::GetFileAttributes(filestruct.name) & FILE_ATTRIBUTE_DIRECTORY )
   {
      // 如果是目录, 则进入该目录并递归调用函数Search_Dirctory进行查找,
      // 注意: 如果目录名的首字符为’.’(即为"."".."), 则不用进行查找
      if( filestruct.name[0] != ’.’ )
      {
         _chdir(filestruct.name);
         Search_Directory(szFilename);
         // 查找完毕之后, 返回上一级目录
         _chdir("..");
      }
   }
   else // 如果第一个实体不是目录, 则检查是否是要查找的文件
   {
      // stricmp对两字符串进行小写形式的对比, 返回为0表示完全一致
      if( !stricmp(filestruct.name, szFilename) )
      {
         // 先获得当前工作目录的全路径
         _getcwd(path_search,_MAX_PATH);
         // 再获得文件的完整的路径名(包含文件的名称)
         strcat(path_search,"\\");
         strcat(path_search,filestruct.name);
         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
      {
         if(!stricmp(filestruct.name,szFilename))
         {
            _getcwd(path_search,_MAX_PATH);
            strcat(path_search,"//");
            strcat(path_search,filestruct.name);
            MessageBox(path_search);
         }
      }
   }
   _findclose(handle);
   // 最后结束整个查找工作
}
  这样我们就可以对整个目录进行遍历搜索,查找某一特定的文件,并输出显示其完整的文件路径。以上的程序在Visual C++ 6.0中已调试通过。

 

3.创建多级目录

BOOL mkdirEx(const char* lpPath)
{
CString pathname = lpPath;
if(pathname.Right(1) != "/")
pathname += "/" ;
int end = pathname.ReverseFind('/');
int pt = pathname.Find('/');
if (pathname[pt-1] == ':')
pt = pathname.Find('/', pt+1);
CString path;
while(pt != -1 && pt<=end)
{
path = pathname.Left(pt+1);
if(_access(path, 0) == -1)
_mkdir(path);
pt = pathname.Find('/', pt+1);
}
return true;
}

 

4.创建包含多个子目录的目录

void CreateAllDirectories(CString strDir)
{
//remove ending / if exists
if(strDir.Right(1)=="//")
  strDir=strDir.Left(strDir.GetLength()-1);

// base case . . .if directory exists
if(GetFileAttributes(strDir)!=-1)
  return;

// recursive call, one less directory
int nFound = strDir.ReverseFind('//');
CreateAllDirectories(strDir.Left(nFound));

// actual work
CreateDirectory(strDir,NULL);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值