/********************************************************************************
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