Original URL: http://shijuanfeng.blogbus.com/logs/100675115.html
The first method: Define a File class object to manipulate
CFile Tempfile;
Tempfile.remove (specify file name);
The second method: Use the system function DeleteFile (LPCSTR filename) to delete the file _rmdir (), delete the directory deletedirectory (Stempdir); Delete directory removedirectory (stempdir);
Eg:deletefile (char *tempfilename);
Note: To empty the file but keep the directory: System ("Del C:\temp"); All files in C:\temp are emptied, but the subdirectories under the folder are not clear and will pop up: whether to delete the Doc box
Delete folder directory (non-empty) the above mentioned Delete directory method can only delete empty directory (that is, folder), if the directory has files or subdirectories, it can not be deleted, VC seems to have no direct function, can only manually write a function to delete, the following provides a way to delete a non-empty directory:
BOOL DeleteDirectory (char* sdirname)
{
CFileFind Tempfind;
Char stempfilefind[200];
strcpy (Stempfilefind, sdirname);
strcat (Stempfilefind, "\\*.*");
BOOL isfinded = Tempfind.findfile (Stempfilefind);
while (isfinded)
{
isfinded = Tempfind.findnextfile ();
Char sfoundfilename[200];
strcpy (Sfoundfilename,tempfind.getfilepath ());
DeleteFile (Sfoundfilename);
}
Tempfind.close ();
}
Empty the contents of the entire folder (including subfolders), but keep the folder
void Crelctrldlg::D eletedirectory (char* sdirname)
{
Char spath[200];
strcpy (spath, sdirname);
CFileFind FF;
BOOL Bfound;
Char stempfilefind[200];
strcpy (Stempfilefind, spath);
strcat (Stempfilefind, "\\*.*");
Bfound = ff. FindFile (Stempfilefind);
while (Bfound)
{
Bfound = ff. FindNextFile ();
CString Sfilepath = ff. GetFilePath ();
if (ff. Isdirectory ())
{
if (!ff. Isdots ())
DeleteDirectory ((LPSTR) (LPCTSTR) sfilepath);
}
Else
{
if (ff. IsReadOnly ())
{
SetFileAttributes (Sfilepath, file_attribute_normal);
}
DeleteFile (Sfilepath);
}
}
Ff. Close ();
SetFileAttributes (spath, file_attribute_normal);
if (!strcmp (spath,sdirname))
{
Return
}
RemoveDirectory (spath);
}
"Go" VC MFC how to delete files, directories, folders