VC ++ copies the entire folder

Source: Internet
Author: User

Copyfile can only copy one file. to copy all the files in the entire folder, you need to traverse the files in the folder.

Myfile. h

 
# Pragma once # include <string> # include <windows. h> # include <iostream> # include <stdio. h> # include <Io. h> # include <string> # include <shlwapi. h> using namespace STD; Class cmyfile {public: explicit cmyfile (void );~ Cmyfile (void); static void mycopyfile (string sourcefilepath, string destfilepath );};

Myfile. cpp

# Include "stdafx. H "# include" myfile. H "Maid (string orig); void copyfilerealize (string filepath, string destpath); void findallfile (string sourcepath, string destpath); cmyfile: cmyfile (void) {/* void mycopyfile (string sourcefilepath, string destfilepath); findallfile (sourcefilepath, destfilepath); */} cmyfile ::~ Cmyfile (void) {} void cmyfile: mycopyfile (string sourcefilepath, string destfilepath) {findallfile (sourcefilepath, destfilepath);} void copyfilerealize (string filepath, string destpath) {string newfilepath = destpath + filepath. substr (2, filepath. length (); int lastindex = newfilepath. find_last_of ("\"); string destdir = newfilepath. substr (0, lastindex); string destfilename = newfilepath. substr (lastindex); lpcwstr wfilepa Th = stringtolpcwstr (filepath); maid (destdir); If (_ access (destdir + destfilename. c_str (), 0) =-1) {createdirectory (wdestdir, null);} copyfile (wfilepath, wdestfilepath, false);} void findallfile (string sourcepath, string destpath) {struct _ finddata_t fileinfo; string strfind = sourcepath + "\ *"; long handle = _ findfirst (strfind. c_str (), & fileinfo ); If (handle =-1l) {cerr <"can not match the folder path" <Endl; exit (-1 );} do {// determine whether there is a subdirectory if (fileinfo. attrib & _ a_subdir) = _ a_subdir) {If (strcmp (fileinfo. name ,". ")! = 0) & (strcmp (fileinfo. Name ,"..")! = 0) {string newpath = sourcepath + "\" + fileinfo. name; findallfile (newpath, destpath) ;}} else {string filepath = sourcepath + "\" + fileinfo. name; copyfilerealize (filepath, destpath) ;}} while (_ findnext (handle, & fileinfo) = 0); _ findclose (handle); // fout. close ();} maid (string orig) {size_t origsize = orig. length () + 1; const size_t newsize = 100; size_t convertedchars = 0; wchar_t * wcstring = (wchar_t *) malloc (sizeof (wchar_t) * (orig. length ()-1); mbstowcs_s (& convertedchars, wcstring, origsize, orig. c_str (), _ truncate); Return wcstring ;}

Use

Cmyfile: mycopyfile ("E: \ test1", "E: \ t2 ");

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.