Results 1 to 4 of 4

Thread: Copy files that exist

  1. #1

    Thread Starter
    Hyperactive Member GlenW's Avatar
    Join Date
    Nov 2001
    Location
    Gateshead, England
    Posts
    479

    Copy files that exist

    I want to write an app that copies files from one directory to another, but only copies files that already exist in the to directory, overwrite them.
    Any ideas?

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142

    Re: Copy files that exist

    you want to copy files to another directory, but only if they already exist in that ' other ' directory ? so basically you want to write over them?
    you can use the System.IO.File.Exists function for this.

    VB Code:
    1. Dim files As String() = IO.Directory.GetFiles("C:\Documents and Settings\den\My Documents\My Pictures")
    2.         For Each s As String In files
    3.             Dim fileinf As New IO.FileInfo(s)
    4.             Dim otherpath As String = "C:\" & fileinf.Name
    5.             If IO.File.Exists(otherpath) Then
    6.                 IO.File.Copy(s, otherpath, True)
    7.             End If
    8.         Next
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  3. #3
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142

    Re: Copy files that exist

    in C#
    Code:
    string[] files = System.IO.Directory.GetFiles(@"C:\Documents and Settings\den\My Documents\My Pictures");
          foreach(string s in files)
          {
                System.IO.FileInfo fileinf = new System.IO.FileInfo(s);
                string otherpath = @"C:\" + fileinf.Name;
                if ( IO.File.Exists(otherpath) )
                {
                    System.IO.File.Copy(s, otherpath, true);
                }
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  4. #4

    Thread Starter
    Hyperactive Member GlenW's Avatar
    Join Date
    Nov 2001
    Location
    Gateshead, England
    Posts
    479

    Re: Copy files that exist

    Thanks, easy when you know how.

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