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 ); Static Void Mydeletedirectory ( String Filedir );};
Myfile. cpp
# Include " Stdafx. h " # Include " Myfile. h " Maid ( String Orig ); Void Copyfilerealize (String Filepath, String Destpath, String Sourcepatht ); Void Findallfile ( String Sourcepath, String Destpath, String Sourcepatht ); Bool Deletedirectory (lpctstr lpszdir, Bool Norecyclebin); cmyfile: cmyfile ( Void ) {} Cmyfile :: ~ Cmyfile ( Void ){} /// // File to be copied in sourcefilepath // Destfilepaht target file /// Void Cmyfile: mycopyfile ( String Sourcefilepath, String Destfilepath) {findallfile (sourcefilepath, destfilepath, sourcefilepath );} Void Cmyfile: mydeletedirectory ( String Filedir) {lpcwstr wfilepath = Stringtolpcwstr (filedir); deletedirectory (wfilepath, True );} Void Copyfilerealize ( String Filepath, String Destpath, String Sourcepatht ){ String Newfilepath = Filepath; Int Replacelength = sourcepatht. find_last_of ( " \\ " ); Newfilepath. Replace ( 0 , Replacelength, destpath ); Int Lastindex = newfilepath. find_last_of ( " \\ " ); String Destdir = newfilepath. substr ( 0 , Lastindex ); String Destfilename = Newfilepath. substr (lastindex); lpcwstr wfilepath = Stringtolpcwstr (filepath); lpcwstr wdestdir = Stringtolpcwstr (destdir); lpcwstr wdestfilepath = Stringtolpcwstr (destdir + Destfilename ); If (_ Access (destdir. c_str (), 0 ) =- 1 ) {Createdirectory (wdestdir, null);} copyfile (wfilepath, wdestfilepath, false );} Bool Deletedirectory (lpctstr lpszdir,Bool Norecyclebin = True ){ Int Len = _ Tcslen (lpszdir); tchar * Pszfrom = New Tchar [Len + 2 ]; _ Tcscpy (pszfrom, lpszdir); pszfrom [Len] = 0 ; Pszfrom [Len + 1 ] = 0 ; Shfileopstruct fileop; fileop. hwnd = NULL; // No Status display Fileop. wfunc = fo_delete; // Delete operation Fileop. pfrom = pszfrom; // Source file name as double NULL terminated string Fileop. PTO = NULL; // No destination needed Fileop. fflags = fof_noconfirmation | fof_silent; // Do not prompt the user If (!Norecyclebin) fileop. fflags | = Fof_allowundo; fileop. fanyoperationsaborted = False; fileop. lpszprogresstitle = NULL; fileop. hnamemappings = NULL; Int Ret = shfileoperation (& Fileop); Delete [] pszfrom; Return (Ret = 0 );} Void Findallfile ( String Sourcepath, String Destpath, String Sourcepatht ){ 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 a subdirectory exists If (Fileinfo. attrib & _ a_subdir) = _ A_subdir ){ If (Strcmp (fileinfo. Name, " . " )! = 0 ) & (Strcmp (fileinfo. Name, " .. " )! = 0 )){ String Newpath = sourcepath + " \\ " +Fileinfo. Name; findallfile (newpath, destpath, sourcepatht );}} Else { String Filepath = sourcepath + " \\ " + Fileinfo. Name; copyfilerealize (filepath, destpath, sourcepatht );}} 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 ;}
Cmyfile: mydeletedirectory ("C: \ messer_2.0changshi \ DB ");
Cmyfile: mycopyfile ("C: \ Program Files \ MDB \ DB", "C: \ messer_2.0changshi ");