Copy folder under the same type of file (CopyFile)

Source: Internet
Author: User

This example enables a TXT file under a folder to be copied to the destination folder. The method is to traverse the source folder and, if it is a TXT file, copy it.

int Ctestcsdndlg::copyfileex (CString strsrcpath, CString strext, CString Strdespath)

{

CFileFind FF;

CString szdir = Strsrcpath;

CString strsize = _t ("");

if (Szdir.right (1)! = "//")

Szdir + = "//";

Szdir + = "* *";

BOOL res = ff. FindFile (Szdir);

while (RES)

{

res = ff. FindNextFile ();

if (ff. Isdirectory () &&!ff. Isdots ())//Folder

{

CString Strfolderpath = ff. GetFilePath ();

CopyFileEx (Strfolderpath, Strext, Strdespath);

}

else if (!ff. Isdirectory () &&!ff. Isdots ())//File

{

To determine the extension name

CString strFileName = ff. GetFileName ();

int NPos;

NPos = Strfilename.reversefind (_t ('. '));

CString Strfileext;

Strfileext = Strfilename.right (Strfilename.getlength ()-nPos-1);

if (Strfileext = = Strext)

{

Copying files

CString strFilePath;

strFilePath = ff. GetFilePath ();

CString Strnewfilepath;

Strnewfilepath = Strdespath + _t ("//") + strFileName;

:: CopyFile (strFilePath, Strnewfilepath, FALSE);

}

}

}

Ff. Close ();

return 0;

}


How to use:
CopyFileEx (_t ("E://test"), _t ("txt"), _t ("E://test 2"));
First parameter: source path
Second parameter: extension
Third parameter: Destination path

If the copy includes subfolders, use the following method:

Modify this snippet:

if (ff. Isdirectory () &&!ff. Isdots ())//Folder
{
CString Strfolderpath = ff. GetFilePath ();
CString strfoldername = ff. GetFileName ();
CString Strdestsubfolder = Strdespath + _t ("//") + strFolderName;
CreateDirectory (Strdestsubfolder, NULL);
CopyFileEx (Strfolderpath, Strext, Strdestsubfolder);
}

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.