Qt empty folders, delete non-empty folders

Source: Internet
Author: User
Tags readable
/********************************************************************************
Description: The classes involved in deleting files and folders include Qdir, QFile, Qfileinfo, Qfileinfolist,

There are many ways to clear a folder, here are only two methods I used

CPP source file, download address http://download.csdn.net/detail/lusirking/9551144 ************************************************** *******************************/

/********************************************************************************
Clearfiles (): Empties only files within the folder (excluding files within subfolders)
Folderfullpath: Folder full path
*********************************************************************************/
void Clearfiles (const QString &folderfullpath)
{
Qdir dir (Folderfullpath);
Dir.setfilter (Qdir::files);
int filecount = Dir.count ();
for (int i = 0; i < FileCount; i++)
Dir.remove (Dir[i]);
}

/********************************************************************************
Clearfolder (): Delete Non-empty folders
Folderfullpath: Folder full path
*********************************************************************************/
void Clearfolder (const QString &folderfullpath)
{
Qdir dir (Folderfullpath);
Qfileinfolist fileList;
Qfileinfo Curfile;
Qfileinfolist filelisttemp;
Int32 infonum;
Int32 i;
Int32 J;
/* First get all the file and folder information in the target folder */
Filelist=dir.entryinfolist (qdir::D irs| Qdir::files
| qdir::readable| Qdir::writable
| qdir::hidden| Qdir::nodotanddotdot
, qdir::name);

while (Filelist.size () > 0)
{
Infonum = Filelist.size ();

for (i = infoNum-1; I >= 0; i--)
{
Curfile = Filelist[i];
if (Curfile.isfile ())/* If it is a file, delete the file */
{
QFile filetemp (Curfile.filepath ());
Filetemp.remove ();
Filelist.removeat (i);
}

if (Curfile.isdir ())/* If it is a folder */
{
Qdir dirtemp (Curfile.filepath ());
Filelisttemp = Dirtemp.entryinfolist (qdir::D IRS | Qdir::files
| qdir::readable | Qdir::writable
| Qdir::hidden | Qdir::nodotanddotdot
, qdir::name);
if (filelisttemp.size () = = 0)/* Lower no files or folders are deleted directly */
{
Dirtemp.rmdir (".");
Filelist.removeat (i);
}
else/* The folder or file on the lower level adds the information to the list */
{
for (j = 0; J < Filelisttemp.size (); j + +)
{
if (! ( Filelist.contains (Filelisttemp[j]))
{
Filelist.append (Filelisttemp[j]);
}
}
}
}
}
}
Dir.rmdir ("."); * Delete the target folder, if you just empty the contents of the folder Folderfullpath and not delete the Folderfullpath itself, then delete the bank can */

}

Another: If you feel that the text pasted on the page is not convenient to download the CPP source file, download address http://download.csdn.net/detail/lusirking/9551144

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.