Ganze Ordner kopieren?



  • Wie kann ich ganze Ordner kopieren (mit Unterordnern) ➡ für Backup Funktion

    geg.: Quellpfad Bsp.: C:\Test1\Test2
    Zielpfad Bsp.: C:\Test3\Test4

    alle Dateien und Ordner aus C:\Test1\Test2 in C:\Test3\Test4 kopieren.



  • Hallo,

    Hier hilft dir die WinAPI mit SHFileOperation



  • Irgendwie klappts nicht, was mach ich falsch ?:

    #include <windows.h>
    #include <shellapi.h>
    
    int main()
    {
        LPSHFILEOPSTRUCT lpFileOp;
        lpFileOp->pTo="C:/Test2";
        lpFileOp->pFrom="C:/Test1";
        SHFileOperation(lpFileOp);
        return 0;
    }
    


  • Dieser Thread wurde von Moderator/in HumeSikkins aus dem Forum C++ in das Forum WinAPI verschoben.

    Im Zweifelsfall bitte auch folgende Hinweise beachten:
    C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?

    Dieses Posting wurde automatisch erzeugt.



  • Kurz: Du solltest dir auch ansehen, was die Funktion für Parameter erwartet.
    Lang:

    • wFunc nicht gesetzt
    • pFrom und pTo müssen doppelt nullterminiert werden
    • ...


  • Und jetzt ?:

    #include <windows.h>
    #include <shellapi.h>
    
    int main()
    {
        BOOL b;
        LPSHFILEOPSTRUCT lpFileOp;
        lpFileOp->hNameMappings=0;
        lpFileOp->lpszProgressTitle=0;
        lpFileOp->fAnyOperationsAborted=b;
        lpFileOp->fFlags=0;
        lpFileOp->hwnd=0;
        lpFileOp->wFunc=FO_COPY;
        lpFileOp->pTo="C:/Test2/0/0";
        lpFileOp->pFrom="C:/Test1/0/0";
        SHFileOperation(lpFileOp);
        return 0;
    }
    


  • Nein, lieber mit \0'en am Ende des String 😉



  • OK, hehe 🙄

    aber es geht trotzdem nicht:

    #include <windows.h>
    #include <shellapi.h>
    
    int main()
    {
        BOOL b;
        LPSHFILEOPSTRUCT lpFileOp;
        lpFileOp->hNameMappings=0;
        lpFileOp->lpszProgressTitle=0;
        lpFileOp->fAnyOperationsAborted=b;
        lpFileOp->fFlags=0;
        lpFileOp->hwnd=0;
        lpFileOp->wFunc=FO_COPY;
        lpFileOp->pTo="C:/Test2\0\0";
        lpFileOp->pFrom="C:/Test1\0\0";
        SHFileOperation(lpFileOp);
        return 0;
    }
    


  • lpFileOp->pTo="C:\\Test2\0\0";
    


  • \\ oder / , es funktioniert trotzdem nicht.

    Hat nicht mal jemand nen Codeschnipsel?



  • Wie wäre es denn, wenn du wirklich mal eine Instanz der Struktur anlegtest, und nicht nur ständig mit einem uninitialisierten Zeiger hantierst?



  • Und wie mach ich das?



  • Indem du SHFILEOPSTRUCT benutzt, nicht LPSHFILEOPSTRUCT. Denn das ist nur ein Zeiger, was du eigentlich auch daran hättest merken müssen, dass du -> benutzen musstest.



  • Also...das geht so (bitte auch versuchen zu verstehen 😉 ):

    /*
    Header:
    #include <windows.h>
    #include <shellapi.h>
    Library:
    #pragma comment(lib, "shell32.lib")
    */
    bool CopyFolder(const TCHAR* pszSrcPath, const TCHAR* pszDestPath)
    {
       if(pszSrcPath == NULL || pszDestPath == NULL)
          return (false);
    
       TCHAR* pszBufFrom = new TCHAR[lstrlen(pszSrcPath) + 2];
       TCHAR* pszBufTo   = new TCHAR[lstrlen(pszDestPath) + 2];
       StrCpy(pszBufFrom, pszSrcPath);
       StrCpy(pszBufTo, pszDestPath);
       pszBufFrom[lstrlen(pszBufFrom) - 1] = 0;
       pszBufTo[lstrlen(pszBufTo) - 1]     = 0;
    
       SHFILEOPSTRUCT shFileOp;
       shFileOp.hwnd                  = NULL;
       shFileOp.wFunc                 = FO_COPY;
       shFileOp.pFrom                 = pszBufFrom;
       shFileOp.pTo                   = pszBufTo;
       shFileOp.fFlags                = FOF_NO_UI | FOF_SILENT; // Hier ggf. andere/weitere ...
       shFileOp.fAnyOperationsAborted = FALSE;
       shFileOp.hNameMappings         = NULL;
       shFileOp.lpszProgressTitle     = NULL; // siehe Hinweis zu 'shFileOp.fFlags'
    
       bool fSuccess = !static_cast<bool>(SHFileOperation(&shFileOp));
    
       delete [] pszBufFrom;
       delete [] pszBufTo;
    
       return (fSuccess);
    }
    

    Beispiel:

    if(CopyFolder(TEXT("C:\\DemoFolderSrc"), TEXT("C:\\DemoFolderDest")))
       MessageBox(NULL, TEXT("Der Ordner wurde erfolgreich kopiert."), TEXT("Kopiervorgang erfolgreich beendet."), MB_OK);
    

    Ist aber nicht getestet ⚠ ➡ 😋 .



  • CodeFinder schrieb:

    StrCpy(...);
    

    Kenne ich nicht... 😉



  • Irgendwie klappts nicht...

    bool CopyFolder(const TCHAR* pszSrcPath, const TCHAR* pszDestPath)
    {
       if(pszSrcPath == NULL || pszDestPath == NULL)
          return (false);
    
       TCHAR* pszBufFrom = new TCHAR[lstrlen(pszSrcPath) + 2];
       TCHAR* pszBufTo   = new TCHAR[lstrlen(pszDestPath) + 2];
       strcpy(pszBufFrom, pszSrcPath);
       strcpy(pszBufTo, pszDestPath);
       pszBufFrom[lstrlen(pszBufFrom) - 1] = 0;
       pszBufTo[lstrlen(pszBufTo) - 1]     = 0;
    
       SHFILEOPSTRUCT shFileOp;
       shFileOp.hwnd                  = NULL;
       shFileOp.wFunc                 = FO_COPY;
       shFileOp.pFrom                 = pszBufFrom;
       shFileOp.pTo                   = pszBufTo;
       shFileOp.fFlags                = /*FOF_NO_UI | -> gibts gar nicht!*/FOF_SILENT; // Hier ggf. andere/weitere ...
       shFileOp.fAnyOperationsAborted = FALSE;
       shFileOp.hNameMappings         = NULL;
       shFileOp.lpszProgressTitle     = NULL; // siehe Hinweis zu 'shFileOp.fFlags'
    
       bool fSuccess = !static_cast<bool>(SHFileOperation(&shFileOp));
    
       delete [] pszBufFrom;
       delete [] pszBufTo;
    
       return (fSuccess);
    }
    int main()
    {
        if(CopyFolder(TEXT("C:\\Test1"), TEXT("C:\\Test2")))
       MessageBox(NULL, TEXT("Der Ordner wurde erfolgreich kopiert."), TEXT("Kopiervorgang erfolgreich beendet."), MB_OK);
    
        return 0;
    }
    


  • tsp schrieb:

    Irgendwie klappts nicht...

    Das nenne ich mal eine umfassende und ausführliche Fehlerbeschreibung.

    Mach aus dem -1 mal +1.



  • pszBufFrom[lstrlen(pszBufFrom)] = 0;
       pszBufTo[lstrlen(pszBufTo)]     = 0;
    

    EDIT: rrr... 😉



  • WebFritzi schrieb:

    CodeFinder schrieb:

    StrCpy(...);
    

    Kenne ich nicht... 😉

    Ich mach Dich gerne bekannt 😉 : http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/shlwapi/string/strcpy.asp

    WebFritzi schrieb:

    pszBufFrom[lstrlen(pszBufFrom)] = 0;
       pszBufTo[lstrlen(pszBufTo)]     = 0;
    

    EDIT: rrr... 😉

    Jupp, stimmt 👍 !

    tsp schrieb:

    /*FOF_NO_UI | -> gibts gar nicht!*/
    

    Natürlich gibt es das, siehe hier: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/structures/shfileopstruct.asp

    MSDN schrieb:

    fFlags
    Flags that control the file operation. This member can take a combination of the following flags.
    [...]
    FOF_NO_UI
    Perform the operation with no user input.

    Du hast warscheinlich nur kein aktuelles SDK installiert 🤡 .


Anmelden zum Antworten