Rekursives finden von Dateien



  • Hallo!
    Ich habe mir eine Funktion geschrieben, die einem Ordner + allen Unterorndnern Dateien auslesen soll.
    funktioniert bei mir aber nicht (geht immer nur in den 1. Unterornder, dann wieder in den nächsten 1.Unterordner,...)
    Was habe ich falsch gemacht!
    Bitte um Hilfe!!!
    Hier der Code:

    code type="c++"]
    void CFindfilesDlg::FindFiles(CString find){
    struct _finddata_t fInfo;
    long hFile;

    if( (hFile = _findfirst( find, &fInfo)) == 1L){

    }else{
    while( _findnext( hFile, &fInfo ) == 0){
    CString info = fInfo.name;
    int found = info.ReverseFind('.');
    if(found == -1){
    if(fInfo.name != "." && fInfo.name != ".."){
    int pos = find.ReverseFind('');
    find.Replace("
    .", fInfo.name);
    find += "\*.
    ";
    FindFiles(find);
    }
    }
    m_list.AddString(fInfo.name);
    }
    _findclose( hFile );
    }
    }[/code]



  • Hier noch mal der Code - Mit code Tags 🙂
    code type=["c++"]
    void CFindfilesDlg::FindFiles(CString find){
    struct _finddata_t fInfo;
    long hFile;

    if( (hFile = _findfirst( find, &fInfo)) == 1L){

    }else{
    while( _findnext( hFile, &fInfo ) == 0){
    CString info = fInfo.name;
    int found = info.ReverseFind('.');
    if(found == -1){
    if(fInfo.name != "." && fInfo.name != ".."){
    int pos = find.ReverseFind('');
    find.Replace("
    .", fInfo.name);
    find += "\*.
    ";
    FindFiles(find);
    }
    }
    m_list.AddString(fInfo.name);
    }
    _findclose( hFile );
    }
    }[/code]



  • Tschudldigung Merk ich mir nie wie das geht mit den Code Tags!

    void CFindfilesDlg::FindFiles(CString find){ 
    struct _finddata_t fInfo;
    long hFile;
    
    if( (hFile = _findfirst( find, &fInfo)) == 1L){
    
    }else{ 
    while( _findnext( hFile, &fInfo ) == 0){
    CString info = fInfo.name;
    int found = info.ReverseFind('.');
    if(found == -1){
    if(fInfo.name != "." && fInfo.name != ".."){
    int pos = find.ReverseFind('*');
    find.Replace("*.*", fInfo.name);
    find += "\\*.*";
    FindFiles(find);
    }
    }
    m_list.AddString(fInfo.name);
    }
    _findclose( hFile );
    }
    }
    


  • Du hast 2 Möglichkeiten:

    • du willst MFC Programmieren - Nimm CFileFind.
    • du willst es so machen, dann poste es ins WinApi forum.

    Devil



  • BTW: Es gibt auch Buttons für die Code-Tags rechts unter dem Edit-Feld in das du deine Antwort schreibst 😉 .



  • Hallo!
    Ja dann nehme ich halt CFileFind, funktiniert ja fast identisch:
    Nur wie mache ich solche eine Rekursion???
    Bitte um Hilfe!!



  • Ich mach das so mit zwei Member-Funktionen:

    ////////////////////////////
    // Verzeichnisse Kopieren //
    ////////////////////////////
    
    long CVerzeichnisKopierenDlg::SnapshotFiles(CString pstrSourcePath)
    {
        BOOL bMore;
        WIN32_FIND_DATA wfd;
        HANDLE handle;
        CString szMask;
        CString szFullName;
    
        szMask = pstrSourcePath + "\\*.*";      //Suchmaske für FindFirstFile
    
        handle = ::FindFirstFile(szMask, &wfd);
        bMore = handle != INVALID_HANDLE_VALUE;
    
        while(bMore)
        {
            if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            {
                if( ((strcmp(wfd.cFileName, ".")  != 0) && 
                     (strcmp(wfd.cFileName, "..") != 0)) )      // filename is a directory
                {
                    szFullName = pstrSourcePath + "\\" + wfd.cFileName;
                    SnapshotFiles(szFullName);  //Funktion rekursiv aufrufen
                }/*if*/
            }/*if*/
    
            else
            {
                CString SourceFile = pstrSourcePath + "\\" + wfd.cFileName;
                m_lNumFiles++;
    
                MSG msg;
                int Return = PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
                if (Return!=0)
                {
                    TranslateMessage(&msg);
                    DispatchMessage(&msg);
                }
            }/*else*/
    
            bMore = ::FindNextFile(handle,&wfd);
    
        }/*while*/
    
        if (handle!=INVALID_HANDLE_VALUE) ::FindClose(handle);
    
        return m_lNumFiles;
    }////////////////////Ende Member-Funktion 
    
    BOOL CVerzeichnisKopierenDlg::XcopyFiles(CString pstrSourcePath, CString pstrTargetPath, long m_lNumFiles)
    {
        BOOL bMore;
        WIN32_FIND_DATA wfd;
        HANDLE handle;
        CString szMask;
        CString szFullName;
        CString szTargetName;
    
        m_ctlButton_Stop.EnableWindow(TRUE);
    
        CreateDirectory(pstrTargetPath, NULL);  //Zielverzeichnis erstellen
        szMask = pstrSourcePath + "\\*.*";      //Suchmaske für FindFirstFile
    
        handle = ::FindFirstFile(szMask, &wfd);
        bMore = handle != INVALID_HANDLE_VALUE;
    
        while(bMore&&(m_bIsKilled!=TRUE))
        {
            if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            {
                if( ((strcmp(wfd.cFileName, ".")  != 0) && 
                     (strcmp(wfd.cFileName, "..") != 0)) )      // filename is a directory
                {
                    szFullName = pstrSourcePath + "\\" + wfd.cFileName;
                    szTargetName = pstrTargetPath + "\\" + wfd.cFileName;
                    CreateDirectory(szTargetName, NULL);        //Ziel-Unterverzeichnisse erstellen
                    m_ctlStaticDirectory.SetWindowText(szFullName); //Verzeichnis anzeigen
                    m_ctlListBox.AddString(szFullName);         //Verzeichnis in Liste zufügen
                    UpdateWindow();                             //Aktualiserung der ListBox
                    XcopyFiles(szFullName, szTargetName, m_lNumFiles ); //Funktion rekursiv aufrufen
                }/*if*/
            }/*if*/
    
            else
            {
                CString SourceFile = pstrSourcePath + "\\" + wfd.cFileName;
                CString TargetFile = pstrTargetPath + "\\" + wfd.cFileName;
                m_ctlStaticFile.SetWindowText(wfd.cFileName);
                ::CopyFile(SourceFile, TargetFile, TRUE);   //Datei wird kopiert, 
                                                            //TRUE:  File wird nicht überschrieben
                                                            //FALSE: File wird überschrieben
    
                MSG msg;
                int Return;
                Return = PeekMessage( &msg, NULL, 0, 0, PM_REMOVE);
                if (Return!=0)
                {
                    TranslateMessage(&msg);
                    DispatchMessage(&msg);
                }
    
                //  Prozentanteil kopierter Dateien, lNumFiles entspricht 100%.                            
                m_lZaehler++;
                m_iProzent = (int) ( m_lZaehler*100 / m_lNumFiles );
    
                CString string;
                string.Format( "%i", m_iProzent );  m_ctlStaticProzent.SetWindowText( string );
                string.Format( "%i", m_lZaehler);   m_ctlStaticNumberFiles.SetWindowText( string );
    
            }/*else*/
    
            bMore = ::FindNextFile(handle,&wfd);
    
        }/*while*/
    
        if (handle!=INVALID_HANDLE_VALUE) ::FindClose(handle);
    
        return TRUE;
    }////////////////////Ende Member-Funktion
    


  • Wie wäre es mal mit der Nachschau in den FAQ.
    Alle Verzeichnisse sowie Dateinamen ausgeben?


Anmelden zum Antworten