Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple file copy?

Status
Not open for further replies.

VoodooRageII

Programmer
Aug 25, 2002
14
US
I need to copy files from one directory to another. The problem is that while I can use the folderbrowserdialog to get the folder paths, and I have a source and a destination variable, I need to copy only files that are not already present in the destination and there will constantly be files being written to the source directory. so once copying begins and finishes I will need to check to see if there are any new files in the source and then copy them until there are not any new ones found. I am fairly fluent in VBA but this is actually using VB 2005 Express. File copying is seemingly fairly complicated. Is there a not terribly complicated way to do this?
 
This has been discussed in thread796-1179848.

Just change the Move function to Copy.

Regards.
mansii
 
The following will get all files in a source folder, and then check for the existance of the file in a destination folder, only copying new files across.

You will also need an Imports System.Io
Code:
    Public Sub zCopyFiles(ByVal sFolder1 As String, ByVal sFolder2 As String)
        'Copy files from sFolder1 to sFolder2
        Dim sNewFile As String
        Dim f As FileInfo() = Me.zGetFilesinDirectory(sFolder1)
        For i As Integer = 0 To f.Length - 1
            sNewFile = sFolder2 + f(i).Name
            If Not File.Exists(sNewFile) Then
                File.Copy(f(i).FullName, sNewFile)
            End If
        Next
    End Sub

    Public Function zGetFilesinDirectory(ByVal sDir As String, _
       Optional ByVal sExtension As String = "") As FileInfo()

        If Not Directory.Exists(sDir) Then Return Nothing

        Dim d As New DirectoryInfo(sDir)
        Dim f As FileInfo() = d.GetFiles("*." + sExtension)
        Return f

 1).fullname
    End Function


Sweep
...if it works dont f*** with it
curse.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top