MFC 文件与文件夹相关操作

41 篇文章 7 订阅

    计算指定文件夹的总大小

    实现函数(注意该函数是写在对话框类中的,所以需要在类中做声明什么的, 这里没有给出)

DWORD64  CMFCGETFOLDERSIZEDlg::GetFolderSize(CString szPath)//获得文件夹大小,返回值大小单位为M
{
	CString szFileFilter = szPath + L"\\*.*";
	HANDLE hFind = NULL;
	WIN32_FIND_DATA fileinfo;//保存文件信息的结构体,个人理解有可能是文件,也有可能是目录
	DWORD64    dwSize = 0;

	hFind = FindFirstFile(szFileFilter, &fileinfo);
	do
	{
		if (fileinfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)//如果目标为文件夹即目录则为真
		{
			CString myfilename = fileinfo.cFileName;
			if (myfilename != "." && myfilename !="..")//“.”表示当前目录,“..”表示上一级目录,只有在非两者的情况下才意味着该对象为我们指定文件夹的子文件夹
			{
				CString sztmp;
				sztmp = szPath +L"\\"+ fileinfo.cFileName;
				dwSize = dwSize + GetFolderSize(sztmp);//递归得到子文件夹大小
			}
		}
		else
		{
			dwSize += fileinfo.nFileSizeLow;
		}
	
	} while (FindNextFile(hFind, &fileinfo));
	FindClose(hFind);
	return dwSize;
}
    调用该函数

void CMFCGETFOLDERSIZEDlg::OnBnClickedOk()
{
	DWORD64  myfoldersize = GetFolderSize(L"E:\\testfolder");
	CString mylong;
	mylong.Format(L"文件夹大小为:%ld", myfoldersize);
	MessageBox(mylong);
}

    删除文件夹(包括非空文件夹)

void CMFCGETFOLDERSIZEDlg::myDeleteDirectory(CString directory_path)   //删除一个文件夹下的所有内容
{
	CFileFind finder;
	CString path;
	path = directory_path + L"\\*.*";
	BOOL bWorking = finder.FindFile(path);
	while (bWorking)
	{
		bWorking = finder.FindNextFile();
		if (finder.IsDirectory() && !finder.IsDots())//处理文件夹
		{
			myDeleteDirectory(finder.GetFilePath());//递归删除文件
			RemoveDirectory(finder.GetFilePath());//删除已被删除文件的文件夹
		}
		else//处理文件
		{
			DeleteFile(finder.GetFilePath());
		}
	}
}
    调用该函数

void CMFCGETFOLDERSIZEDlg::OnBnClickedButtonDelete()
{
	myDeleteDirectory(L"E:\\testfolder");//删除文件夹下所有文件  
	RemoveDirectory(L"E:\\testfolder");//删除该空文件夹  
}

    获得指定文件夹下的子文件夹名称(无递归,只获得一层)函数

void CMFCGETFOLDERSIZEDlg::GetFolderSonName(CString directory_path)//获得文件夹下的子文件夹名称
{
	CFileFind finder;
	CString path;
	path = directory_path + L"\\*.*";
	BOOL bWorking = finder.FindFile(path);
	while (bWorking)
	{
		bWorking = finder.FindNextFile();
		if (finder.IsDirectory() && !finder.IsDots())//处理文件夹
		{
			CString SonFolderName = finder.GetFileName();
			MessageBox(SonFolderName);
		}
	}
}

    获得指定文件夹下的所有文件名称函数

void CFindTheSameFileDlg::BrowseCurrentAllFile(CString strDir)
{
	if (strDir == _T(""))
	{
		return;
	}
	else
	{
		if (strDir.Right(1) != L"\\")
			strDir += L"\\";
		strDir = strDir + L"*.*";
	}
	CFileFind finder;
	CString strPath;
	BOOL bWorking = finder.FindFile(strDir);
	while (bWorking)
	{
		bWorking = finder.FindNextFile();
		strPath = finder.GetFilePath();
		if (finder.IsDirectory() && !finder.IsDots())
		{
			BrowseCurrentAllFile(strPath); //递归调用
		}  
		else if (!finder.IsDirectory() && !finder.IsDots())
		{
			CString SonFolderName = finder.GetFileName();//获得文件名称
			MessageBoxW(SonFolderName);
		}
	}
}

    调用该函数

