Segment copy file folder

Source: Internet
Author: User

For personal backup only

// Segment copy a single file note # define lengtheachtime 1024 // parameter 1. source File Path 2. source File Name 3. destination folder path // test: // cstring A = l "d: \ backup \ My Documents \ 11.txt"; // cstring B = l" d: \ backup \ My Documents \ 1 "; // filepartcopy (A, l" 11.txt", B); // funcvoid filepartcopy (cstring srcfilepath, cstring srcfilename, cstring desfilepath) {/* DWORD lengtheachtime = 1024; * // number of bytes copied each time desfilepath = desfilepath + _ T ("\") + srcfilename; // create the ifstream filein (srcfilepath, IOS: Binary | IOs: In) folder in the target path; // open-source ofstream fileout (desfilepath, IOS: Binary | IOs:: Out); // create the target filein. seekg (0, IOS: End); DWORD srcfilesize = (DWORD) filein. tellg (); // get the file length filein. seekg (0, IOS: Beg); // restore the file pointer to the file header if (lengtheachtime <srcfilesize) // If the multipart copy, that is, each copy content is smaller than the total length of the file {char buffer [lengtheachtime]; // cache DWORD copied = 0; // store the copied size while (copied <= (srcfilesize-lengtheachtime )) // The Difference Between the copied content and the source file size must be at least one lengtheachtime copy {filein. read (buffer, lengtheachtime); fileout. write (buffer, lengtheachtime); copied + = lengtheachtime;} DWORD leftlength = srcfilesize-copied; // copy the remaining part of filein. read (buffer, leftlength); fileout. write (buffer, leftlength);} else // overall copy, that is, the content of each copy is greater than the total length of the file {char buffer [lengtheachtime]; filein. read (buffer, srcfilesize); fileout. write (buffer, srcfilesize);} filein. close (); fileout. close ();}

 

// Segment copy folder note # define lengtheachtime 1024 // parameter 1. source folder Path 2. source folder name 3. destination folder path // funcvoid directorypartcopy (cstring srcdirectorypath, cstring srcdirectoryname, cstring desdirectorypath) {cstring dessrcdirectory = desdirectorypath + _ T ("\") + srcdirectoryname; // create the source folder createdirectory (dessrcdirectory, null); bool ifexist = false; cfilefind finder; ifexist = finder. findfile (srcdirectorypath + _ T ("\\*. * "); If (! Ifexist) {return;} while (ifexist) {ifexist = finder. findnextfile (); If (finder. isdots () // if it is ". "(current directory) or ".. "(parent directory) {continue;} cstring tempsrcdirectorypath = finder. getfilepath (); cstring tempsrcdirectoryname = finder. getfilename (); If (finder. isdirectory () {directorypartcopy (tempsrcdirectorypath, tempsrcdirectoryname, dessrcdirectory);} else {cstring dessrcfile = dessrcdirectory + _ T ("\") + tempsrcdirectoryname; // create the file ifstream filein (tempsrcdirectorypath, IOS: Binary | IOs: In) at the destination path; // open-source ofstream fileout (dessrcfile, IOS: Binary | IOs:: Out); // create the target filein. seekg (0, IOS: End); DWORD srcfilesize = (DWORD) filein. tellg (); If (lengtheachtime <srcfilesize) // If multipart copy is performed, the content of each copy is smaller than the total file length {char buffer [lengtheachtime]; DWORD copied = 0; while (copied <= (srcfilesize-lengtheachtime) // copy the subject part {filein. read (buffer, lengtheachtime); fileout. write (buffer, lengtheachtime); copied + = lengtheachtime;} DWORD leftlength = srcfilesize-copied; // copy the remaining part of filein. read (buffer, leftlength); fileout. write (buffer, leftlength);} else // if the overall copy is performed, the content of each copy is greater than the total length of the file {char buffer [lengtheachtime]; filein. read (buffer, srcfilesize); fileout. write (buffer, srcfilesize);} filein. close (); fileout. close ();}}}

 

// Optimize void directorypartcopy (cstring srcdirectorypath, cstring srcdirectoryname, cstring desdirectorypath) {cstring dessrcdirectory = desdirectorypath + _ T ("\") + srcdirectoryname; // create the source folder createdirectory (dessrcdirectory, null); bool ifexist = false; cfilefind finder; ifexist = finder. findfile (srcdirectorypath + _ T ("\\*. * "); If (! Ifexist) {return;} while (ifexist) {ifexist = finder. findnextfile (); If (finder. isdots () // if it is ". "(current directory) or ".. "(parent directory) {continue;} cstring tempsrcdirectorypath = finder. getfilepath (); cstring tempsrcdirectoryname = finder. getfilename (); If (finder. isdirectory () {directorypartcopy (tempsrcdirectorypath, tempsrcdirectoryname, dessrcdirectory);} else {filepartcopy (tempsrcdirectorypath, tempsrcdirectoryname, dessrcdirectory );}}}

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.