Copy files to a specified path in batches

Source: Internet
Author: User

Copying files to a specified path in batches (there are still many problems with the code written by myself. I hope you can give me some advice and paste the code below, the MFC project, and all the code I write have my comments, the rest is generated by the system itself, so that beginners like me can study it easily)

// Copyfiledlg. h: header file
//

# Pragma once

// ========== 2011-1-8 Liu. W start ============
// If you want to use vector, add codes as follow
# Include <vector>
Using namespace STD;
// ========== 2011-1-8 Liu. w end ============

 

// Ccopyfiledlg Dialog
Class ccopyfiledlg: Public cdialog
{
// Construction
Public:
Ccopyfiledlg (cwnd * pparent = NULL); // standard Constructor

 

// ========== 2011-1-8 Liu. W start ============
// Declare Function
Bool getfilecontext (cstring cstrfilename, vector <cstring> & vectmp );

Bool copyfile (vector <cstring> vecfile, cstring strsrcpath, cstring strdstpath );
// ========== 2011-1-8 Liu. w end ============

 

// Dialog data
Enum {IDD = idd_copyfile_dialog };

Protected:
Virtual void dodataexchange (cdataexchange * PDX); // DDX/DDV support
 

// Implementation
Protected:
Hicon m_hicon;

// Generated message map Functions
Virtual bool oninitdialog ();
Afx_msg void onsyscommand (uint NID, lparam );
Afx_msg void onpaint ();
Afx_msg hcursor onquerydragicon ();
Declare_message_map ()
Public:
Afx_msg void onbnclickedbutton1 ();
Public:
Afx_msg void onbnclickedbutton2 ();
PRIVATE:
Vector <cstring> vecfilecontext;
Vector <cstring> vecfilepath;
Afx_msg void onbnclickedbutton3 ();
Afx_msg void onbnclickedbutton4 ();
Afx_msg void onbnclickedbutton5 ();
};

 

 

//************************************** ************************************

 

// Copyfiledlg. cpp: implementation file
//

# Include "stdafx. H"
# Include "copyfile. H"
# Include "copyfiledlg. H"

# Include <windows. h>
# Include <iostream>
# Include "shlwapi. H"

# Ifdef _ debug
# Define new debug_new
# Endif

// Caboutdlg Dialog used for app about

Class caboutdlg: Public cdialog
{
Public:
Caboutdlg ();

// Dialog data
Enum {IDD = idd_aboutbox };

Protected:
Virtual void dodataexchange (cdataexchange * PDX); // DDX/DDV support

// Implementation
Protected:
Declare_message_map ()
};

Caboutdlg: caboutdlg (): cdialog (caboutdlg: IDD)
{
}

Void caboutdlg: dodataexchange (cdataexchange * PDX)
{
Cdialog: dodataexchange (PDX );
}

Begin_message_map (caboutdlg, cdialog)
End_message_map ()

// Ccopyfiledlg Dialog

 

Ccopyfiledlg: ccopyfiledlg (cwnd * pparent/* = NULL */)
: Cdialog (ccopyfiledlg: IDD, pparent)
{
M_hicon = afxgetapp ()-> loadicon (idr_mainframe );
}

Void ccopyfiledlg: dodataexchange (cdataexchange * PDX)
{
Cdialog: dodataexchange (PDX );
}

Begin_message_map (ccopyfiledlg, cdialog)
On_wm_syscommand ()
On_wm_paint ()
On_wm_querydragicon ()
//} Afx_msg_map
On_bn_clicked (idc_button1, & ccopyfiledlg: onbnclickedbutton1)
On_bn_clicked (idc_button2, & ccopyfiledlg: onbnclickedbutton2)
On_bn_clicked (idc_button3, & ccopyfiledlg: onbnclickedbutton3)
On_bn_clicked (idc_button4, & ccopyfiledlg: onbnclickedbutton4)
On_bn_clicked (idc_button5, & ccopyfiledlg: onbnclickedbutton5)
End_message_map ()

// Ccopyfiledlg message handlers

Bool ccopyfiledlg: oninitdialog ()
{
Cdialog: oninitdialog ();

// Add "about..." menu item to system menu.

// Idm_aboutbox must be in the system command range.
Assert (idm_aboutbox & 0xfff0) = idm_aboutbox );
Assert (idm_aboutbox <0xf000 );

