首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在目录中搜索文件

在目录中搜索文件
EN

Stack Overflow用户
提问于 2011-12-16 00:15:06
回答 2查看 1.3K关注 0票数 0

我在一个项目中工作,我需要知道一个文件在目录中是否是唯一的。那么,我如何才能发现一个文件是否存在于目录中呢?我有不带扩展名的文件名和目录的路径。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-12-16 00:18:20

我想这里没有现成的函数,但是你可以使用下面这样的函数:

代码语言:javascript
复制
static bool fileExists( const char *path )
{
    const DWORD attr = ::GetFileAttributesA( path );
    return attr != INVALID_FILE_ATTRIBUTES &&
           ( ( attr & FILE_ATTRIBUTE_ARCHIVE ) || ( attr & FILE_ATTRIBUTE_NORMAL ) );
}

这验证了它是一个“正常”文件。如果您还想处理隐藏文件,则可能需要添加/删除标志检查。

票数 2
EN

Stack Overflow用户

发布于 2011-12-16 00:27:49

我更喜欢在C++上做这件事,但是你提到了一个Visual-C++标签,所以有一种方法可以在Visual-C++.NET上做:

代码语言:javascript
复制
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

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8523132

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档