CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VC/MFC >  基础类

如何删除一个文件夹及文件夹下所有文件?

楼主ligeleng0()2005-09-28 09:01:43 在 VC/MFC / 基础类 提问

我要删除一个文件夹及文件夹下所有文件,该怎么删?  
  remove函数好像只能删除文件,不能删除文件夹的,  
  多谢 问题点数:20、回复次数:6Top

1 楼phoenix96_2000(Arcrest)回复于 2005-09-28 09:02:54 得分 5

http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=210476  
   
  http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=193461Top

2 楼titilima(李马 - www.titilima.cn)回复于 2005-09-28 09:03:11 得分 5

//////////////////////////////////////////////////////////////////////////  
  //   DelTree  
  //   删除一个文件夹  
  //   lpszPath   -   要删除的文件夹路径  
  //   返回值:成功返回TRUE,否则返回FALSE  
  //   备注:亦可用来删除单个文件  
  //////////////////////////////////////////////////////////////////////////  
  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;  
          return   SHFileOperation(   &FileOp   )   ==   0;  
  }Top

3 楼laiyiling(陌生人[MVP])回复于 2005-09-28 09:04:44 得分 5

删除非空目录更简单  
  SHFILEOPSTRUCT   shfileop;  
  shfileop.hwnd   =   NULL;  
  shfileop.wFunc   =   FO_DELETE   ;  
  shfileop.fFlags   =   FOF_SILENT   |   FOF_NOCONFIRMATION;  
  shfileop.pFrom   =   "c:\\Download1";  
  shfileop.pTo   =   "";  
  shfileop.lpszProgressTitle   =   "";  
  shfileop.fAnyOperationsAborted   =   TRUE;  
  int   nOK   =   SHFileOperation(&shfileop);  
   
  -------------------------------  
   
  BOOL   DeleteDirectory(LPCTSTR   DirName)  
  {  
          CFileFind   tempFind;   //声明一个CFileFind类变量,以用来搜索  
          char   tempFileFind[200];   //用于定义搜索格式  
          sprintf(tempFileFind,"%s\\*.*",DirName);  
          //匹配格式为*.*,即该目录下的所有文件    
           
          BOOL   IsFinded=(BOOL)tempFind.FindFile(tempFileFind);  
          //查找第一个文件  
          while(IsFinded)  
          {  
                  IsFinded=(BOOL)tempFind.FindNextFile();   //递归搜索其他的文件    
          if(!tempFind.IsDots())   //如果不是"."目录  
          {  
                  char   foundFileName[200];  
                  strcpy(foundFileName,tempFind.GetFileName().GetBuffer(200));  
                  if(tempFind.IsDirectory())   //如果是目录,则递归地调用  
                  {   //DeleteDirectory  
                  char   tempDir[200];  
                  sprintf(tempDir,"%s\\%s",DirName,foundFileName);  
                  DeleteDirectory(tempDir);  
                  }  
                  else  
                  {   //如果是文件则直接删除之  
                          char   tempFileName[200];  
                          sprintf(tempFileName,"%s\\%s",DirName,foundFileName);  
                          DeleteFile(tempFileName);  
                  }  
          }  
          }  
          tempFind.Close();  
          if(!RemoveDirectory(DirName))   //删除目录  
          {  
                  AfxMessageBox("删除目录失败!",MB_OK);  
                  return   FALSE;  
          }  
          return   TRUE;  
  }Top

4 楼gunney(楚州才子)回复于 2005-09-28 09:20:24 得分 5