Cmenu * psysmenu = getsystemmenu (false );
If (psysmenu! = NULL)
{
Cstring straboutmenu;
Straboutmenu. loadstring (ids_aboutbox );
If (! Straboutmenu. isempty ())
{
Psysmenu-> appendmenu (mf_separator );
Psysmenu-> appendmenu (mf_string, idm_aboutbox, straboutmenu );
}
}

// Set the icon for this dialog. The framework does this automatically
// When the application's main window is not a dialog
Seticon (m_hicon, true); // set big icon
Seticon (m_hicon, false); // set small icon

// Todo: add extra initialization here
 
// ========== 2011-1-8 Liu. W start ============
Cstring strinipath = l "d: // new_jp_inputdata // cpicture // PIC //";
Getdlgitem (idc_edit_picture)-> setwindowtext (strinipath );

Cstring strdestinipath = l "d: // cpicture //";
Getdlgitem (idc_edit_dest)-> setwindowtext (strdestinipath );

Cstring strsetinipath = l "d: // cpicture // list.txt ";
Getdlgitem (idc_edit_source)-> setwindowtext (strsetinipath );
// ========== 2011-1-8 Liu. w end ============

Return true; // return true unless you set the focus to a control
}

Void ccopyfiledlg: onsyscommand (uint NID, lparam)
{
If (NID & 0xfff0) = idm_aboutbox)
{
Caboutdlg dlgabout;
Dlgabout. domodal ();
}
Else
{
Cdialog: onsyscommand (NID, lparam );
}
}

// If you add a Minimize button to your dialog, you will need the code below
// To draw the icon. For MFC applications using the document/view model,
// This is automatically done for you by the framework.

