C ++ implements File Deletion

Source: Internet
Author: User
The Folder deletion method I used in the program does not help you
Bool deletedirectory (ansistring dirname)
{
// This function deletes a non-empty directory dirname
Shfileopstruct filestruct;
Memset (& filestruct, 0, sizeof (filestruct); // fill the structure buffer with 0
Filestruct. hwnd = NULL;
Filestruct. wfunc = fo_delete; // Delete
Filestruct. pfrom = dirname. c_str ();
Filestruct. PTO = NULL;
Filestruct. fflags = fof_noconfirmation + fof_silent;
Filestruct. hnamemappings = NULL;
If (shfileoperation (& filestruct) = 0)
Return true;
Else
Return false;
}
// In addition, one of the methods I get from others is as follows:
Bool _ stdcall deldir (char * dir_fullpath) // Delete the specified directory
{
Char dir [260];
Char filename [260];
Int Len = 0;
Int CH = '//';

Strcpy (Dir, dir_fullpath );

Len = strlen (DIR );

Char * temp = strrchr (Dir, CH );
If (LEN <4 | temp = NULL) // it may be the disk root directory or an invalid directory path.
Return false;
If (temp! = NULL)
{
If (temp-Dir + 1 )! = Len) // Add '/' After the Directory '/'
Strcat (Dir ,"//");
}

Getcurrentdirectory (260, filename );
Strcat (filename ,"//");
If (strcmp (Dir, filename) = 0) // If the directory to be deleted is the current directory
{
Strcat (filename ,"..");
Setcurrentdirectory (filename); // change the current directory
}

Win32_find_data finddata;
Handle ffile;
Bool flag;

Strcpy (filename, DIR );
Strcat (filename ,"*.*");
Ffile = findfirstfile (filename, & finddata );
Flag = true;

If (ffile! = Invalid_handle_value) // This directory is not an empty directory
{
Bool bfind = true;
While (bfind)
{
If (finddata. cfilename [0]! = '.')
{
Strcpy (filename, DIR );
Strcat (filename, finddata. cfilename );
If (finddata. dwfileattributes & file_attribute_directory)
{// Delete the subdirectory found
Strcat (filename ,"//");
Flag = Flag & deldir (filename );
}
Else
{// Delete the found File
Setfileattributes (filename, file_attribute_normal );
Flag = Flag & deletefile (filename );
}
}
Bfind = findnextfile (ffile, & finddata );
}
Findclose (ffile );
}
If (FLAG)
{
Setfileattributes (dir_fullpath, file_attribute_normal );
If (removedirectory (dir_fullpath ))
Return true;
}
Return false;
}
 

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.