MFC finds files and copies them via TXT-memo

Source: Internet
Author: User
Tags function prototype

MFC dialog-based demo

A 23-bit card number per line in txt.

The folder contains an image file named after the card number. (FPT or BMP file)

Request to traverse the folder, find the TXT card number corresponding to the image file, and copied out.

Written by VC6.0.

Too lazy, the code has been written before, direct copy is OK.

**.cpp

The code below is more commonly used and the changes are relatively large, please forget it.

voidCrenamefiledlg::onbutton1 () {//Button 1UpdateData (); CharBuf[max_path] = {0}; if(Select_any (BUF)) {M_pathname=buf;    UpdateData (FALSE); }    }voidCrenamefiledlg::onbutton2 () {//Button 2UpdateData (); //Generate an Open dialog boxCFileDialog *Lpszopenfile; Lpszopenfile=NewCFileDialog (TRUE,"Ccd.txt","", Ofn_filemustexist | Ofn_hidereadonly,"file Type (*.txt) |*.txt| All Files (*. *) |*.*|"); if(Lpszopenfile->domodal () = = IDOK)//If you click the dialog box to confirm the button{m_pathname2= Lpszopenfile->getpathname ();//get the path to the open fileSetWindowText (m_pathname2);//Show Path on window caption} updatedata (FALSE);}voidCrenamefiledlg::onbutton3 () {//Button 3UpdateData (); CharBuf[max_path] = {0}; if(Select_any (BUF)) {M_newfilepath=buf;    UpdateData (FALSE); }    }
voidCrenamefiledlg::onbutton2ok () {//Button 4strtemp="";    UpdateData (); if(m_pathname=="") {MessageBox ("Please select a path","Tips", MB_OK);return;    } h_filefile (M_pathname); ////Theapp.addtotxt (strtemp); AfxMessageBox ("The operation is complete! "); }

Traverse the folder and process the mold function.

voidcrenamefiledlg::h_filefile (CString path) {CString FilePath=path+_t ("//*.*");//can traverse all subfolders//CString filepath=path+_t ("//*.bmp "); Cannot traverseCFileFind Filefinder; BOOL bfinished=Filefinder.findfile (FilePath);  while(bfinished)//each cycle corresponds to a category directory{bfinished=Filefinder.findnextfile (); if(Filefinder.isdirectory () &&!filefinder.isdots ())//If the directory is recursive, call this method{h_filefile (Filefinder.getfilepath ()); }        Else  //determine if the file name is in txt        {                    //Get path name C:\123\456.bmpCString path2=Filefinder.getfilepath (); //Get file name 456.bmpCString fileName = Filefinder.getfilename ();////Gets a file name that does not contain a path. suffixCString F_fix,f_fname; F_fix=filename.right (4); F_fname=filename.left ( at); if(f_fix==". bmp")            {                            /////////////////CStdioFile file; if(!file. Open (M_pathname2,cfile::moderead)) {MessageBox ("can not open file!"); return;  } CString Strline,strtemp,strrow; //Strline:txt the contents of each row                 while(file.                    ReadString (StrLine)) {CString str,p; STR=strline.getbuffersetlength (Strline.getlength ()); if(strline!="")                    {                                              if(F_fname==str. Left ( at))//                        {                                                        //can write yourselfvarious processing functions

Strtemp=strtemp+filefinder.getfilepath () +"\ r \ n"; } } } ////////////////////////// } }//End_else }//End While }

I used to.

Copy file function function prototype BOOL CopyFile (LPCTSTR lpexistingfilename,lpctstr lpnewfilename,bool bfailifexists);  CopyFile (source file, target file, FALSE); //false OverwriteCopyFile (c \123\456. fpt,c:\newfilefolder\678. fpt,false); Move file function declaration bool MoveFile (LPCTSTR lpexistingfilename,//file nameLPCTSTR Lpnewfilename//New file name); Use case MoveFile ("C:\\file1.txt","C:\\file3.txt"function function to move an existing file or directory (including subdirectories) parameter description lpexistingfilename an existing file or folder string pointer lpnewfilename A string pointer to a file or folder that does not yet exist use the DeleteFile method to delete the specified file. BOOL DeleteFile (Lpcstrlpfilename//A pointer to the file name to delete), parameter lpfilename required option. The path of the file to be deleted. Return value successfully returns nonzero, failure returns 0 more error information obtained using GetLastError. If the program tries to delete a file that does not exist. GetLastError returns ERROR_FILE_NOT_FOUND. If the file is read-only, GetLastError returns error_access_denied//Get path nameCString Path2=filefinder.getfilepath ();//AfxMessageBox ("path2=" +path2); //Get file nameCString FileName =Filefinder.getfilename ();    CString Delfilepath; intDotpos33=path2. Reversefind ('\\'); Delfilepath=path2. Left (DOTPOS33) +'\\'; //Deleting Files            if(Filename.find ("-01.bmp")!=-1)            {                //AfxMessageBox ("match delete" +delfilepath+filename);DeleteFile (delfilepath+fileName); }

---------End---------

CopyFile can only copy files, cannot copy the folder of the file, copy the code of the folder to be verified. (lazy, se years try)

Shfileopstruct SFO;
Sfo.hwnd = NULL;
Sfo.wfunc = fo_copy;
Sfo.pfrom = "C:\\my_docs1\0";
Sfo.pto = "C:\\my_docs2\0";
Sfo.fflags = Fof_silent |   fof_noconfirmation | Fof_noconfirmmkdir;
hr = SHFileOperation (&SFO);

-------------

I want to move a folder to another path, along with all the sub-files and subfolders under the folder! For example, f:\a\b is a folder that moves the entire B folder to d:\b. Hopefully it can be done by a function. If the function is MoveFolder (CString s,cstring D), C and D hold the path of the original folder and the destination folder respectively. Hope that the expert can give the complete code, if the parameters are not CString, can give the conversion code!! BOOL MoveFolder (CString strsrcpath, CString strdestpath) {Strsrcpath+ = _t (' /');//Note Here must add _t (' ""), do not write _t ("" ");Shfileopstruct si = {0}; Si.hwnd=:: GetDesktopWindow (); Si.wfunc=fo_copy; Si.pfrom=Strsrcpath; Si.pto=Strdestpath; Si.fflags= Fof_noconfirmation |Fof_noconfirmmkdir; return 0= = SHFileOperation (&si);}----------voidmycopydirectory (CString source, CString target) {createdirectory (target,null);//Create a destination folder//AfxMessageBox ("Create folder" +target); CFileFind Finder;          CString path; Path. Format ("%s/*.*", source);      AfxMessageBox (path); BOOLBworking =Finder.      FindFile (path);  while(bworking) {bworking=Finder.          FindNextFile (); AfxMessageBox (finder.          GetFileName ()); if(finder.) Isdirectory () &&!finder. Isdots ()) {//is a folder and the name does not contain. Or. Mycopydirectory (finder. GetFilePath (), target+"/"+finder. GetFileName ());//recursively Create folders + "/" +finder. GetFileName ()        }          Else{//is the file is copied directly//AfxMessageBox ("Copy File" +finder. GetFilePath ());//+finder. GetFileName ()CopyFile (finder. GetFilePath (), target+"/"+Finder.          GetFileName (), FALSE); }      }  }

MFC finds files and copies them via TXT-memo

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.