Copy Directorystructure with files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dafmeister
    New Member
    • Nov 2008
    • 5

    Copy Directorystructure with files

    Hello,

    I would like to make a copy function, like xcopy but in c++.I wouldn't use xcopy in my c++ file, because I don't have enough error catching.

    I would like to copy the whole directory structure from source to destination, but if the file or directory exists in the destination, it may not be copied again.

    I wrote this code. But it doesn't work. Can somebody help me please?

    Code:
    SHFILEOPSTRUCT fileop;
    fileop.hwnd = NULL;
    fileop.wFunc = FO_COPY;
    fileop.pFrom = "C:\\a\\*.*\0"; // additional null needed
    fileop.pTo = "C:\\b\\\0"; // additional null needed
    fileop.fFlags = FOF_NOCONFIRMATION|FOF_NOCONFIRMMKDIR |FOF_RENAMEONCOLLISION;
    
    if (!SHFileOperation(&fileop) == NULL)
    {
    printf("copy failed");
    getchar();
    }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    In what way does it not work?

    Comment

    • Dafmeister
      New Member
      • Nov 2008
      • 5

      #3
      Nope, Because if I coppied the directory "foo" (c:\a\foo) to (c:\b\). I get c:\'\foo. Ok, that's correct, but if I copied c:\a\foo again to c:\b\. I get this:

      -c:\
      -b\
      -foo\
      -copy of foo\

      And that's not wat I'm want, I want this, if I coppied the directory again:

      -c:\
      -b\
      -foo\

      If I change a file in c:\a\foo and I copied it to c:\b en foo and the file does exists in this directory, I want that the file becomes overwrite.


      So, Directories may not overwrite and files must be overwrite.

      I hope I make myself clear enough.

      Comment

      • Dafmeister
        New Member
        • Nov 2008
        • 5

        #4
        Originally posted by Dafmeister
        Nope, Because if I coppied the directory "foo" (c:\a\foo) to (c:\b\). I get c:\'\foo. .
        I mean: I get c:\b\foo

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          The FOF_RENAMEONCOL LISION flag you are using specifically tells the function to rename (add "Copy of") if the destination already exists.

          Comment

          • Dafmeister
            New Member
            • Nov 2008
            • 5

            #6
            Yes I know,

            But the directories may not get "Copy of" but files with the same name must get "Copy of"

            How do I do that?

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Instead of calling a shell function you do the work yourself making use of the File management Functions in WIN32.

              With the use of the FOF_NORECURSION flag you could probably still use SHFileOperation to do the file copying while handling the directories yourself.

              Comment

              • Dafmeister
                New Member
                • Nov 2008
                • 5

                #8
                Do you have any tips or an example, because that's the reason why I'm asking.

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #9
                  Not really, I have done this sort of thing in the past but not recently, I think you could use FindFirstEx plus related functions to traverse your directory tree to copy creating directories in the destination as required and copying the files.

                  Comment

                  Working...