Results 1 to 7 of 7

Thread: Copy files from one location to another given location

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Location
    Kolkata, India
    Posts
    290

    Copy files from one location to another given location

    Hi,

    How to copy all files from one folder to another folder and also rename of all files in destination folder

    Thanks,

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Copy files from one location to another given location

    Directory.GetFiles will give you an array containing the path of all files in a folder. You can loop through that and call File.Copy for each one. You can transform the file name however is appropriate. Path.GetFileName and .GetFileNameWithoutExtension might be handy in getting the original file name as a source for that transformation.

  3. #3
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Copy files from one location to another given location

    Doesn't GetFiles call the API FindFirstFile which dates back to around Windows 2000. Wouldn't EnumerateFiles() be a better solution?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Copy files from one location to another given location

    Quote Originally Posted by ident View Post
    Doesn't GetFiles call the API FindFirstFile which dates back to around Windows 2000. Wouldn't EnumerateFiles() be a better solution?
    Unless you're dealing with a large number of files, it will make no significant difference. If you're using .NET 4.0 or later though, using EnumerateFiles will not hurt.

  5. #5
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Copy files from one location to another given location

    I wouldn't expect there to be any performance difference. Just appears to fit better.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Location
    Kolkata, India
    Posts
    290

    Re: Copy files from one location to another given location

    Copy file from one location to another can be done using file.copy but problem is while copy rename the file too.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Copy files from one location to another given location

    Quote Originally Posted by asm View Post
    Copy file from one location to another can be done using file.copy but problem is while copy rename the file too.
    When you call File.Copy you have to specify the destination file path. If you want the name destination name to be different to the source name then you use a different name in the destination path to the source path. I already addressed that in my first reply.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width