"Go" VC MFC how to delete files, directories, folders

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.