2012-06-21 10 views
36

Ich möchte die Dateinamen aller Dateien, die eine bestimmte Erweiterung in einem bestimmten Ordner (und rekursiv, seine Unterordner) haben. Das heißt, der Dateiname (und die Erweiterung), nicht der vollständige Dateipfad. Das ist in Sprachen wie Python unglaublich einfach, aber ich bin nicht vertraut mit den Konstrukten dafür in C++. Wie kann es gemacht werden?Wie bekomme ich eine Liste von Dateien mit einer bestimmten Erweiterung in einem bestimmten Ordner?

+7

[ 'boost :: filesystem'] (http://www.boost.org/doc/libs/1_41_0/libs/filesystem/doc/index.htm) ist gut mit Dateien. – chris

+0

Schreiben von C++ nach Python muss nach C++ wie in einer Assemblersprache schreiben :) Was das Standard-C++ betrifft, ist dies eine überraschend Code-intensive Aufgabe. Ich unterstütze den Vorschlag, 'boost :: filesystem 'zu verwenden. – dasblinkenlight

+0

Super, danke für die Referenz. Ich werde das überprüfen. – Jim

Antwort

47
#define BOOST_FILESYSTEM_VERSION 3 
#define BOOST_FILESYSTEM_NO_DEPRECATED 
#include <boost/filesystem.hpp> 

namespace fs = ::boost::filesystem; 

// return the filenames of all files that have the specified extension 
// in the specified directory and all subdirectories 
void get_all(const fs::path& root, const string& ext, vector<fs::path>& ret) 
{ 
    if(!fs::exists(root) || !fs::is_directory(root)) return; 

    fs::recursive_directory_iterator it(root); 
    fs::recursive_directory_iterator endit; 

    while(it != endit) 
    { 
     if(fs::is_regular_file(*it) && it->path().extension() == ext) ret.push_back(it->path().filename()); 
     ++it; 

    } 

} 
+0

@ namar0x0309 Ich habe Ihre Bearbeitung angewendet, das ist richtig. – Gigi

+2

dort ist dies die tragbare Antwort. –

+2

Die einzige Sache, die für mich nicht funktionierte, ist das 'und', ich ersetzte es durch '&&'. Der Rest ist ausgezeichnet. +1 –

16

Auf Fenster tun Sie etwas wie folgt aus:

void listFiles(const char* path) 
{ 
    struct _finddata_t dirFile; 
    long hFile; 

    if ((hFile = _findfirst(path, &dirFile)) != -1) 
    { 
     do 
     { 
     if (!strcmp(dirFile.name, "." )) continue; 
     if (!strcmp(dirFile.name, ".." )) continue; 
     if (gIgnoreHidden) 
     { 
      if (dirFile.attrib & _A_HIDDEN) continue; 
      if (dirFile.name[0] == '.') continue; 
     } 

     // dirFile.name is the name of the file. Do whatever string comparison 
     // you want here. Something like: 
     if (strstr(dirFile.name, ".txt")) 
      printf("found a .txt file: %s", dirFile.name); 

     } while (_findnext(hFile, &dirFile) == 0); 
     _findclose(hFile); 
    } 
} 

auf Posix, wie Linux oder OSX:

void listFiles(const char* path) 
{ 
    DIR* dirFile = opendir(path); 
    if (dirFile) 
    { 
     struct dirent* hFile; 
     errno = 0; 
     while ((hFile = readdir(dirFile)) != NULL) 
     { 
     if (!strcmp(hFile->d_name, "." )) continue; 
     if (!strcmp(hFile->d_name, "..")) continue; 

     // in linux hidden files all start with '.' 
     if (gIgnoreHidden && (hFile->d_name[0] == '.')) continue; 

     // dirFile.name is the name of the file. Do whatever string comparison 
     // you want here. Something like: 
     if (strstr(hFile->d_name, ".txt")) 
      printf("found an .txt file: %s", hFile->d_name); 
     } 
     closedir(dirFile); 
    } 
} 
+0

Ihre Lösungen würden die Datei im Muster "foo.txt.exe" finden. Ich würde das nicht als Erweiterung von .txt betrachten. –

+0

Auf Windows, wo setzen Sie den Wert von "gIgnoreHidden". Ist es eine Flagge? – hshantanu

+0

ja - es ist ein globaler Boolean im Beispiel. Aber Sie können wirklich alles hier tun - als ein weiteres Argument für ListFiles übergeben. –

3

Get Liste von Dateien und Prozess jede Datei und Schleife durch sie und speichern zurück in verschiedenen Ordner

void getFilesList(string filePath,string extension, vector<string> & returnFileName) 
{ 
    WIN32_FIND_DATA fileInfo; 
    HANDLE hFind; 
    string fullPath = filePath + extension; 
    hFind = FindFirstFile(fullPath.c_str(), &fileInfo); 
    if (hFind != INVALID_HANDLE_VALUE){ 
     returnFileName.push_back(filePath+fileInfo.cFileName); 
     while (FindNextFile(hFind, &fileInfo) != 0){ 
      returnFileName.push_back(filePath+fileInfo.cFileName); 
     } 
    } 
} 

USE: Sie können wie diese Last alle Dateien aus dem Ordner und eine Schleife durch einen nach dem anderen

String optfileName ="";   
String inputFolderPath =""; 
String extension = "*.jpg*"; 
getFilesList(inputFolderPath,extension,filesPaths); 
vector<string>::const_iterator it = filesPaths.begin(); 
while(it != filesPaths.end()) 
{ 
    frame = imread(*it);//read file names 
     //doyourwork here (frame); 
    sprintf(buf, "%s/Out/%d.jpg", optfileName.c_str(),it->c_str()); 
    imwrite(buf,frame); 
    it++; 
} 
1

ein 17 C++ verwenden Code

#include <fstream> 
#include <iostream> 
#include <experimental/filesystem> 
namespace fs = std::experimental::filesystem; 

int main() 
{ 
    std::string path("/your/dir/"); 
    std::string ext(".sample"); 
    for(auto& p: fs::recursive_directory_iterator(path) 
    { 
     if(p.path().extension() == ext()) 
      std::cout << p << '\n'; 
    } 
    return 0; 
} 
Verwandte Themen