2011-12-15 116 views
0

我在一个项目中工作,我需要知道文件在目录中是否是唯一的。 那么如何才能找到一个文件是否存在于一个目录中? 我有没有扩展名和目录路径的文件名。搜索目录中的文件

+1

你说的“独特意思“?为什么没有扩展?你的意思是你想知道是否有文件仅在其扩展中有所不同? – Kevin 2011-12-15 16:18:52

回答

2

有为了这个,我觉得没有现成的功能,但你可以使用这样的事情:

static bool fileExists(const char *path) 
{ 
    const DWORD attr = ::GetFileAttributesA(path); 
    return attr != INVALID_FILE_ATTRIBUTES && 
      ((attr & FILE_ATTRIBUTE_ARCHIVE) || (attr & FILE_ATTRIBUTE_NORMAL)); 
} 

这验证了这是一个“普通”文件。如果你想处理隐藏的文件,你可能想要添加/删除标志检查。

+0

谢谢你这是exactely我需要什么,但我需要知道只有一件事的参数(路径=目录+文件名+扩展名)? – nidhal 2011-12-15 16:24:48

1

我喜欢做这在C++的方式,但你提到一个Visual-C++标签,所以有办法做到这一点基于Visual-C++。NET:

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 

}