路径目录(即文件夹)的创建、删除及文件夹下文件的删除、复制、移动

一、创建目录文件夹

  BOOL CreateDirectory(strDirName,NULL);

二、判断目录文件夹是否已存在

  BOOL PathIsDirectory(_In_ LPCTSTR  pszPath);

  在使用此函数时可能需要导入shlwapi:

    #include <shlwapi.h>
    #pragma comment(lib, "shlwapi.lib")

三、删除空目录文件夹(仅对空文件夹有效)

  BOOL DeleteDirectory(strTempDir);

  BOOL RemoveDirectory(strTempDir);

四、删除非空目录文件夹

  1、shell方法

  2、函数自实现

    先遍历要删除的文件夹下的所有子文件和子文件夹,先删除掉子文件夹中的文件,再删除子文件及子文件夹,最后删除该文件夹

BOOL DeleteDirectory(char *DirName)
{   USES_CONVERSION;   CFileFind tempFind;   
char tempFileFind[200];   sprintf(tempFileFind,"%s\*.*",DirName);   BOOL IsFinded=(BOOL)tempFind.FindFile(A2T(tempFileFind));   while(IsFinded)   {     IsFinded=(BOOL)tempFind.FindNextFile();     if(!tempFind.IsDots())
    {       
char foundFileName[200];   
      CString dd
=tempFind.GetFileName();   
      const char* mm =T2A(dd);       //_tcscpy(mm,dd);       /*_tcscpy(mm,dd.GetBuffer());*/     strcpy(foundFileName,(const char*)mm);     if(tempFind.IsDirectory())     {         char tempDir[200];
        sprintf(tempDir,
"%s\%s",DirName,foundFileName);
        DeleteDirectory(tempDir);
      }
      else
      {         
char tempFileName[200];
        sprintf(tempFileName,
"%s\%s",DirName,foundFileName);
        DeleteFile(A2T(tempFileName));
      }
    }
  }
  tempFind.Close();   
if(!RemoveDirctory(A2T(DirName)))
  {
    MessageBox(
0,_T("删除目录失败!","警告信息"),MK_OK);
    return FALSE;
  }
  

  return
TRUE;
}

///////////////////////////测试///////////////////////////
char *DirName="C:\abc";
DeleteDirectory(DirName);

 五、将一个目录文件夹下的文件复制、移动到另一个目录文件夹

CString strNewFolderPath; 
strNewFolderPath.Format("./%s",strNewFolderName);

if(!PathIsDirectory(strNewFolderPath))  //判断目录文件夹是否已存在
{
    if(CreateDirectory(strNewFolderPath,NULL))  //创建目录文件夹
    {
        CFileFind finder;
        CString strOldPath="./src/*.*";  //原始路径目录下的文件(*.*表示所有文件)
        CString strNewPath;
        strNewPath.Format("%s/",strNewFolderPath);  //新路径目录 

        DeleteFile(strNewPath+"*.*");  //删除新路径目录下的指定文件,成功返回非0,失败返回0

        BOOL bWorking=finder.FindFile(strOldPath);
        while(bWorking)
        {
            bWorking=finder.FindNextFile();
       //将查找到的文件以相同的文件名从原始路径目录复制到新路径目录下,
       //FALSE为若新路径目录下已存在该名称文件则覆盖,成功返回非0,失败返回0
            CopyFile(finder.GetFilePath(),strNewPath+finder.GetFileName(),FALSE);
        }

        CFileFind finder02;
        CString strFileName="ZWQ.dll";
        CString strOldPath02;
        strOldPath02.Format("./%s",strFileName);  //原始路径目录下的文件ZWQ.dll
        if (finder02.FindFile(strOldPath02))
        {
            //将ZWQ.dll文件以相同的文件名从原始路径目录移动到新路径目录下
       //成功返回非0,失败返回0
       MoveFile(strOldPath02,strNewPath+strFileName);
        }
        else
        {
            CString strErrMsg02;
            strErrMsg02="当前目录下未找到ZWQ.dll文件!";
            MessageBox(strErrMsg02,"提示",MB_OK|MB_ICONWARNING);
        }
    }
}
else
{
    CString strErrMsg01;
    strErrMsg01.Format("文件夹%s已存在!",strNewFolderName);
    MessageBox(strErrMsg01,"提示",MB_OK|MB_ICONWARNING);
}

 

  

原文地址:https://www.cnblogs.com/zhouwanqiu/p/8514546.html