2011-12-15 20 views
0

私はプロジェクトで働き、ファイルがディレクトリ内で一意であるかどうかを知る必要があります。 ファイルがディレクトリに存在するかどうかはどうすればわかりますか? 拡張子のないファイル名とディレクトリのパスがあります。ディレクトリ内のファイルを検索する

+1

あなたは「ユニークとはどういう意味ですか"?なぜ拡張子なし?拡張子だけが異なるファイルがあるかどうか知りたいのですか? – Kevin

答えて

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

これは私が必要としている正確にありがとう、しかし私はちょうど1つの事をパラメータ(パス=ディレクトリ+ファイル名+拡張子)を知る必要がありますか? – nidhal

+1

@nidahl:そうです。 'path'引数はファイルシステム内の実際のパスです。 –

1

私はC++の方法でこれを行うことを好むが、あなたは、Visual-C++タグを述べたので、ビジュアル-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 

} 
関連する問題