Dateien von HDD auflisten und als Linkliste speichern



  • Ich suche ein Tool womit ich alle Dateien eines Ordners (mit den Dateien aus Unterordnern) auflisten kann und die Links zu den Dateien in eine .txt Datei abspeichern kann.
    z.B.
    Ordner\Datei1
    Ordner\Ordner1\Datei2

    Habe schon sowas ähnliches gefunden, nur das keine Links zu den Dateien gespeichert werden. Vielleicht hat ja jemand so ein Programm oder kann mir beim anpassen des Quelltextes helfen.
    Ich kann nur ein wenig C und verstehe deshalb die meisten Funktionen nicht.

    /*
     * This file is part of DirectoryNavigator.
     * Copyright (C) 2005 - 2009 CodePlanet. All rights reserved.
     *
     * DirectoryNavigator is free software: you can redistribute it and/or modify
     * it under the terms of the GNU General Public License as published by
     * the Free Software Foundation, either version 3 of the License, or
     * (at your option) any later version.
     * 
     * DirectoryNavigator is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     * GNU General Public License for more details.
     * 
     * You should have received a copy of the GNU General Public License
     * along with DirectoryNavigator.  If not, see <http://www.gnu.org/licenses/>. 
     */
    
    #include "listdir.h"
    #include "timer.h"
    
    /**
     * File:          listdir.cpp                                          
     * Description:   Recursively walk through directories, starting        
     *                at a specified path and insert it into a an          
     *                STL-like container class for n-ary trees.           
     * Version:       1.0                                                 
     * Language:      C++, MSVC 8.0                                        
     * Platform:      x86, Windows NT                                      
     * Author:        http://www.codeplanet.eu/              
     */
    Item::Item()
    { 
    }
    
    Item::~Item()
    {
    }
    
    // Default dir name display function
    void Item::dirsItem(const std::string& dirName) 
    { 
        std::cout << "\n  " << dirName  << std::endl;
    }
    
    // Default file data display function
    void Item::fileItem(const WIN32_FIND_DATA *findData) 
    {
        int filesize = findData->nFileSizeLow;
    
        FILETIME ft;
        FileTimeToLocalFileTime(&(findData->ftLastWriteTime), &ft);
        SYSTEMTIME st;
        FileTimeToSystemTime(&ft, &st);
    
        std::cout << "\t";
        std::cout.setf(std::ios::left, std::ios::adjustfield);
        std::cout << std::setw(15) << findData->cFileName;
        std::cout.setf(std::ios::right, std::ios::adjustfield);
        std::cout << std::setw(10) << filesize << "    ";
        std::cout << std::setw(2) << st.wMonth << "/"
                  << std::setw(2) << st.wDay << "/"
                  << std::setw(4) << st.wYear << "  "
                  << std::setw(2) << st.wHour << ":"
                  << std::setw(2) << st.wMinute << std::endl;  
    }
    
    // Constructor: Save user's working directory
    Navigator::Navigator(std::string r)
    : root(r), directories(0), files(0)
    {
        // Create first element at the top
        viter.push_back(strTree.begin());                     
        viter.push_back(strTree.insert(viter[0], ""));
    
        // save user's working directory
        char buffer[256];
        GetCurrentDirectory(256, buffer);
        userDir = buffer;
        p = new Item();
    }
    
    // Use alternate Item class
    void Navigator::use(Item *dp) 
    { 
        delete p;
        p = dp; 
    }
    
    // Destructor: Restore user's working directory
    // and delete p
    Navigator::~Navigator() 
    {  
        SetCurrentDirectory(userDir.c_str()); 
        delete p;
    }
    
    // Specify starting directory and initiate walk
    void Navigator::start() 
    {
        // Convert "." or ".." into full path name
        char buffer[256];
    
        if( (root == ".") || (root == "..") ) {
            if( !SetCurrentDirectory(root.c_str()) )
                std::cout << "Could not find directory " << root << std::endl;
    
            GetCurrentDirectory(256, buffer);
            root = buffer;
        }
    
        // Start recursive directory walk
        walk(root);
    }
    
    // Walk directory tree rooted at dir
    void Navigator::walk(const std::string &dir) 
    {
        char *fileName;
        char curDir[256];
        char fullName[256];
        HANDLE fileHandle;
        WIN32_FIND_DATA findData;
        std::string fileMask = "*.*";
    
        // Save current dir so we can restore it
        if( !GetCurrentDirectory(256, curDir) ) 
            return;
    
        // If the directory name is neither . or .. then
        // change to it, otherwise ignore it                              
        if( (dir != ".") && (dir != "..") ) {
            if(!SetCurrentDirectory(dir.c_str())) 
                return;
        } else {
            return;
        }
    
        // Print out the current directory name
        if( !GetFullPathName(fileMask.c_str(), 256, fullName, &fileName) ) 
            return;
    
        std::string tmp(fullName);
        ++directories;
    
        /* Check if we have more subdirectory's
        if(lastDir.compare(curDir) == 0)      
            std::cout << "Another Subdirectory!" << std::endl;
        */
        viter.push_back(strTree.append_child(viter[viter.size()-1], tmp));
    
        // Loop through all files in the directory
        fileHandle = FindFirstFile(fileMask.c_str(), &findData);
    
        while( fileHandle != INVALID_HANDLE_VALUE ) {
            // If the name is a directory,
            // recursively walk it. Otherwise print
            // print the file's data
            if( findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) {
                // OK, it is a directory
                walk(findData.cFileName);
            } else {
                fileItem(&findData);
            }
    
            // Loop through remaining entries in the dir
            if( !FindNextFile(fileHandle, &findData) )
                break;
        }
    
        // Clean up and restore directory
        FindClose(fileHandle);
        SetCurrentDirectory(curDir);
        viter.pop_back();
    }
    
    // Write our tree to console
    void Navigator::displayTree()
    {
        tree<std::string>::pre_order_iterator it = strTree.begin(), end = strTree.end();
    
        if( !strTree.is_valid(it) ) {
            throw Exception(Exception::INVALID_ARGUMENT, "Navigator: Could not display tree. No valid tree object!");
            return;
        }
    
        int rootdepth = strTree.depth(it);
        std::cout << "---------------------------------------------------" << std::endl;
        while( it != end ) {
            for(int i=0; i < strTree.depth(it)-rootdepth; ++i) 
                std::cout << "  ";
            std::cout << (*it) << std::endl << std::flush;
            ++it;
        }
        std::cout << "---------------------------------------------------" << std::endl;
    }
    
    // Write our tree into a file
    void Navigator::writeToFile(std::ofstream& filestream)
    {
        tree<std::string>::pre_order_iterator it = strTree.begin(), end = strTree.end();
    
        if( !strTree.is_valid(it) ) 
            throw Exception(Exception::INVALID_ARGUMENT, "Navigator: Could not write tree to file. No valid tree object!");
    
        int rootdepth = strTree.depth(it);
        filestream << "\nDirectory Tree Structure of " << root << ":" << std::endl; 
        filestream << "---------------------------------------------------" << std::endl;
        while( it != end ) {
          for( int i=0; i < strTree.depth(it) - rootdepth; ++i ) 
             filestream << "  ";
          filestream << (*it) << std::endl << std::flush;
          ++it;
        }
        filestream << std::endl;    
    }
    
    // Insert file data to our tree
    void Navigator::fileItem(const WIN32_FIND_DATA *findData)
    {
        int filesize = findData->nFileSizeLow; 
    
        FILETIME ft;
        FileTimeToLocalFileTime(&(findData->ftLastWriteTime), &ft);
        SYSTEMTIME st;
        FileTimeToSystemTime(&ft, &st);
    
        std::string fileData(findData->cFileName);
    
        // Need some Conversion ;)
        std::stringstream ss;
    
        //ss << std::setw(10) << filesize << "    " 
        //   << std::setw(2) << std::setfill('0') << st.wDay << "/"
        //   << std::setw(2) << st.wMonth << "/"
        //   << std::setw(4) << st.wYear << "  "
        //   << std::setw(2) << st.wHour << ":"
        //   << std::setw(2) << st.wMinute; 
    
        fileData += ss.str();
    
        strTree.append_child( viter[viter.size()-1], fileData );
        ++files;
    }
    
    void Navigator::sortTree()
    {
        // Sorting the entire tree, level by level    
        strTree.sort( strTree.begin(), strTree.end(), true ); 
    }
    
    void Navigator::setNewRoot(const std::string& nroot)
    { 
        // Clear up data
        strTree.clear();
        viter.clear();
    
        if( strTree.empty() && viter.empty() ) {
            root = nroot; 
    
            // Create first element at the top
            viter.push_back(strTree.begin());                     
            viter.push_back(strTree.insert(viter[0], ""));
        } else {
            throw Exception(Exception::OTHER_ERROR, "Navigator: setNewRoot failed!");
        }
    }
    
    /*
     * This file is part of DirectoryNavigator.
     * Copyright (C) 2005 - 2009 CodePlanet. All rights reserved.
     *
     * DirectoryNavigator is free software: you can redistribute it and/or modify
     * it under the terms of the GNU General Public License as published by
     * the Free Software Foundation, either version 3 of the License, or
     * (at your option) any later version.
     * 
     * DirectoryNavigator is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     * GNU General Public License for more details.
     * 
     * You should have received a copy of the GNU General Public License
     * along with DirectoryNavigator.  If not, see <http://www.gnu.org/licenses/>. 
     */
    
    #include "listdir.h"
    #include "timer.h"
    
    /**
     * File:          main.cpp                                             
     * Description:   This is the main class. It shows some examples how to    
     *                use the other classes like Navigator and Timer. 
     * Version:       1.0                                                  
     * Language:      C++, MSVC 8.0                                        
     * Platform:      x86, Windows NT                                  
     * Author:        http://www.codeplanet.eu/ 
     */
    int main(int argc, char *argv[]) 
    {
        try {
            if(argc < 2) 
                throw InvalidArgument("Please enter one starting path!");
    
            unsigned long dwResult = GetFileAttributes(argv[1]);
    
            if(dwResult == INVALID_FILE_ATTRIBUTES)
                throw InvalidArgument("Please enter a valid path!");
            else if(!(dwResult & FILE_ATTRIBUTE_DIRECTORY))
                throw InvalidArgument("This is not a directory!");
    
            const char filename[] = "output.txt";
            SYSTEMTIME st;
            GetSystemTime(&st);  
            std::ofstream file(filename, std::ios_base::out);
            Timer tim;
    
            tim.StartTimer();   // Start a timer
    
            if( !file )
                throw Exception(Exception::IO_ERROR, "Error opening file!");
    
            file << "Listing generated at: " << st.wDay  << "." << st.wMonth<< "." << st.wYear 
                 << " - " << std::setw(2) << std::setfill('0') << st.wHour << ":"
                 << std::setw(2) << st.wMinute << ":" << std::setw(2) 
                 << st.wSecond << " (GMT)" << std::setfill(' ') << std::endl;
    
            // Create new instance
            Navigator o(argv[1]);
    
            // Start recursive listing
            o.start();
            o.writeToFile( file );
            file << "\n\nContent: " << o.rFiles() << " Files, " << o.rDirectories() << " Directories.\n";
            file.close();
    
            std::cout << "Total time for execution: " << tim.ElapsedTime() << " seconds" << std::endl;
    
            /* o.sortTree();
               o.displayTree();
               o.setNewRoot("C:\\");
               o.start();
               o.displayTree(); */
        } catch( Exception& e ) {
            std::cout << e.what() << " (" << e.GetErrorType() << ")" << std::endl;
            return -1;
        } catch(...) {
            std::cout << "Unknown Exception!" << std::endl;
            return -1;
        }
    
        return 0;
    }
    


  • dir c:\ /S /B > dateien.txt



  • 😮 Wieso einfach, wenn es auch kompliziert geht.
    Hatte es schon mit "tree" probiert.

    Kann man den Laufwerksbuchstaben auch nicht mit speichern?

    also so:
    Ordner/Datei.txt

    Schonmal vielen Dank volkard.



  • TVJunkie schrieb:

    Kann man den Laufwerksbuchstaben auch nicht mit speichern?

    Jetzt wären Deine superlativen Programmierkenntnisse angebracht, daß Du die dateien.txt zeilenweise einliest und jede Zeile ohne die ersten beiden Zeichen wieder ausgibst.

    int main(){
       string zeile;
       while(in>>zeile){
          cout<<zeile.substr(2,-1)<<'\n';//oder so
       }
    }
    

    und dann
    dir c:\ /S /B | abschnippler > dateien.txt



  • volkard schrieb:

    cout<<zeile.substr(2,-1)<<'\n';//oder so
    

    zeile.substr(2); reicht doch - und produziert weder warnings noch kann(könnte) es bei langen strings das ende abschneiden

    bb



  • Ich steige noch nicht ganz durch, wo denn der Pfad zu der Datei (die ich einlesen will) angegeben wird, damit das Programm weiss, was es öffnen soll.

    Aber wenn ich das sowieso neu einlesen muss kann ich das auch in dem eigentlichen Programm machen, wozu ich die Linkliste brauche.

    Mache dazu mal ein neues Thema auf.

    Erstmal Danke an euch beide.


Anmelden zum Antworten