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