MFC不能遍历某文件夹下的.BIN文件

qq_26868981 2017-09-30 09:37:31
我找到了遍历txt的代码,以为改个类型就好了,,。然而并不可以。

void CTestDlg::getWhole(CString pathstr,CStringArray& arrstrfile)
{
TRACE("\n进入子函数");
CString mypath,fdpath;
mypath=pathstr+"\\*.*";
CString strtmp;
CFileFind find;
BOOL bf=find.FindFile(mypath);
while(bf)
{
bf=find.FindNextFileA();
if(!find.IsDots())
{
fdpath=find.GetFilePath();
if(find.IsDirectory())
getWhole(fdpath,arrstrfile);
else
{
strtmp=fdpath.Right(4);
strtmp.MakeLower();
if(strtmp==".txt") //修改此处为.BIN就没有输出结果
arrstrfile.Add(fdpath);
}
}
}
find.Close();
for(int i=0;i<arrstrfile.GetCount();i++)
TRACE("\n%s",arrstrfile[i]);
}
...全文
899 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_26868981 2017-10-10
  • 打赏
  • 举报
回复
引用 2 楼 schlafenhamster 的回复:
strtmp.MakeLower(); 变成 小写 ,与 “ .BIN“ 比?
赶着往后写一直没看,仁兄好慧眼。当我被相对路径坑到想哭,才发现,,原来这个这么简单啊
赵4老师 2017-09-30
  • 打赏
  • 举报
回复
system("dir /b /a-d c:\\*.* >d:\\allfiles.txt"); //读文件d:\\allfiles.txt的内容即C:\\下所有文件的名字 system("dir /b /a-d /s c:\\*.* >d:\\allfilesinsub.txt"); //读文件d:\\allfilesinsub.txt的内容即C:\\下所有文件的名字包含子目录 system("dir /b /ad c:\\*.* >d:\\alldirs.txt"); //读文件d:\\alldirs.txt的内容即C:\\下所有子目录的名字 请记住,能用shell命令获取文件、文件夹信息或者操作文件、文件夹最好用shell命令获取或者操作,而不要用各种API获取或者操作,因为当遇到非法文件夹名或非法文件名或非法文件长度、非法文件日期、压缩文件、链接文件、稀疏文件……等各种意料之外的情况时,API会处理的不全面或陷入死循环,而shell命令不会。 如果嫌system黑窗口一闪,将system("...")替换为WinExec("cmd /c ...",SW_HIDE);
孤客天涯 2017-09-30
  • 打赏
  • 举报
回复

/*================================================================*
【函數】:	GetDirInfo()
【功能】:	查找指定目录指定文件类型的相关信息
【参数】:	strPath			:	目录名
			strDef			:	文件扩展名
			nSubFolderArray	:	返回子目录链表
			nFilesArray		:	返回文件链表
			bIncludeSubFolder : 包括子目录
【返回】:	无
*================================================================*/
void CUtility::GetDirInfo(const CString &strPath,const CString &strDef,CStringArray &nSubFolderArray,CStringArray &nFilesArray,BOOL bIncludeSubFolder)
{
	CString strPathTemp = strPath;
	if(strPathTemp.Right(1) == _T('\\'))
	{
		strPathTemp = strPathTemp.Left(strPathTemp.GetLength() - 1);
	}

	WIN32_FIND_DATA FileData; 
	HANDLE hSearch; 
	BOOL fFinished = FALSE; 
	
	hSearch = FindFirstFile(strPathTemp + "\\*.*", &FileData);
	if(hSearch != INVALID_HANDLE_VALUE)
	{
		while (!fFinished)
		{ 
			CString strFileName = FileData.cFileName;
			if((FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
			{
				if(strFileName != "." && strFileName != "..")
				{
					CString strPathName;
					strPathName.Format(_T("%s\\%s"),strPathTemp,strFileName);
					nSubFolderArray.Add(strPathName);
					if(bIncludeSubFolder)
					{
						GetDirInfo(strPathName,strDef,nSubFolderArray,nFilesArray,bIncludeSubFolder);
					}
				}
			}
			else 
			{
				if(strDef == _T("*.*"))
				{
					CString strName;
					strName.Format(_T("%s\\%s"),strPathTemp,strFileName);
					nFilesArray.Add(strName);
				}
				else
				{
					CString strDefEx1 = _T("");
					int iPos1 = strFileName.ReverseFind(_T('.'));
					if(iPos1 > 0)
					{
						strDefEx1 = strFileName.Right(strFileName.GetLength() - iPos1);
					}
					
					CString strDefEx2 = strDef;
					int iPos2 = strDef.ReverseFind(_T('.'));
					if(iPos2 > 0)
					{
						strDefEx2 = strDef.Right(strDef.GetLength() - iPos2);
					}
					
					if(strDefEx1.CompareNoCase(strDefEx2) == 0)
					{
						CString strName;
						strName.Format(_T("%s\\%s"),strPathTemp,strFileName);
						nFilesArray.Add(strName);
					}
				}
			}
			
			if(!FindNextFile(hSearch, &FileData))fFinished = TRUE;
		} 
	}
	FindClose(hSearch);
}

【例子】:	GetDirInfo(_T("c:\\)",_T("*.txt"),nSubFolderArray,nFilesArray,FALSE);
schlafenhamster 2017-09-30
  • 打赏
  • 举报
回复
strtmp.MakeLower(); 变成 小写 ,与 “ .BIN“ 比?

1,649

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 非技术类
社区管理员
  • 非技术类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