Void ccopyfiledlg: onpaint ()
{
If (isiconic ())
{
Cpaintdc DC (this); // device context for painting

Sendmessage (wm_iconerasebkgnd, reinterpret_cast <wparam> (DC. getsafehdc (), 0 );

// Center icon in client rectangle
Int cxicon = getsystemmetrics (sm_cxicon );
Int cyicon = getsystemmetrics (sm_cyicon );
Crect rect;
Getclientrect (& rect );
Int x = (rect. Width ()-cxicon + 1)/2;
Int y = (rect. Height ()-cyicon + 1)/2;

// Draw the icon
DC. drawicon (X, Y, m_hicon );
}
Else
{
Cdialog: onpaint ();
}
}

// The system callthis function to obtain the cursor to display while the user drags
// The minimized window.
Hcursor ccopyfiledlg: onquerydragicon ()
{
Return static_cast }

// ========== 2011-1-8 Liu. W start ============
Bool blnfileexist (cstring cstrfilename)
{
Cfilefind pfile;
If (pfile. findfile (cstrfilename, 0 ))
{
Pfile. Close ();
Return true;
}
Else
{
Return false;
}
}

Bool ccopyfiledlg: getfilecontext (cstring cstrfilename, vector <cstring> & vectmp)
{
Cstring strline;
Cstring strmsg = l "set file not found, please check ";
Cstdiofile file;
 
Vectmp. Clear ();

If (! Blnfileexist (cstrfilename ))
{
MessageBox (strmsg );
Return false;
}
Else // file exists, try to read it's context by row into a vector
{
If (file. Open (cstrfilename, cfile: moderead ))
{
While (file. readstring (strline ))
{
Vectmp. push_back (strline );
}
File. Close ();
}
}
Return true;
}

Void ccopyfiledlg: onbnclickedbutton1 ()
{
// Todo: add your control notification handler code here

Cstring cstrfilename;
Getdlgitem (idc_edit_source)-> getwindowtext (cstrfilename );

// Get picture names from setting file
If (! Getfilecontext (cstrfilename, vecfilecontext ))
{
Return;
}

// Change folder automatically according setting file's full name
Int NPOs = cstrfilename. reversefind ('//');
Cstring strtmp = cstrfilename. Left (NPOs + 1 );
Getdlgitem (idc_edit_dest)-> setwindowtext (strtmp );

If (vecfilecontext. Size ()> 0)
{
MessageBox (L "load setting file successfully ");
Getdlgitem (idc_button2)-> enablewindow ();
}
}

Void ccopyfiledlg: onbnclickedbutton2 ()
{
// Todo: add your control notification handler code here
Int idx = 0;

Cstring strsrcpath;
Cstring strdstpath;
Getdlgitem (idc_edit_picture)-> getwindowtext (strsrcpath );
Getdlgitem (idc_edit_dest)-> getwindowtext (strdstpath );

If (! : Pathfileexists (strdstpath) // DEST folder not exist
{
// Create DEST folder
: Createdirectory (strdstpath, null );
}

If (! : Pathfileexists (strsrcpath) // picture folder not exist
{
MessageBox (L "picture folder not found, check again ");
Return;
}

// To check path is legal or not/* D:/path/is legal
If (strsrcpath. Right (1 )! = _ T ("//"))
{
Strsrcpath = strsrcpath + _ T ("//");
}

If (strdstpath. Right (1 )! = _ T ("//"))
{
Strdstpath = strdstpath + _ T ("//");
}

Copyfile (vecfilecontext, strsrcpath, strdstpath );
}

Bool ccopyfiledlg: copyfile (vector <cstring> vecfile, cstring strsrcpath, cstring strdstpath)
{
Cstdiofile filecopy;
Cstring strsrcpathtmp, strdstpathtmp;
 
Int idx = 0;
For (idx = 0; idx <vecfile. Size (); idx ++)
{
Strsrcpathtmp = strsrcpath + vecfile. At (idx );
Strdstpathtmp = strdstpath + vecfile. At (idx );

Try
{
Cfile: Rename (strsrcpathtmp, strdstpathtmp );
}
Catch (cfileexception, E)
{
# Ifdef _ debug
Afxdump <"file" <strsrcpathtmp <"not found, cause ="
<E-> m_cause <"/N ";
# Endif
MessageBox (strsrcpathtmp + L "not found ");
}
End_catch

}
Return true;
}

Int callback browsecallbackproc (hwnd,
Uint umsg,
Lparam LP,
Lparam pdata)
{
Return 0;
}

Void ccopyfiledlg: onbnclickedbutton3 ()
{
// Todo: add your control notification handler code here
Cfiledialog DLG (true, l "TXT", null, ofn_hidereadonly | ofn_overwriteprompt, null, null, 0 );
DLG. domodal ();
Cstring cstrfilename = DLG. getpathname ();
If (1)
{
Getdlgitem (idc_edit_source)-> setwindowtext (cstrfilename );
}
}

Void ccopyfiledlg: onbnclickedbutton4 ()
{
// Todo: add your control notification handler code here
Lpmalloc pmalloc;
If (: shgetmalloc (& pmalloc) = noerror)
{
Browseinfo Bi;
Tchar pszbuffer [max_path];
Lpitemidlist pidl;
Bi. hwndowner = This-> m_hwnd;
Bi. pidlroot = NULL;
Bi. pszdisplayname = pszbuffer;
Bi. lpsztitle = _ T ("choose folder ...");
Bi. ulflags = bif_returnfsancestors | bif_returnonlyfsdirs;
Bi. lpfn = browsecallbackproc;
Bi. lparam = 0;
Bi. iimage = 0;
If (pidl =: shbrowseforfolder (& BI ))! = NULL)
{
If (: shgetpathfromidlist (pidl, pszbuffer ))
{
Getdlgitem (idc_edit_dest)-> setwindowtext (pszbuffer );
}
Else
Return;
Pmalloc-> free (pidl );
}
Else
Return;
Pmalloc-> release ();

}
Updatedata (false );
}

Void ccopyfiledlg: onbnclickedbutton5 ()
{
// Todo: add your control notification handler code here
Lpmalloc pmalloc;
If (: shgetmalloc (& pmalloc) = noerror)
{
Browseinfo Bi;
Tchar pszbuffer [max_path];
Lpitemidlist pidl;
Bi. hwndowner = This-> m_hwnd;
Bi. pidlroot = NULL;
Bi. pszdisplayname = pszbuffer;
Bi. lpsztitle = _ T ("choose folder ...");
Bi. ulflags = bif_returnfsancestors | bif_returnonlyfsdirs;
Bi. lpfn = browsecallbackproc;
Bi. lparam = 0;
Bi. iimage = 0;
If (pidl =: shbrowseforfolder (& BI ))! = NULL)
{
If (: shgetpathfromidlist (pidl, pszbuffer ))
{
Getdlgitem (idc_edit_picture)-> setwindowtext (pszbuffer );
}
Else
Return;
Pmalloc-> free (pidl );
}
Else
Return;
Pmalloc-> release ();
}
Updatedata (false );
}
// ========== 2011-1-8 Liu. w end ============

 

 

 

 

 

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.