代码如下:I Created this to show you how to delete all the files and subfolders in a selected folder including subfolders. It's very easy to understand and it's all by using the MFC (CFileFind, with some API functions)void RecursiveDelete(CString szPath)
{
CFileFind ff;
CString path = szPath;

if(path.Right(1) != "\\")
path += "\\"; path += "*.*"; BOOL res = ff.FindFile(path); while(res)
{
res = ff.FindNextFile();
if (!ff.IsDots() && !ff.IsDirectory())
DeleteFile(ff.GetFilePath());
else if (ff.IsDirectory())
{
path = ff.GetFilePath();
RecursiveDelete(path);
RemoveDirectory(path);
}
}
}

解决方案 »

  1.   

    奋战几分钟,终于搞定(递归调用还真麻烦):Public Function RecursiveDelete(szPath As String)
    Dim ff As String
    Dim dd As String
    Dim dv(100) As String
    Dim ii, jj As Integer
    On Error Resume Next
    If Right(szPath, 1) <> "\" Then szPath = szPath + "\"'删目录及子目录下文件的部分
    dd = Dir(szPath, vbDirectory)
    While dd <> ""
      If dd <> "." And dd <> ".." And dd <> "" Then
        jj = jj + 1
        dv(jj) = dd
      End If
      dd = Dir
    WendIf jj > 0 Then
        For ii = 1 To jj
            RecursiveDelete (szPath + dv(ii))
            RmDir (szPath + dv(ii))
        Next
    End If
    '删文件的部分
    ff = Dir(szPath, vbArchive)
    If ff <> "" Then
      Kill szPath + ff
    End IfWhile ff <> ""
      ff = Dir
      Kill szPath + ff
    WendEnd Function
      

  2.   

    请问penglc(猎者)非常感谢您的回答,我刚学vb,能不能把其中代码详细讲解一下,特别是定义的变量意义和
    用到的函数如dir,
      

  3.   

    简单说吧:函数:
    dir() 是返回指定路径的 文件或目录的系统函数。 上面有两种用法,一种是返回目录名的,一种是返回文件名的。变量:ff 用来存放dir 返回的文件名。
    dd 用来存放dir 返回的目录名。
    dv(100) 数组,由于不知目录下会有多少个子目录, 故先置100个的数组来存放。
    ii, jj 都是计数用的变量。
     此函数写法是跟 你给的C代码基本一样的, 如果该目录下没有子目录,则删除该目录下所有文件; 如果有子目录,则再传递子目录路径调用自己删除其下的文件及子目录,这样一直递归调用,直到指定目录下的所有 文件、子子目录、子子目录下的文件删完为止。