C++中如何查找一个文件夹,例如查找Temp这个文件夹?

Mr_Right 2013-01-28 08:54:55
要查找一个文件夹是否存在电脑上,该怎么实现?
...全文
331 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2013-01-29
  • 打赏
  • 举报
回复
GetTempPath The GetTempPath function retrieves the path of the directory designated for temporary files. DWORD GetTempPath( DWORD nBufferLength, // size, in characters, of the buffer LPTSTR lpBuffer // pointer to buffer for temp. path ); Parameters nBufferLength Specifies the size, in characters, of the string buffer identified by lpBuffer. lpBuffer Pointer to a string buffer that receives the null-terminated string specifying the temporary file path. The returned string ends with a backslash, for example, C:\TEMP\. Return Values If the function succeeds, the return value is the length, in characters, of the string copied to lpBuffer, not including the terminating null character. If the return value is greater than nBufferLength, the return value is the size of the buffer required to hold the path. If the function fails, the return value is zero. To get extended error information, call GetLastError. Remarks Windows 95 and Windows 98: The GetTempPath function gets the temporary file path as follows: The path specified by the TMP environment variable. The path specified by the TEMP environment variable, if TMP is not defined or if TMP specifies a directory that does not exist. The current directory, if both TMP and TEMP are not defined or specify nonexistent directories. Windows NT: The GetTempPath function does not verify that the directory specified by the TMP or TEMP environment variables exists. The function gets the temporary file path as follows: The path specified by the TMP environment variable. The path specified by the TEMP environment variable, if TMP is not defined. The Windows directory, if both TMP and TEMP are not defined. QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Windows CE: Unsupported. Header: Declared in winbase.h. Import Library: Use kernel32.lib. Unicode: Implemented as Unicode and ANSI versions on Windows NT. See Also File I/O Overview, File Functions, GetTempFileName
Mr_Right 2013-01-29
  • 打赏
  • 举报
回复
引用 5 楼 zhao4zhong1 的回复:
system("dir /b /a-d c:\\*.* >d:\\allfiles.txt"); //读文件d:\\allfiles.txt的内容即C:\\下所有文件的名字 system("dir /b /ad c:\\*.* >d:\\alldirs.txt"); //读文件d:\\alldirs.txt的内容即C:\\下所有子目录的名字 请记住,能用shel……
这个不是太明白,我现在就是想要查找C盘的Temp临时文件夹,不管win7还是Xp都可以,能给个例子吗,谢谢!
Mr_Right 2013-01-29
  • 打赏
  • 举报
回复
问题解决了,感谢各位的帮助!
赵4老师 2013-01-29
  • 打赏
  • 举报
回复
char tempdir[_MAX_PATH]=getenv("temp");
mujiok2003 2013-01-29
  • 打赏
  • 举报
回复
boost::filesytem
cfanandhacker2 2013-01-29
  • 打赏
  • 举报
回复
不过还有很多要完善的地方,我写的这个小例子找到第一个Temp文件夹就停止了,假如C盘下各级子目录有多个Temp文件夹就只能找到其中一个。而且对大小写敏感,如果是TEMP就会忽略。你可以自己按照自己的思路完善。
cfanandhacker2 2013-01-29
  • 打赏
  • 举报
回复
用MFC的CFileFind类比较容易实现。递归遍历C盘下所有的文件夹和子文件夹。 写一个函数实现你的功能。
CString FindTemp(CString strPath)
{
	CFileFind finder;
	BOOL bNext = finder.FindFile(strPath + "\\*.*");
	while(bNext)
	{
		bNext = finder.FindNextFile();
		if (finder.IsDirectory()&&!finder.IsDots())
		{
			if (finder.GetFileName() == "Temp")
				return finder.GetFilePath();
			else
			{
				CString strResult = FindTemp(finder.GetFilePath());
				if (strResult =="没有找到!")
					continue;
				else 
					return strResult;
			}
		}
	}
	return "没有找到!";
}
然后调用这个函数 CString str = FindTemp("c:"); MessageBox(str); 没有这个文件夹得话就提示“没有找到!” 找到了的话就显示它的全路径
Mr_Right 2013-01-28
  • 打赏
  • 举报
回复
不知道具体路径可以实现吗?
caoh110 2013-01-28
  • 打赏
  • 举报
回复
//在mfc中的方法 CString folder; //存文件夹路径 CFileFind finder; //在mfc中有这样一个类叫做CFileFind, finder.FindFile(folder); //如果返回值为1表示路径存在,为0则表示不存在
苹果皮 2013-01-28
  • 打赏
  • 举报
回复
//获取目录 GetTempPath SHGetFolderPath //判断是否存在 PathFileExists
图灵狗 2013-01-28
  • 打赏
  • 举报
回复
函数名: access 功 能: 确定文件的访问权限 用 法: int access(const char *filename, int amode); 以前一直没用过这个函数,今天调试程序发现了这个函数,感觉挺好用,尤其是判断一个文件或文件夹是否存在的时候,用不着再find了,文件的话还可以检测读写权限,文件夹的话则只能判断是否存在,下面摘自MSDN: int _access( const char *path, int mode ); Return Value Each of these functions returns 0 if the file has the given mode. The function returns –1 if the named file does not exist or is not accessible in the given mode; in this case, errno is set as follows: EACCES Access denied: file’s permission setting does not allow specified access. ENOENT Filename or path not found. Parameters path File or directory path mode Permission setting Remarks When used with files, the _access function determines whether the specified file exists and can be accessed as specified by the value of mode. When used with directories, _access determines only whether the specified directory exists; in Windows NT, all directories have read and write access. mode Value Checks File For 00 Existence only 02 Write permission 04 Read permission 06 Read and write permission
赵4老师 2013-01-28
  • 打赏
  • 举报
回复
system("dir /b /a-d c:\\*.* >d:\\allfiles.txt"); //读文件d:\\allfiles.txt的内容即C:\\下所有文件的名字 system("dir /b /ad c:\\*.* >d:\\alldirs.txt"); //读文件d:\\alldirs.txt的内容即C:\\下所有子目录的名字 请记住,能用shell命令获取文件、文件夹信息或者操作文件、文件夹最好用shell命令获取或者操作,而不要用各种API获取或者操作,因为当遇到非法文件夹名或非法文件名或非法文件长度、非法文件日期、压缩文件、链接文件、稀疏文件……等各种意料之外的情况时,API会处理的不全面或陷入死循环,而shell命令不会。

64,652

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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