批量移動文件(八)
本文講述如何批量移動文件到指定目錄。
1. 創建一個對話框工程:BatchMoveFile。
2. 添加一個List Control控件,變量名稱爲:m_filelist。
3. 添加一個按鈕“添加文件”,代碼如下:
CFileDialoglog(TRUE,"文件","*.*",OFN_HIDEREADONLY|
OFN_ALLOWMULTISELECT,"FILE(*.*)|*.*||",NULL);
if(log.DoModal() == IDOK)
{
POSITIONpos = log.GetStartPosition();
while(pos != NULL)
{
CStringpathname = log.GetNextPathName(pos);
m_filelist.InsertItem(m_filelist.GetItemCount(),pathname);
}
}
4. 添加一個函數,如下:
CStringCBatchMoveFileDlg::GetNameFromPath(CStringpath)
{
CStringstrright;
intpos=path.Find("//");
while(pos>0)
{
path=path.Right(path.GetLength()-1-pos);
pos=path.Find("//");
}
returnpath;
}
5. 添加一個按鈕“移動到”,代碼如下:
BROWSEINFObi;
charbuffer[MAX_PATH];
ZeroMemory(buffer,MAX_PATH);
bi.hwndOwner=GetSafeHwnd();
bi.pidlRoot=NULL;
bi.pszDisplayName=buffer;
bi.lpszTitle="選擇一個文件夾";
bi.ulFlags=BIF_EDITBOX;
bi.lpfn=NULL;
bi.lParam=0;
bi.iImage=0;
LPITEMIDLISTpList = NULL;
if((pList = SHBrowseForFolder(&bi)) != NULL)
{
charpath[MAX_PATH];
ZeroMemory(path,MAX_PATH);
SHGetPathFromIDList(pList,path);
for(inti=0;i<m_filelist.GetItemCount();i++)
{
CStringpathtemp;
pathtemp.Format("%s//%s",path,GetNameFromPath(m_filelist.GetItemText(i,0)));
::MoveFile(m_filelist.GetItemText(i,0),pathtemp);
}
AfxMessageBox("移動文件完成");
}
完成,編譯運行即可。