在目录中搜索文件
Search a file in a directory
我在一个项目中工作,我需要知道一个文件在目录中是否唯一。那么,如何查找目录中是否存在文件呢?我有没有扩展名的文件名和目录路径。
我认为没有现成的函数,但你可以使用这样的东西:
static bool fileExists( const char *path )
{
const DWORD attr = ::GetFileAttributesA( path );
return attr != INVALID_FILE_ATTRIBUTES &&
( ( attr & FILE_ATTRIBUTE_ARCHIVE ) || ( attr & FILE_ATTRIBUTE_NORMAL ) );
}
这将验证它是一个"正常"文件。如果您也想处理隐藏文件,则可能需要添加/删除标志检查。
我更喜欢用c++的方式来做这件事,但是你提到了一个visual c++标签,所以有一种方法可以在visual c++上做到这一点。净:
using <mscorlib.dll>
using namespace System;
using namespace System::IO;
bool search(String folderPath, String fileName) {
String* files[] = Directory::GetFiles(folderPath, fileName+".*"); //search the file with the name fileName with any extension (remember, * is a wildcard)
if(files->getLength() > 0)
return true; //there are one or more files with this name in this folder
else
return false; //there arent any file with this name in this folder
}
相关文章:
- 按单词搜索文件,然后在C++中打印特定行数
- 在C++目录中搜索文件的函数输出中出错
- 按扩展名过滤搜索文件会返回太多结果
- 如何向 g++ 编译器解释如何读取用于从'list'文件中搜索 .h 文件的'include'目录列表
- C++,使用自己的条件搜索文件中的字符串
- C/C++ 从根位置搜索文件夹并返回绝对路径
- 在目录中搜索文件夹
- 搜索文件的特定位置
- 按文件名搜索文件
- 使用扩展功能在整个系统中搜索文件
- 在目录中搜索文件
- 在C++中搜索文件夹
- 生成文件应该在另一个目录中搜索 .o 文件
- 有没有更好的方法来搜索文件中的字符串
- 在 c++ 中搜索文件中的字符串以查找非常大的输入的有效方法
- 如何在子目录中搜索文件
- 如何指定在整个系统中搜索文件的目录
- 如何在visualstudio中快速搜索文件内容以查找大型代码库
- C++显示include搜索文件的位置
- 在计算机中递归搜索文件