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 ");