如何删除文件夹下所有的文件(包括非空目录和文件)????
最近遇到个问题,要删除某个文件夹下所有的文件,该文件夹下包括非空目录和一些文件,而使用DeleteFile只能删除一个指定名称的文件,哪位大侠能帮帮忙给解决一下。小弟在这里谢谢了!!!!!
问题点数:
50、回复次数:
11
1楼
h98458 (
零点起飞)
回复于
2005-03-28 09:42:36 得分
10

void __fastcall TFrmMain::delete_dir(AnsiString Source)
{
SHFILEOPSTRUCT OpStruc;
{
OpStruc.hwnd = Handle;
OpStruc.wFunc = FO_DELETE; //FO_COPY, FO_MOVE, FO_DELETE,FO_RENAME
OpStruc.fFlags = FOF_NOCONFIRMATION;
OpStruc.pFrom =Source.c_str(); //设置源文件或目录
OpStruc.pTo =NULL; //设置目标文件或目录这里是删除文件所以可以忽略
OpStruc.lpszProgressTitle = "正在删除文件……";
}
//执行函数
SHFileOperation(&OpStruc);
}
2楼
jishiping (
JSP 季世平)
回复于
2005-03-28 09:48:14 得分
40

bool DeleteFiles(AnsiString Files)
{
SHFILEOPSTRUCT fo;
Files += '/0'; //见下面的说明,注意不是 "/0"
memset(&fo, 0, sizeof(fo));
fo.fFlags = FOF_SILENT | FOF_NOCONFIRMATION;
fo.wFunc = FO_DELETE;
fo.pFrom = Files.c_str(); //必须是2个0字符结尾
return SHFileOperation(&fo) == 0;
}
这儿的参数Files,可以是文件名,也可以是目录名,可以使用*和?匹配符。比如删除
Files为 "C://Temp//*.*" 就是删除 C:/Temp 这个目录下的所有文件以及子目录。
3楼
songhtao (
三十年孤独)
回复于
2005-03-28 09:48:46 得分
0

Deletes a subfolder from the given folder.
Syntax
HRESULT DeleteFolder(ULONG cbEntryID, LPENTRYID lpEntryID, ULONG ulUIParam, LPMAPIPROGRESS lpProgress, ULONG ulFlags)
Parameters
cbEntryID
Input parameter containing the number of bytes in the entry identifier pointed to by the lpEntryID parameter.
lpEntryID
Input parameter pointing to the entry identifier of the folder to delete.
ulUIParam
Input parameter containing the handle of the window the dialog box is modal to. The ulUIParam parameter is ignored unless the FOLDER_DIALOG flag is set in the ulFlags parameter and NULL is passed in the lpProgress parameter.
lpProgress
Input parameter pointing to a progress object that contains client-supplied progress information. If NULL is passed in the lpProgress parameter, the progress information is provided by MAPI. The lpProgress parameter is ignored unless the FOLDER_DIALOG flag is set in the ulFlags parameter.
ulFlags
Input parameter containing a bitmask of flags used to control the deletion of the folders. The following flags can be set:
DEL_FOLDERS
Deletes all subfolders of the subfolder indicated in the lpEntryID parameter.
DEL_MESSAGES
Deletes all messages in the folder indicated in the lpEntryID parameter.
FOLDER_DIALOG
Displays a progress-information user interface while the operation proceeds.
Return Values
S_OK
The call succeeded and has returned the expected value or values.
MAPI_E_HAS_FOLDERS
The folder being deleted contains folders and the DEL_FOLDERS flag was not set. The folder was not deleted.
MAPI_E_HAS_MESSAGES
The folder being deleted contains messages and the DEL_MESSAGES flag was not set. The folder containing the messages was not deleted.
MAPI_W_PARTIAL_COMPLETION
The call succeeded, but not all of the entries were successfully deleted. Use the HR_FAILED macro to test for this warning, though the call should be handled as a successful return.
Comments
Message store providers use the IMAPIFolder::DeleteFolder method to delete a subfolder of a folder. If the message store provider so indicates, DeleteFolder can also delete all messages or all subfolders in a subfolder, or both. To delete all messages in a subfolder, the client application sets the DEL_MESSAGES flag in the ulFlags parameter; to delete all subfolders in a subfolder, the client application sets the DEL_FOLDERS flag in ulFlags. However, no flag need be set to delete any associated items, such as views or form definitions, from a folder.
To allow deletions of more than one subfolder to continue even if one or more subfolders marked for deletion by the calling application do not exist or have been moved elsewhere, and thus cannot be deleted, a message store provider should attempt to complete the deletion as best it can for each subfolder specified. The provider should stop the deletion without completing it only in the case of failures it cannot control, such as running out of memory or disk space, message store corruption, and so on.
If DeleteFolder successfully deletes every folder marked for deletion, it returns the value S_OK. If one or more folders cannot be deleted, DeleteFolder returns the value MAPI_W_PARTIAL_COMPLETION or MAPI_E_NOT_FOUND, depending on the message store's implementation. If DeleteFolder returns a different value, such as MAPI_E_NOT_ENOUGH_MEMORY, that indicates the call did not complete; it might have already deleted one or more folders or messages without being able to continue. The calling application cannot proceed on the assumption that an error return implies no work was done.
During a DeleteFolder call, messages that are being processed by the MAPI spooler are not deleted, nor does the message store provider attempt to call the IMsgStore::AbortSubmit method on such messages. A message being processed by the MAPI spooler is left in the folder in which it resides. This functionality might prevent one or more folders from being deleted because they still have contents.
4楼
jishiping (
JSP 季世平)
回复于
2005-03-28 09:54:01 得分
0

h98458(零点起飞) 有一个严重的错误,可能会导致严重的问题!!!如果Source指向的内存不凑巧的话,就是说万一Source结尾的0字符后面是一个合法的文件名或者目录名的话,就会将这个文件或者目录删除(尽管这个概率很小,但是一旦发生了,将造成无法挽回的损失)。 fo.pFrom 必须以2个0字符结尾!!!
6楼
h98458 (
零点起飞)
回复于
2005-03-28 10:07:33 得分
0

回复人: jishiping(JSP 季世平) ( ) 信誉:211
-----------------------------------------------
受益非浅!