?
Solved

Copy files to another directory

Posted on 2005-04-18
5
Medium Priority
?
340 Views
Last Modified: 2013-12-16
I'm trying to create a basic application that copies files from one directory to another. The target directory name can change though. It needs to loop through the main directory and sub directories.

I may be going about it wrong, it tells me the directory does not exist, it has an extra "coldfusion" and if i remove it, it tells me the file "c:\test" already exists. What am I doing wrong?

Sub GetDirectories(ByVal Directory As String)
        Dim di As New DirectoryInfo(Directory)

        For Each fi As FileInfo In di.GetFiles()
            lblStatus.Refresh()
            'lblStatus.Text = fi.Name
            If fi.Extension = ".cfm" Then
                File.Copy("C:\Inetpub\wwwroot\ColdFusion" & "\" & di.Name & "\" & fi.Name, "C:\test")
            End If
        Next
        For Each dir As DirectoryInfo In di.GetDirectories()
            GetDirectories(dir.FullName)

        Next
    End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        GetDirectories("C:\Inetpub\wwwroot\ColdFusion")
    End Sub
0
Comment
Question by:JRockFL
  • 3
5 Comments
 
LVL 21

Expert Comment

by:mastoo
ID: 13807640
Put the source and destination in strings so you can check them, and the destination has to be a file name:

dim sSource as string = "C:\Inetpub\wwwroot\" & di.Name & "\" & fi.Name
dim sDest as string = "C:\test\" & fi.Name
File.Copy( sSource, sDest )
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13807674
   Sub GetDirectories(ByVal Directory As String)
        Dim di As New DirectoryInfo(Directory)

        For Each fi As FileInfo In di.GetFiles()
            lblStatus.Refresh()
            'lblStatus.Text = fi.Name
            If fi.Extension = ".cfm" Then
                fi.CopyTo("C:\test\" & fi.Name)
            End If
        Next
        For Each dir As DirectoryInfo In di.GetDirectories()
            GetDirectories(dir.FullName)
        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        GetDirectories("C:\Inetpub\wwwroot\ColdFusion\")
    End Sub
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13807683
Might not fix the problem... but it saves you building up that string for the source file
0
 
LVL 12

Accepted Solution

by:
S-Twilley earned 2000 total points
ID: 13807711
or even better...

    Sub GetDirectories(ByVal Directory As String)
        Dim di As New DirectoryInfo(Directory)

        For Each fi As FileInfo In di.GetFiles("*.cfm")
            lblStatus.Refresh()
            fi.CopyTo("C:\test\" & fi.Name)
        Next
        For Each dir As DirectoryInfo In di.GetDirectories()
            GetDirectories(dir.FullName)
        Next
    End Sub

==================

fi.CopyTo("C:\test\" & fi.Name)

this lines might be causing you problems... because your code is recursive... it may come across a file with the same name within a different directory... but since you are copying all files to "C:\Test"... there will be overwriting problems... so you can either overwrite... or build the directory structure within "C:\Test"... you need to choose
0
 
LVL 8

Author Comment

by:JRockFL
ID: 13808276
Thank you for your replies. This code your provided works perfectly. You are right though, I will need to build
the directory structure. I will try on my own first, may need your help :)

    Sub GetDirectories(ByVal Directory As String)
        Dim di As New DirectoryInfo(Directory)

        For Each fi As FileInfo In di.GetFiles("*.cfm")
            lblStatus.Refresh()
            fi.CopyTo("C:\test\" & fi.Name)
        Next
        For Each dir As DirectoryInfo In di.GetDirectories()
            GetDirectories(dir.FullName)
        Next
    End Sub
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The purpose of this video is to demonstrate how to automatically show related posts at the bottom of a blog post in WordPress. This will be demonstrated using a Windows 8 PC. Plugin “Yet Another Related Posts Plugin” will be used. Go to your…
The purpose of this video is to demonstrate how to insert an Iframe into WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Open Page or Post…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question