当前位置: 移动技术网 > IT编程>脚本编程>Python > 选择根目录,自动给其他编辑框添加根目录下的文件及文件夹路径

选择根目录,自动给其他编辑框添加根目录下的文件及文件夹路径

2020年07月13日  | 移动技术网IT编程  | 我要评论

就如下图,我只选择了根目录,其他的编辑框,下拉框都是自动选取的,省了一个一个选的时间
在这里插入图片描述

我的根目录下的文件结构如下

在这里插入图片描述在这里插入图片描述

代码如下

void CVideoCutDlg::OnBnClickedRootdir()
{
	// TODO: 在此添加控件通知处理程序代码
	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;  	//获得文件夹的全路径
			CString rootDir;
			rootDir = m_strFileOut;
			USES_CONVERSION;
			char* fileName2 = T2A(rootDir);//读取需要分离的图片的路径CString->char*
			std::vector <CString>  rootNames;//用来保存图片的名字	
			readImgNamefromFile(fileName2, rootNames);//读入图片名字到数组
			 testerNum= rootNames[0].Mid(3,rootNames[0].Find(_T("-"))-3);
			 CString select= rootNames[0].Mid( rootNames[0].Find(_T(".")) - 6, 2);
			 CString cutPath = _T("D:\\VideoSaveFolder");
	 		 CString cutP= cutPath+_T("\\")+_T("sub")+testerNum;
			 char* fileName3 = T2A(cutP);//读取需要分离的图片的路径CString->char*
			 std::vector <CString>  cutPathNames;//用来保存图片的名字	
			 readImgNamefromFile(fileName3, cutPathNames);//读入图片名字到数组
			 if (select == _T("01"))
			 {
				 m_Box.SetCurSel(0);
				 SetDlgItemText(IDC_EDIT2, cutP+_T("\\")+cutPathNames[2]);
			 }
			 else if (select == _T("02"))
			 {
				 m_Box.SetCurSel(1);
				 SetDlgItemText(IDC_EDIT2, cutP + _T("\\") + cutPathNames[4]);
			 }
			 else if (select == _T("03"))
			 {
				 m_Box.SetCurSel(2);
				 SetDlgItemText(IDC_EDIT2, cutP + _T("\\") + cutPathNames[0]);
			 }
			 else if (select == _T("04"))
			 {
				 m_Box.SetCurSel(3);
				 SetDlgItemText(IDC_EDIT2, cutP + _T("\\") + cutPathNames[1]);
			 }
			 else
			 {
				 m_Box.SetCurSel(4);
				 SetDlgItemText(IDC_EDIT2, cutP + _T("\\") + cutPathNames[3]);
			 }
			if (rootNames.size() == 3)
			{
				SetDlgItemText(IDC_EDIT6, rootDir + _T("\\") + rootNames[0]);
				SetDlgItemText(IDC_EDIT8, rootDir + _T("\\") + rootNames[2]);
				SetDlgItemText(IDC_EDIT1, rootDir + _T("\\") + rootNames[1]);
				SetDlgItemText(IDC_EDIT7, _T(""));
				SetDlgItemText(IDC_EDIT4, testerNum);
			}
			if (rootNames.size() == 4)
			{
				SetDlgItemText(IDC_EDIT6, rootDir + _T("\\") + rootNames[0]);
				SetDlgItemText(IDC_EDIT8, rootDir + _T("\\") + rootNames[2]);
				SetDlgItemText(IDC_EDIT1, rootDir + _T("\\") + rootNames[1]);
				SetDlgItemText(IDC_EDIT7, rootDir + _T("\\") + rootNames[3]);
				SetDlgItemText(IDC_EDIT4, testerNum);
			}
		}
	}
	SetDlgItemText(IDC_EDIT9, m_strFileOut);
}
void readImgNamefromFile(char* fileName, vector <CString> &imgNames)

{

	// vector清零 参数设置

	imgNames.clear();

	WIN32_FIND_DATA file;

	int i = 0;

	char tempFilePath[MAX_PATH + 1];

	char tempFileName[50];

	// 转换输入文件名

	sprintf_s(tempFilePath, "%s/*", fileName);

	// 多字节转换

	WCHAR   wstr[MAX_PATH] = { 0 };

	MultiByteToWideChar(CP_ACP, 0, tempFilePath, -1, wstr, sizeof(wstr));

	// 查找该文件待操作文件的相关属性读取到WIN32_FIND_DATA

	HANDLE handle = FindFirstFile(wstr, &file);

	if (handle != INVALID_HANDLE_VALUE)

	{

		FindNextFile(handle, &file);

		FindNextFile(handle, &file);

		// 循环遍历得到文件夹的所有文件名	

		do

		{

			sprintf_s(tempFileName, "%s", fileName);

			imgNames.push_back((file.cFileName));

			//	imgNames[i].Insert(0, 1);

			i++;

		} while (FindNextFile(handle, &file));

	}

	FindClose(handle);

}

本文地址:https://blog.csdn.net/ningmengshuxiawo/article/details/107283201

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网