MFC文件夹操作

遍历文件夹里的照片:

在这里插入代码片CString CCameraControlDlg::BroseAllFile(CString picPath)
{
	CRect PicRc;
	CString filename = _T("");
	CString fullname = _T("");
	CString fdPath, strTmp;
	int numTemp = 0;
	CFileFind find;
	BOOL IsFind = find.FindFile(picPath + _T("/*.*"));

	while (IsFind)
	{

		IsFind = find.FindNextFile();
		if (find.IsDots())//一定要加这个才能循环遍历
		{
			continue;
		}
		else
		{
			fdPath = find.GetFilePath();
			strTmp = fdPath.Right(4);//CString很方便
			strTmp.MakeLower();
			if (".jpg" == strTmp)//指定格式后缀
			{
				filename = find.GetFileName();
				fullname = picPath +"\\" + filename;
				m_string.Add(fullname);
				numTemp++;
			}
		}
		
	}
	find.Close();
	picNum = numTemp;
	return fullname;
}
`
移动文件(将当前项目文件移到指定文件夹):

void CCameraControlDlg::movephoto()
{
CString fdPath;
CString num;
CString strFloderPath;//strFloderPath = _T(“E://EDSDK3610CD(3.6.1)//Windows//Sample//VC//CameraControl”);
//CString strDataPath = strFloderPath + “*.*”;
CString strTmp;

CFileFind find;//MFC的文件查找类
strFloderPath = "\*.*";//项目工程的相对路径,MFC里要加* *
BOOL bf = find.FindFile(strFloderPath);
while (bf)//循环遍历文件夹下的文件
{

	bf = find.FindNextFile();
	if (!find.IsDots())
	{

		fdPath = find.GetFilePath();
		strTmp = fdPath.Right(4);//CString很方便
		strTmp.MakeLower();//将字符串变为小写
		if (".jpg" == strTmp)//指定格式后缀
		{
			CTime tm = CTime::GetCurrentTime();
			CString PicName;//图片名称
			CString PathName;//文件夹名称
			CFile file;
			CFileException fileException;
			//PathName.Format(_T("D:\\%d_%d_%d"), tm.GetYear(), tm.GetMonth(), tm.GetDay());
			//photo_path = PathName+"\\";
			PicName.Format(_T("%d%d%d%d%d%d.JPG"),tm.GetYear(),tm.GetMonth(), tm.GetDay(), tm.GetHour(), tm.GetMinute(), tm.GetSecond());//通过当前时间命名
			//num.Format("%d", Photo_num );
			//strEndrPath += num;
			CString strEndrPath;
			strEndrPath = photo_path + "\\" + PicName;//目标文件的路径
			MoveFile(fdPath, strEndrPath);//把文件从fdPath 移到strEndrPath
			
		}
	}
}
find.Close();

}

电脑磁盘地址选择:

void CCameraControlDlg::OnBnClickedBtncheck()//选择图片存储地址
{

char szPath[MAX_PATH];     //存放选择的目录路径 


ZeroMemory(szPath, sizeof(szPath));

BROWSEINFO bi;
bi.hwndOwner = m_hWnd;
bi.pidlRoot = NULL;
bi.pszDisplayName = szPath;
bi.lpszTitle = "请选择图片目录:";
bi.ulFlags = 0;
bi.lpfn = NULL;
bi.lParam = 0;
bi.iImage = 0;
//弹出选择目录对话框
LPITEMIDLIST lp = SHBrowseForFolder(&bi);

if (lp && SHGetPathFromIDList(lp, szPath))
{
	
	str.Format("%s", szPath);//获得选中的磁盘
	PathSaveName = str;
	SetDlgItemText(IDC_FILEPATH, szPath);
	//SetTimer(1, 6000, NULL);//定时读取图片时间间隔  6000毫秒

}
else
	AfxMessageBox("无效的目录,请重新选择");

}

遍历文件夹的文件,获取文件名和文件修改时间
// 遍历文件夹,获取文件信息
void TravelFolder(CString strDir)
{
    // 文件当前目录
    TCHAR Buffer[MAX_PATH];
    DWORD dwRet = GetCurrentDirectory(MAX_PATH, Buffer);
    CString strCurrent(Buffer);

    CFileFind filefind;                                         //声明CFileFind类型变量

    CString strWildpath = strDir + _T("\\*.*");     //所有文件都列出。

    if(filefind.FindFile(strWildpath, 0))                    //开始检索文件
    {
        BOOL bRet = TRUE;

        while(bRet)
        {
            bRet = filefind.FindNextFile();                 //枚举一个文件            

            if(filefind.IsDots())                                 //如果是. 或 .. 做下一个
            {
                continue;
            }

            // 文件名 begin
            CString strFileName = filefind.GetFileName();
            // 文件名 end

            // 文件修改时间 begin
            FILETIME   filetime;
            FILETIME   localtime;
            SYSTEMTIME systemtime;
            filefind.GetLastWriteTime(&filetime);

            FileTimeToLocalFileTime(&filetime, &localtime); //换成本地时间

            FileTimeToSystemTime(&localtime, &systemtime);  //换成系统时间格式

            CString strTime = _T("");
            strTime.Format(_T("%04d%02d%02d%02d%02d%02d"),
                systemtime.wYear, systemtime.wMonth, systemtime.wDay,
                systemtime.wHour, systemtime.wMinute, systemtime.wSecond);
            // 文件修改时间 end            

            if(!filefind.IsDirectory())                          //不是子目录,把文件名打印出来
            {
                CString strWrite = _T("");
                strWrite += strFileName;
                strWrite += _T("\t");
                strWrite += strTime;
                strWrite +=  + _T("\r\n");
                TRACE(strWrite);
            }
            else                                                   //如果是子目录,递归调用该函数
            {
                CString strNewDir = strDir + CString(_T("\\")) + filefind.GetFileName();

                TravelFolder(strNewDir);//递归调用该函数打印子目录里的文件
            }
        }

        filefind.Close();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值