Copying a whole directory and its content

This is a discussion on Copying a whole directory and its content within the C Programming forums, part of the General Programming Boards category; I've been working on a file manager/mp3 manager for the last week. Now that my user interface and some minor ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    Antwerp, Belgium
    Posts
    9

    Copying a whole directory and its content

    I've been working on a file manager/mp3 manager for the last week.
    Now that my user interface and some minor functions are completed I just need to code the delete and copy functions.
    Delete function went well, I used the unlink and rmdir function but for the copying of the files I am wondering if there would be a command to copy the whole lot.

    Currently I would copy a folder and all it's contents by reading all the files and when I encounter a file I would open it as binary and write it to the destination folder as binary too.
    When I would encounter a directory I woul add it in the destinationdirectory with the mkdir and read it's contents recursively til the directory is copied.

    So my question is: Is there any easier/better way to do the copying?

    Note:
    I am currently using a Windows machine (and a NTFS file system) so windows.h is included.

    Thanks anyway.
    Last edited by Enira; 03-11-2006 at 08:01 AM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    system("cp -r source dest") ?

    Or you could implement it yourself, ie open the source directory and copy each file to the destination. Recursively copying folders. With opendir() or whatever the windows equivalent for that is.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Location
    Antwerp, Belgium
    Posts
    9
    Quote Originally Posted by dwks
    system("cp -r source dest") ?

    Or you could implement it yourself, ie open the source directory and copy each file to the destination. Recursively copying folders. With opendir() or whatever the windows equivalent for that is.
    Yup recursively I though too, that is how I made my delete function.(opendir() is btw correct.)
    I will check the system() function out.
    Experience with the delete function has thought me that a the system() function could sometimes crash the application, but it is worth a shot(saves me a huge amount of time).

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    There are better ways than system() to start a program. The point of my example is that if you have UNIX or Cygwin, cp does exactly what you want.
    Experience with the delete function has thought me that a the system() function could sometimes crash the application
    Ouch. Yes, and it's slow too.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    20
    Good luck on kdg

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21