MFC file operation - obtain all file (folder) paths under the specified folder and delete all files under the specified folder

Table of contents

1. Get all file paths under the specified folder

 2. Delete all files under the specified folder

3.MFC’s CString string operation

4. Conversion between MFC's Int type and Htuple type data

 5. Function applications mentioned above

6.MFC and Halcon joint programming to obtain halcon exceptions

7.MFC gets the folder path under the specified path

          ①. How to obtain folder path

②. Open the file - how to obtain the file path


1. Get all file paths under the specified folder

//读取文件夹下的所有文件路径
void getFiles(CString path, vector<CString>& files)
{
	CFileFind find;
	BOOL IsFind = find.FindFile(path + _T("/*.*"));
	while (IsFind)
	{
		IsFind = find.FindNextFile();
		if (find.IsDots())
		{
			continue;
		}
		else
		{
			CString filename = _T("");
			CString fullname = _T("");
			filename = find.GetFileName();
			fullname = path + filename;
			files.push_back(fullname);
		}
	}
}

 2. Delete all files under the specified folder

//删除指定文件夹下面所有文件
void CDirectory::DeleteDirectory(const CString &strPath)

{

	CFileFind tempFind;

	TCHAR sTempFileFind[MAX_PATH] = { 0 };

	wsprintf(sTempFileFind, _T("%s\\*.*"), strPath);

	BOOL IsFinded = tempFind.FindFile(sTempFileFind);

	while (IsFinded) 

	{ 

		IsFinded = tempFind.FindNextFile(); 



		if (!tempFind.IsDots()) 

		{ 

			TCHAR sFoundFileName[200] = { 0 }; 

			_tcscpy(sFoundFileName, tempFind.GetFileName().GetBuffer(200)); 



			if (tempFind.IsDirectory()) 

			{ 

				TCHAR sTempDir[200] = { 0 }; 

				wsprintf(sTempDir, _T("%s\\%s"),strPath, sFoundFileName); 

				DeleteDirectory(sTempDir); 

			} 

			else 

			{ 

				TCHAR sTempFileName[200] = { 0 }; 

				wsprintf(sTempFileName, _T("%s\\%s"), strPath, sFoundFileName); 

				DeleteFile(sTempFileName); 

			} 

		} 

	} 


	tempFind.Close(); 

//若不想删除当前文件夹,则注释掉下面一句
	if(!RemoveDirectory(strPath))

		return false;


	return true;

}

3.MFC’s CString string operation

CString strModelpath, strMax, strMin;
strModelpath.Format(("./printcheck_model/model-%d.shm"), hv_i[0].I());
strMax.Format(("./printcheck_model/ModelMax-%d.bmp"), hv_i[0].I());
strMin.Format(("./printcheck_model/ModelMin-%d.bmp"), hv_i[0].I());

4. Conversion between MFC's Int type and Htuple type data

int k = (hv_Int[hv_Index]).I();
CString sr = Files[(hv_Int[hv_Index]).I()];
ReadImage(&ho_Image, HTuple(Files[(hv_Int[hv_Index]).I()]));

 5. Function applications mentioned above

vector<CString> Files;
getFiles("D:/Desktop/文字缺损检测/产品0(√)/文字部分好品/", Files);
DeleteDirectory("./printcheck_model");

6.MFC and Halcon joint programming to obtain halcon exceptions

try
{
	ReadShapeModel(strModelpath.GetBuffer(), &hv_ModelID);

}
	catch (const HException& H)
{
	HString str = H.ErrorText();
	string a = string(str.ToLocal8bit());
}

7.MFC gets the folder path under the specified path

 Folder paths and file paths are different. The former can only select a folder to obtain the path of the folder, but the latter must open a specified file, such as a .jpg image, in order to obtain the file path. . So which program to use depends on your needs.

①. How to obtain folder path

CString m_strFileOut = _T("");  //初始化适应Unicode
 TCHAR szSelected[MAX_PATH];//用来存放文件夹路径  
 BROWSEINFO bi;  
 LPITEMIDLIST pidl;  
 bi.hwndOwner = this->m_hWnd;  
 bi.pidlRoot = NULL;  
 bi.pszDisplayName = szSelected;  
 bi.lpszTitle = _T("选择输出文件路径");  
 bi.ulFlags = BIF_RETURNONLYFSDIRS;  
 bi.lpfn = NULL;  
 bi.lParam = NULL;  
 bi.iImage = NULL;  
 if((pidl = SHBrowseForFolder(&bi)) != NULL)  
 {  
  if(SUCCEEDED(SHGetPathFromIDList(pidl, szSelected))) //得到文件夹的全路径,不要的话,只得本文件夹名  
  {  
   m_strFileOut = szSelected;  	//获得文件夹的全路径
  }  
 }  

②. Open the file - how to obtain the file path

                CString filePath;
				CFileDialog dlg(TRUE, _T("*.bmp"), "", OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY, "image files (*.bmp; *.jpg) |*.bmp;*.jpg|All Files (*.*)|*.*||", NULL);
				char title[] = { "Open model-Image" };
				dlg.m_ofn.lpstrTitle = title;
				if (dlg.DoModal() == IDOK)
				{
					filePath = dlg.GetPathName();//获取当前的路径
					m_sModel_b.modelimg_path = filePath;
					//读入图像
					//手动选择1张图像创建形状模板
					ReadImage(&ho_Image, HTuple(m_sModel_b.modelimg_path));
					DispObj(ho_Image, m_lWindowHandle);
					AfxMessageBox("模板参考图像读取完成!");
				}

Guess you like

Origin blog.csdn.net/weixin_50016546/article/details/131833376