如何删除文件夹?在线等,谢谢.......

qqcc 2003-11-25 10:43:11
比如,有文件夹d:\mydir\tt\,在tt中有一些文件,如何删除?好象没有现成的函数,分
98和XP两种情况,在线等,谢谢
...全文
76 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
smallbull 2003-11-25
  • 打赏
  • 举报
回复
BOOL DelDirectory(CString DirName)
{
    CFileFind tempFind;
    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())
            {
                  char tempDir[200];
                  sprintf(tempDir,"%s\\%s",DirName,foundFileName);
                  DelDirectory(tempDir);
            }
            else
            {
                  char tempFileName[200];
                  sprintf(tempFileName,"%s\\%s",DirName,foundFileName);
                  DeleteFile(tempFileName);
            }
        }
  }
  tempFind.Close();
  if(!RemoveDirectory(DirName))
  {
      ::MessageBox(0,"删除目录失败!","警告信息",MB_OK);
      return FALSE;
  }
  return TRUE;
}
keiven 2003-11-25
  • 打赏
  • 举报
回复
up
akiy 2003-11-25
  • 打赏
  • 举报
回复
BOOL EmptyDirectory(LPCTSTR szPath, BOOL bDeleteDesktopIni,
BOOL bWipeIndexDat)
{
WIN32_FIND_DATA wfd;
HANDLE hFind;
CString sFullPath;
CString sFindFilter;
DWORD dwAttributes = 0;

sFindFilter = szPath;
sFindFilter += _T("\\*.*");
if ((hFind = FindFirstFile(sFindFilter, &wfd)) == INVALID_HANDLE_VALUE)
{
return FALSE;
}

do
{
if (_tcscmp(wfd.cFileName, _T(".")) == 0 ||
_tcscmp(wfd.cFileName, _T("..")) == 0 ||
(bDeleteDesktopIni == FALSE && _tcsicmp(wfd.cFileName, _T("desktop.ini")) == 0))
{
continue;
}

sFullPath = szPath;
sFullPath += _T('\\');
sFullPath += wfd.cFileName;

//去掉只读属性
dwAttributes = GetFileAttributes(sFullPath);
if (dwAttributes & FILE_ATTRIBUTE_READONLY)
{
dwAttributes &= ~FILE_ATTRIBUTE_READONLY;
SetFileAttributes(sFullPath, dwAttributes);
}

if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
EmptyDirectory(sFullPath, bDeleteDesktopIni, bWipeIndexDat);
RemoveDirectory(sFullPath);
}
else
{
if (bWipeIndexDat && _tcsicmp(wfd.cFileName, _T("index.dat")) == 0)
{
WipeFile(szPath, wfd.cFileName);
}

DeleteFile(sFullPath);
}
}
while (FindNextFile(hFind, &wfd));
FindClose(hFind);

return TRUE;
}
fireseed 2003-11-25
  • 打赏
  • 举报
回复

Platform SDK: Storage
RemoveDirectory

The RemoveDirectory function deletes an existing empty directory.


BOOL RemoveDirectory(
LPCTSTR lpPathName
);

Parameters
lpPathName
[in] Pointer to a null-terminated string that specifies the path of the directory to be removed. The path must specify an empty directory, and the calling process must have delete access to the directory.
In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path. For more information, see Naming a File.


Windows Me/98/95: This string must not exceed MAX_PATH characters.


Return Values
If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks
To recursively delete the files in a directory, use the SHFileOperation function.



Windows Me/98/95: RemoveDirectoryW is supported by the Microsoft Layer for Unicode. To use this, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.



Requirements
Client: Included in Windows XP, Windows 2000 Professional, Windows NT Workstation, Windows Me, Windows 98, and Windows 95.
Server: Included in Windows Server 2003, Windows 2000 Server, and Windows NT Server.
Unicode: Implemented as Unicode and ANSI versions. Note that Unicode support on Windows Me/98/95 requires Microsoft Layer for Unicode.
Header: Declared in Winbase.h; include Windows.h.
Library: Use Kernel32.lib.


See Also
Directory Management Functions, CreateDirectory

Platform SDK Release: February 2003 What did you think of this topic?
Order a Platform SDK CD



Requirements
Client: Included in Windows XP, Windows 2000 Professional, Windows NT Workstation, Windows Me, Windows 98, and Windows 95.
Server: Included in Windows Server 2003, Windows 2000 Server, and Windows NT Server.
Unicode: Implemented as Unicode and ANSI versions. Note that Unicode support on Windows Me/98/95 requires Microsoft Layer for Unicode.
Header: Declared in Winbase.h; include Windows.h.
Library: Use Kernel32.lib.

See Also
Directory Management Functions, CreateDirectory
vcforever 2003-11-25
  • 打赏
  • 举报
回复
删除不为空的文件夹是一个递归过程,下面这个函数可以删除不为空的文件夹
BOOL DeleteDirectory(CString sDirectory)
{
CString sPath = sDirectory;
sPath += "\\*.*";

CFileFind finder;
CString sFileName;
BOOL bIsFindFile = finder.FindFile(sPath);
while (bIsFindFile)
{
bIsFindFile = finder.FindNextFile();
if (!finder.IsDots())
{
sFileName = finder.GetFilePath();
if (finder.IsDirectory())
{
::SetFileAttributes(sFileName, FILE_ATTRIBUTE_ARCHIVE);
if (!DeleteDirectory(sFileName))
{
return FALSE;
}
}
else
{
::SetFileAttributes(sFileName, FILE_ATTRIBUTE_ARCHIVE);
::DeleteFile(sFileName);
}
}
}
finder.Close();
::SetFileAttributes(sDirectory, FILE_ATTRIBUTE_ARCHIVE);
if (!::RemoveDirectory(sDirectory))
{
return FALSE;
}
return TRUE;
}
bcpl 2003-11-25
  • 打赏
  • 举报
回复
下面的代码用于删除文件夹tt

SHFILEOPSTRUCT sfo;
memset(&sfo, 0, sizeof(sfo));
sfo.fFlags = FOF_NOCONFIRMATION;
sfo.wFunc = FO_DELETE;
sfo.pFrom = "d:\\mydir\\tt";
::SHFileOperation(&sfo);
xjtt2000 2003-11-25
  • 打赏
  • 举报
回复
先将文件夹内的文件一一删除,再将文件夹删除。
remove()删文件,
rmdir()删目录。

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