borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (compression - anyone playing with ICSharpCode.SharpZipLib.GZip - help...)Next Topic (SetTcpEntry) New Topic New Poll Post Reply
AndreaVB Forum : VB.Net : Copy files
Poster Message
Anita
Level: Big Cheese

Registered: 19-05-2005
Posts: 24

icon Copy files

I am trying to copy files from one computer on a network to another computer on a network.  Using VB .Net.  I see a file and directory object in the system.io namespace.  But I don't understand how to copy several files within a folder using these objects.  Can someone help me with this.  Thanks!

19-05-2005 at 06:45 PM
View Profile Send Email to User Show All Posts | Quote Reply
humberto
Level: VB Lord

Registered: 13-01-2005
Posts: 246
icon Re: Copy files


' Recursively copy all files and subdirectories from the
' specified source to the specified destination.
Private Sub RecursiveCopyFiles( _
    ByVal sourceDir As String, _
    ByVal destDir As String, _
    ByVal fRecursive As Boolean)

    Dim i As Integer
    Dim posSep As Integer
    Dim sDir As String
    Dim aDirs() As String
    Dim sFile As String
    Dim aFiles() As String

    ' Add trailing separators to the supplied paths if they don't exist.
  
If Not sourceDir.EndsWith(System.IO.path.DirectorySeparatorChar.ToString()) Then
        sourceDir& = System.IO.path.DirectorySeparatorChar
    End If

    
If Not destDir.EndsWith(System.IO.path.DirectorySeparatorChar.ToString()) Then
destDir& = System.IO.path.DirectorySeparatorChar
    End If

    ' Recursive switch to continue drilling down into dir structure.
    If fRecursive Then

        ' Get a list of directories from the current parent.
        aDirs = System.IO.Directory.GetDirectories(sourceDir)

        For i = 0 To aDirs.GetUpperBound(0)

            ' Get the position of the last separator in the current path.
            posSep = aDirs(i).LastIndexOf("\")

            ' Get the path of the source directory.
            sDir = aDirs(i).Substring((posSep + 1), aDirs(i).Length - (posSep + 1))

            ' Create the new directory in the destination directory.
            System.IO.Directory.CreateDirectory (destDir + sDir)

            ' Since we are in recursive mode, copy the children also
            RecursiveCopyFiles(aDirs(i), (destDir + sDir), fRecursive)
        Next

    End If

    ' Get the files from the current parent.
    aFiles = System.IO.Directory.GetFiles(sourceDir)

    ' Copy all files.
    For i = 0 To aFiles.GetUpperBound(0)

        ' Get the position of the trailing separator.
        posSep = aFiles(i).LastIndexOf("\")

        ' Get the full path of the source file.
        sFile = aFiles(i).Substring((posSep + 1), aFiles(i).Length - (posSep + 1))

        ' Copy the file.
        System.IO.File.Copy(aFiles(i), destDir + sFile)

    Next i

End Sub

20-05-2005 at 11:44 AM
View Profile Send Email to User Show All Posts | Quote Reply
Anita
Level: Big Cheese

Registered: 19-05-2005
Posts: 24
icon Re: Copy files

This code is cool, but it looks like it copies all the files in all directories from the source path to the destination.  But perhaps I don't fully understand the code.

What if I want to copy only the files in the last folder of the source path?  

Thanks!

20-05-2005 at 02:36 PM
View Profile Send Email to User Show All Posts | Quote Reply
admin
Level: Administrator


Registered: 04-04-2002
Posts: 530
icon Re: Copy files

Looking at the code it seems that if you call the sub passing False in fRecursive it will only copy the files from the source path to the destination path without going deeper in subdirectories

RecursiveCopyFiles("C:\temp","D:\temp",true)
to copy all files and all subdirectories from c:\temp into d:\temp

RecursiveCopyFiles("C:\temp","D:\temp",false)
copies only the files in C:\temp and doesn't go deeper in copying subdirectories

____________________________
AndreaVB

20-05-2005 at 02:55 PM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
Anita
Level: Big Cheese

Registered: 19-05-2005
Posts: 24
icon Re: Copy files

Great, now I get it.  Thanks!

20-05-2005 at 03:03 PM
View Profile Send Email to User Show All Posts | Quote Reply
Anita
Level: Big Cheese

Registered: 19-05-2005
Posts: 24
icon Re: Copy files

This works really great, but now I want to copy only a directory and files in the last directory of the path.  For example,

"c:\temp\filesanddir"    

Filesanddir contains some files and another dir with files.  I only want to copy the directory and files that are in filesanddir and not any files or dirs in temp.  

Can I tweak the code to do this?  Thanks.

24-05-2005 at 07:55 PM
View Profile Send Email to User Show All Posts | Quote Reply
Anita
Level: Big Cheese

Registered: 19-05-2005
Posts: 24
icon Re: Copy files

Well, after further testing, it looks like the code already is doing what I want.  So, that's great.  

How can I make the code create a destination directory if one does not exist.  For example, if the source path is

c:\temp\filesanddir

and the destination is

c:\temp

Thanks for your help!

24-05-2005 at 08:37 PM
View Profile Send Email to User Show All Posts | Quote Reply
Goran
Level: Moderator

Registered: 16-05-2002
Posts: 1681
icon Re: Copy files

Directory Class: System.IO Namespace

Directory.Exists (Path) - check if directory exists

Directory.CreateDirectory (Path) - creates new directory

[Edited by Goran on 25-05-2005 at 06:30 PM GMT]

____________________________
If you find the answer helpful, please mark this topic as solved.

25-05-2005 at 05:08 PM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VB.Net : Copy files
Previous Topic (compression - anyone playing with ICSharpCode.SharpZipLib.GZip - help...)Next Topic (SetTcpEntry) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder