批量移动文件(八)
本文讲述如何批量移动文件到指定目录。
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("移动文件完成");
}
完成,编译运行即可。