void CFindTheSameFileDlg::OnBnClickedButtonFine()
{
	BrowseCurrentAllFile(L"D:\\重复文件查找\\FindTheSameFile\\FindTheSameFile\\cha");
}

    获取系统时间创建文件夹

void CMFCGETFOLDERSIZEDlg::GetSystimeCreatFolder()
{
	SYSTEMTIME sys;
	GetLocalTime(&sys);//获得系统时间
	CString myname;
	myname.Format(L"%4d%02d%02d", sys.wYear, sys.wMonth, sys.wDay);
	CFileFind filefind;
	CString strPathname = L"d:\\" + myname;
	if (filefind.FindFile(strPathname))//判断文件夹是否存在,不存在则创建
		MessageBox(L"文件存在");
	else
	{
		MessageBox(L"文件不存在,创建该文件夹");
		CreateDirectory(strPathname,NULL);
	}	
}
    调用该函数

void CMFCGETFOLDERSIZEDlg::OnBnClickedButton3()
{
	GetSystimeCreatFolder();
}

    判断指定路径文件是否存在

if (PathFileExists(mFilePath))
{
	MessageBox(L"存在");
}
else
{
	MessageBox(L"不存在");
}






  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MFC打开文件文件夹 一、利用API实现打开文件对话框和利用MFC来实现打开文件对话框。 方法一:API部分: // OPENFILENAME ofn; // TCHAR szFile[MAX_PATH]; // ZeroMemory(&ofn,sizeof(ofn)); // ofn.lStructSize = sizeof(ofn); // ofn.lpstrFile = szFile; // ofn.lpstrFile[0] = TEXT('/0'); // ofn.nMaxFile = sizeof(szFile); // ofn.lpstrFilter = TEXT("all/0*.*/0jpg/0*.jpg/0bmp/0*.bmp/0"); //定义三个选项,all,text和exe // ofn.nFilterIndex = 1; //定义首选项是哪一个 // ofn.lpstrFileTitle = NULL; // ofn.nMaxFileTitle = 0; // ofn.lpstrInitialDir = NULL; // ofn.Flags = OFN_EXPLORER |OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; // if(GetOpenFileName(&ofn)) // { // ::SetDlgItemText(this->m_hWnd,IDC_EDIT1,szFile); // } 方法二、MFC实现 // CFileDialog dlg(TRUE, NULL, NULL, // OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, // NULL); // // if(dlg.DoModal() == IDOK) // { // szFilename = dlg.GetPathName(); // ::MessageBox(NULL,szFilename,NULL,MB_OK); // } 注意:打开文件的方式和打开文件夹的方式是不同的。打开文件的方式是不断的打开直到出现末端为文件,否则遇到文件夹还要继续打开。而打开文件夹则是只要选择到一个路径的文件夹就打开。 下面的这种方式是打开文件夹MFC实现。 static TCHAR strDirName[MAX_PATH]; BROWSEINFO bi; CString szString = TEXT("选择一个源文件文件夹"); bi.hwndOwner = ::GetFocus(); bi.pidlRoot = NULL; bi.pszDisplayName = strDirName; bi.lpszTitle = szString; bi.ulFlags = BIF_BROWSEFORCOMPUTER | BIF_DONTGOBELOWDOMAIN | BIF_RETURNONLYFSDIRS; bi.lpfn = NULL; bi.lParam = 0; bi.iImage = 0; LPITEMIDLIST pItemIDList = ::SHBrowseForFolder(&bi); if(pItemIDList == NULL) { return ; } ::SHGetPathFromIDList(pItemIDList, strDirName); CString str = strDirName; if(str != "" && str.Right(1) != '//') str += '//'; ::SetDlgItemText(this->m_hWnd,IDC_EDIT1,str);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值