VC/MFC (C ++) for file copying and Deletion

Source: Internet
Author: User

1. copy a file

/////////////////////////////////
// Copy an MFC File
/////////////////////////////////
Void filecopyto (cstring source, cstring destination, cstring searchstr, bool cover = true)
{
Cstring strsourcepath = source;
Cstring strdespath = destination;
Cstring strfilename = searchstr;
Cfilefind filefinder;
Cstring strsearchpath = strsourcepath + "//" + strfilename;
Cstring filename;
Bool bfind = filefinder. findfile (strsearchpath );

Cstring sourcepath, dispath;
While (bfind)
{
Bfind = filefinder. findnextfile ();
Filename = filefinder. getfilename ();
Sourcepath = strsourcepath + "//" + filename;
Dispath = strdespath + "//" + filename;
Copyfile (lpcstr) sourcepath, (lpcstr) dispath, cover );
}
Filefinder. Close ();
}

/////////////////////////////////
// Copy a C ++ File
/////////////////////////////////
# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
# Include <Io. h>
# Include <windows. h>
Void filecopyto (char * Source, char * Dis, char * searchchars, bool bcover/* = true */)
{
Struct _ finddata_t ffblk;
Char path [256], sourcepath [256], dispath [256];
Sprintf (path, "% S // Q _ *", source );
Long done = _ findfirst (path, & ffblk );
Int find = 0;
While (find = 0)
{
If (strcmp (ffblk. Name, "Q _"))
{
Sprintf (sourcepath, "% S // % s", source, ffblk. Name );
Sprintf (dispath, "% S // % s", DIS, ffblk. Name );
Copyfile (sourcepath, dispath, bcover );
}
Find = _ findnext (done, & ffblk );
}
_ Findclose (done );
}

2. delete a file

// Delete the folder directory (not empty)
Bool deletedirectory (char * sdirname)
{
Cfilefind tempfind;
Char stempfilefind [200];

Sprintf (stempfilefind, "% S // *. *", sdirname );
Bool isfinded = tempfind. findfile (stempfilefind );
While (isfinded)
{
Isfinded = tempfind. findnextfile ();

If (! Tempfind. isdots ())
{
Char sfoundfilename [200];
Strcpy (sfoundfilename, tempfind. getfilename (). getbuffer (200 ));

If (tempfind. isdirectory ())
{
Char stempdir [200];
Sprintf (stempdir, "% S // % s", sdirname, sfoundfilename );
Deletedirectory (stempdir );
}
Else
{
Char stempfilename [200];
Sprintf (stempfilename, "% S // % s", sdirname, sfoundfilename );
Deletefile (stempfilename );
}
}
}
Tempfind. Close ();
If (! Removedirectory (sdirname ))
{
Return false;
}
Return true;
}
-------------------------------------------------------------
Method 2 (directly call the command ):

# Include <stdlib. h>
# Include <stdio. h>

Int main ()
{
System ("md d: // AA // Zhao"); // create a folder
System ("del D: // AA // Zhao"); // delete all files in this folder
}
Article Source: http://www.diybl.com/course/3_program/vc/vc_js/2008426/111574.html

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.