int   SHFileOperation(                     LPSHFILEOPSTRUCT   lpFileOp  
  );  
  Parameters  
   
  lpFileOp  
  [in]   Pointer   to   an   SHFILEOPSTRUCT   structure   that   contains   information   this   function   needs   to   carry   out   the   specified   operation.   This   parameter   must   contain   a   valid   value   that   is   not   NULL.   You   are   responsibile   for   validating   the   value.   If   you   do   not   validate   it,   you   will   experience   unexpected   results.  
  Return   Value  
   
  Syntax  
   
  typedef   struct   _SHFILEOPSTRUCT   {  
          HWND   hwnd;  
          UINT   wFunc;  
          LPCTSTR   pFrom;  
          LPCTSTR   pTo;  
          FILEOP_FLAGS   fFlags;  
          BOOL   fAnyOperationsAborted;  
          LPVOID   hNameMappings;  
          LPCTSTR   lpszProgressTitle;  
  }   SHFILEOPSTRUCT,   *LPSHFILEOPSTRUCT;  
  Members  
   
  hwnd  
  Window   handle   to   the   dialog   box   to   display   information   about   the   status   of   the   file   operation.    
  wFunc  
  Value   that   indicates   which   operation   to   perform.   This   member   can   be   one   of   the   following   values:    
  FO_COPY  
  Copy   the   files   specified   in   the   pFrom   member   to   the   location   specified   in   the   pTo   member.    
  FO_DELETE  
  Delete   the   files   specified   in   pFrom.    
  FO_MOVE  
  Move   the   files   specified   in   pFrom   to   the   location   specified   in   pTo.    
  FO_RENAME  
  Rename   the   file   specified   in   pFrom.   You   cannot   use   this   flag   to   rename   multiple   files   with   a   single   function   call.   Use   FO_MOVE   instead.    
  pFrom  
  Address   of   a   buffer   to   specify   one   or   more   source   file   names.   These   names   must   be   fully   qualified   paths.   Standard   Microsoft®   MS-DOS®   wild   cards,   such   as   "*",   are   permitted   in   the   file-name   position.   Although   this   member   is   declared   as   a   null-terminated   string,   it   is   used   as   a   buffer   to   hold   multiple   file   names.   Each   file   name   must   be   terminated   by   a   single   NULL   character.   An   additional   NULL   character   must   be   appended   to   the   end   of   the   final   name   to   indicate   the   end   of   pFrom.    
  pTo  
  Address   of   a   buffer   to   contain   the   name   of   the   destination   file   or   directory.   This   parameter   must   be   set   to   NULL   if   it   is   not   used.   Like   pFrom,   the   pTo   member   is   also   a   double-null   terminated   string   and   is   handled   in   much   the   same   way.   However,   pTo   must   meet   the   following   specifications:    
  Wildcard   characters   are   not   supported.    
  Copy   and   Move   operations   can   specify   destination   directories   that   do   not   exist   and   the   system   will   attempt   to   create   them.   The   system   normally   displays   a   dialog   box   to   ask   the   user   if   they   want   to   create   the   new   directory.   To   suppress   this   dialog   box   and   have   the   directories   created   silently,   set   the   FOF_NOCONFIRMMKDIR   flag   in   fFlags.    
  For   Copy   and   Move   operations,   the   buffer   can   contain   multiple   destination   file   names   if   the   fFlags   member   specifies   FOF_MULTIDESTFILES.  
  Pack   multiple   names   into   the   string   in   the   same   way   as   for   pFrom.    
  Use   only   fully-qualified   paths.   Using   relative   paths   will   have   unpredictable   results.    
  fFlags  
  Flags   that   control   the   file   operation.   This   member   can   take   a   combination   of   the   following   flags:    
  FOF_ALLOWUNDO  
  Preserve   Undo   information,   if   possible.   If   pFrom   does   not   contain   fully   qualified   path   and   file   names,   this   flag   is   ignored.    
  FOF_CONFIRMMOUSE  
  Not   currently   used.    
  FOF_FILESONLY  
  Perform   the   operation   on   files   only   if   a   wildcard   file   name   (*.*)   is   specified.    
  FOF_MULTIDESTFILES  
  The   pTo   member   specifies   multiple   destination   files   (one   for   each   source   file)   rather   than   one   directory   where   all   source   files   are   to   be   deposited.    
  FOF_NOCONFIRMATION  
  Respond   with   "Yes   to   All"   for   any   dialog   box   that   is   displayed.    
  FOF_NOCONFIRMMKDIR  
  Do   not   confirm   the   creation   of   a   new   directory   if   the   operation   requires   one   to   be   created.    
  FOF_NO_CONNECTED_ELEMENTS  
  Version   5.0.   Do   not   move   connected   files   as   a   group.   Only   move   the   specified   files.    
  FOF_NOCOPYSECURITYATTRIBS  
  Version   4.71.   Do   not   copy   the   security   attributes   of   the   file.  
  FOF_NOERRORUI  
  Do   not   display   a   user   interface   if   an   error   occurs.    
  FOF_NORECURSION  
  Only   operate   in   the   local   directory.   Don't   operate   recursively   into   subdirectories.  
  FOF_NORECURSEREPARSE  
  Treat   reparse   points   as   objects,   not   containers.   You   must   set   _WIN32_WINNT   to   5.01   or   later   to   use   this   flag.   See   Shell   and   Common   Controls   Versions   for   further   discussion   of   versioning.    
  FOF_RENAMEONCOLLISION  
  Give   the   file   being   operated   on   a   new   name   in   a   move,   copy,   or   rename   operation   if   a   file   with   the   target   name   already   exists.    
  FOF_SILENT  
  Do   not   display   a   progress   dialog   box.    
  FOF_SIMPLEPROGRESS  
  Display   a   progress   dialog   box   but   do   not   show   the   file   names.    
  FOF_WANTMAPPINGHANDLE  
  If   FOF_RENAMEONCOLLISION   is   specified   and   any   files   were   renamed,   assign   a   name   mapping   object   containing   their   old   and   new   names   to   the   hNameMappings   member.  
  FOF_WANTNUKEWARNING  
  Version   5.0.   Send   a   warning   if   a   file   is   being   destroyed   during   a   delete   operation   rather   than   recycled.   This   flag   partially   overrides   FOF_NOCONFIRMATION.  
  fAnyOperationsAborted  
  Value   that   receives   TRUE   if   the   user   aborted   any   file   operations   before   they   were   completed,   or   FALSE   otherwise.    
  hNameMappings  
  A   handle   to   a   name   mapping   object   containing   the   old   and   new   names   of   the   renamed   files.   This   member   is   used   only   if   the   fFlags   member   includes   the   FOF_WANTMAPPINGHANDLE   flag.   See   Remarks   for   more   details.  
  lpszProgressTitle  
  Address   of   a   string   to   use   as   the   title   of   a   progress   dialog   box.   This   member   is   used   only   if   fFlags   includes   the   FOF_SIMPLEPROGRESS   flag.  
  Remarks  
   
  If   the   pFrom   or   pTo   members   are   unqualified   names,   the   current   directories   are   taken   from   the   global   current   drive   and   directory   settings   as   managed   by   the   GetCurrentDirectory   and   SetCurrentDirectory   functions.    
   
  If   pFrom   is   set   to   a   file   name,   deleting   the   file   with   FO_DELETE   will   not   move   it   to   the   Recycle   Bin,   even   if   the   FOF_ALLOWUNDO   flag   is   set.   You   must   use   a   full   path.  
   
  There   are   two   versions   of   this   structure,   an   ANSI   version   (SHFILEOPSTRUCTA)   and   a   Unicode   version   (SHFILEOPSTRUCTW).   The   Unicode   version   is   identical   to   the   ANSI   version,   except   that   wide   character   strings   (LPCWSTR)   are   used   in   place   of   ANSI   character   strings   (LPCSTR).   On   Microsoft   Windows®   98   and   earlier,   only   the   ANSI   version   is   supported.   On   Microsoft   Windows   NT®   4.0   and   later,   both   the   ANSI   and   Unicode   versions   of   this   structure   are   supported.   SHFILEOPSTRUCTW   and   SHFILEOPTSTRUCTA   should   never   be   used   directly;   the   appropriate   structure   is   redefined   as   SHFILEOPSTRUCT   by   the   precompiler   depending   on   whether   the   application   is   compiled   for   ANSI   or   Unicode.   SHNAMEMAPPING   has   similar   ANSI   and   Unicode   versions.   For   ANSI   applications,   hNameMappings   points   to   an   int   followed   by   an   array   of   ANSI   SHNAMEMAPPING   structures.   For   Unicode   applications,   hNameMappings   points   to   an   int   followed   by   an   array   of   Unicode   SHNAMEMAPPING   structures.   However,   on   Windows   NT   4.0   and   later,   SHFileOperation   always   returns   a   handle   to   a   Unicode   set   of   SHNAMEMAPPING   structures.   If   you   want   applications   to   be   functional   with   all   versions   of   Windows,   the   application   must   employ   conditional   code   to   deal   with   name   mappings.   For   example:    
   
  x   =   SHFileOperation(&shop);  
  ...  
  if   (fWin9x)   {  
  HandleAnsiNameMappings(shop.hNameMappings);  
  }  
   
  else   {  
  HandleUnicodeNameMappings(shop.hNameMappings);  
  }  
   
   
  Treat   hNameMappings   as   a   pointer   to   a   structure   whose   members   are   an   UINT   value   followed   by   a   pointer   to   an   array   of   SHNAMEMAPPING   structures,   for   example:    
   
  struct   HANDLETOMAPPINGS   {  
          UINT                             uNumberOfMappings;     //   number   of   mappings   in   array  
          LPSHNAMEMAPPING       lpSHNameMapping;         //   pointer   to   array   of   mappings  
  };  
   
  The   UINT   value   is   set   to   the   number   of   SHNAMEMAPPING   structures   in   the   array.   Each   SHNAMEMAPPING   structure   contains   the   old   and   new   path   for   one   of   the   renamed   files.  
   
  Note     The   handle   must   be   freed   with   SHFreeNameMappings.  
  For   a   complete   application   that   uses   the   SHFILEOPSTRUCT   structure   and   explains   how   to   set   up   the   SHNAMEMAPPING   structure,   see   the   Knowledge   Base   Article   Q151799   at   http://support.microsoft.com/support/kb/articles/Q151/7/99.asp.  
   
  Top

5 楼gunney(楚州才子)回复于 2005-09-28 09:22:50 得分 0

int   SHFileOperation(LPSHFILEOPSTRUCT   lpFileOp);  
  直接删除目录或文件   无论目录是否为空  
  而且可以定制是否删除到垃圾箱     是否显示进度   这是一个   SHELL函数    
  在导入库中加入   shell32.lib   头文件是shellapi.h  
  爽吧    
  Minimum   DLL   Version   shell32.dll   version   4.0   or   later    
  Custom   Implementation   No    
  Header   shellapi.h    
  Import   library   shell32.lib    
  Minimum   operating   systems   Windows   NT   4.0,   Windows   95    
   
   
  Top

6 楼gunney(楚州才子)回复于 2005-09-28 09:24:11 得分 0

如果你要删除  
  lpFileOp-》wFunc   =   FO_DELETE    
  SO   EASY   楼主是吧Top

相关问题

  • 删除文件夹
  • 请问 asp.net怎样删除文件夹里的所有文件? 而对子文件夹不能删除?
  • 如何删除一个文件夹下的所有文件?
  • 急!!!!如何删除一个文件夹下的所有文件
  • 如何删除一个文件夹下的所有文件?
  • 如何删除某一文件夹下的所有文件?
  • 如何删除一个文件夹内的所有文件?
  • 如何删除文件夹?
  • 怎样删除文件夹
  • 文件夹删除不了

关键词

  • .net
  • 文件夹
  • 文件
  • null
  • fileop
  • 删除
  • shfileop
  • lpszpath
  • fof
  • faq

得分解答快速导航

  • 帖主:ligeleng0
  • phoenix96_2000
  • titilima
  • laiyiling
  • gunney

相关链接

  • Visual C++类图书
  • Visual C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo