14 Replies - 1829 Views - Last Post: 13 August 2011 - 05:13 AM Rate Topic: ***-- 2 Votes

#1 NY Andrew  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 81
  • Joined: 25-May 11

How can I copy an entire folder to a user selected location?

Posted 11 August 2011 - 11:28 AM

I am trying to copy an entire folder and all of its contents that is in the same location as my program that is running to a user selected location. I started off by using a SaveFileDialog, but now I am working with a FolderBrowserDialog. Here is some code:
Private Sub BackupButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BackupButton.Click
        Dim sourcePath As String
        sourcePath = Application.StartupPath & "\SavedFiles\"
        Dim destPath As String


        FolderBrowserDialog1.RootFolder = Environment.SpecialFolder.Desktop
        If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            destPath = FolderBrowserDialog1.SelectedPath
        End If

        Dim dir As New DirectoryInfo(sourcePath)
        Dim infos As FileSystemInfo() = dir.GetFileSystemInfos
        BackupFiles(infos)

    End Sub


    Private sourcePath As String
    Private destPath As String

    Private Sub BackupFiles(ByVal FSInfo() As FileSystemInfo)
        Dim i As FileSystemInfo
        For Each i In FSInfo
            If TypeOf i Is DirectoryInfo Then
                    'i is a Directory
                    Dim sourceDir As String = i.FullName.ToString
                    'Matching dest-filespec
                    Dim destDir As String = Replace(sourceDir, sourcePath, destPath)
                    'If Dir in Backup-Destination not exists, copy Dir with SubDirs and Files
                    If Not My.Computer.FileSystem.DirectoryExists(destDir) Then
                        My.Application.DoEvents()
                        My.Computer.FileSystem.CopyDirectory(sourceDir, destDir, True)
                        Continue For
                    Else
                        Dim dInfo As DirectoryInfo = CType(i, DirectoryInfo)
                        'Start the loop again for existing Dirs to check for new Subdirs and Files that must be backuped
                        BackupFiles(dInfo.GetFileSystemInfos)
                    End If
                ElseIf TypeOf i Is FileInfo Then
                    Dim sourceFile As String = i.FullName.ToString
                    Dim destFile As String = Replace(sourceFile, sourcePath, destPath)
                    If Not File.Exists(destFile) Then
                        My.Application.DoEvents()
                        File.Copy(sourceFile, destFile, True)
                    Else
                        If File.GetLastWriteTime(sourceFile) > File.GetLastWriteTime(destFile) Then
                            My.Application.DoEvents()
                            File.Copy(sourceFile, destFile, True)
                        End If
                    End If
                End If
        Next
    End Sub


But when I run the program, it works, but it does not copy the entire folder in the application startup location.. Any ideas how I can copy that folder that is in the same location as my program to a user selected location?

Is This A Good Question/Topic? 0
  • +

Replies To: How can I copy an entire folder to a user selected location?

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13491
  • View blog
  • Posts: 53,888
  • Joined: 12-June 08

Re: How can I copy an entire folder to a user selected location?

Posted 11 August 2011 - 11:47 AM

Well, what part doesn't it copy? The running application?
Was This Post Helpful? 0
  • +
  • -

#3 NY Andrew  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 81
  • Joined: 25-May 11

Re: How can I copy an entire folder to a user selected location?

Posted 11 August 2011 - 11:52 AM

View Postmodi123_1, on 11 August 2011 - 11:47 AM, said:

Well, what part doesn't it copy? The running application?

It's not copying the ENTIRE FOLDER in that location..
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13491
  • View blog
  • Posts: 53,888
  • Joined: 12-June 08

Re: How can I copy an entire folder to a user selected location?

Posted 11 August 2011 - 11:55 AM

Again - what sort of files are not being copied?
Was This Post Helpful? 0
  • +
  • -

#5 NY Andrew  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 81
  • Joined: 25-May 11

Re: How can I copy an entire folder to a user selected location?

Posted 11 August 2011 - 11:59 AM

View Postmodi123_1, on 11 August 2011 - 11:55 AM, said:

Again - what sort of files are not being copied?

Ok, so there is a folder that's called "SavedFiles" in the same folder as my program. All the files inside that folder "SavedFiles" are .txts and xmls..
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13491
  • View blog
  • Posts: 53,888
  • Joined: 12-June 08

Re: How can I copy an entire folder to a user selected location?

Posted 11 August 2011 - 12:08 PM

.. and it is only moving a portion of those files? Are they being used by another process at the time?
Was This Post Helpful? 0
  • +
  • -

#7 NY Andrew  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 81
  • Joined: 25-May 11

Re: How can I copy an entire folder to a user selected location?

Posted 11 August 2011 - 12:10 PM

View Postmodi123_1, on 11 August 2011 - 12:08 PM, said:

.. and it is only moving a portion of those files? Are they being used by another process at the time?

No, it is not moving ANY of those files.. That's why I am here to get some help on how to copy a folder to another location or something...
Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: How can I copy an entire folder to a user selected location?

Posted 11 August 2011 - 12:12 PM

Why aren't you simply copying the directory?
MSDN CopyDirectory
Was This Post Helpful? 0
  • +
  • -

#9 fixo  Icon User is offline

  • D.I.C Regular

Reputation: 85
  • View blog
  • Posts: 335
  • Joined: 10-May 09

Re: How can I copy an entire folder to a user selected location?

Posted 11 August 2011 - 12:14 PM

One more link:
http://msdn.microsof...62914.aspx#Y131
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13491
  • View blog
  • Posts: 53,888
  • Joined: 12-June 08

Re: How can I copy an entire folder to a user selected location?

Posted 11 August 2011 - 12:16 PM

Quote

But when I run the program, it works, but it does not copy the entire folder in the application startup location..


Quote

No, it is not moving ANY of those files..


Okay, odd way of phrasing it - it works but doesn't. :sigh:

Back to my other question - is any application currently using those files? Do you an error and a crash?
Was This Post Helpful? 0
  • +
  • -

#11 NY Andrew  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 81
  • Joined: 25-May 11

Re: How can I copy an entire folder to a user selected location?

Posted 11 August 2011 - 12:21 PM

View PostAdamSpeight2008, on 11 August 2011 - 12:12 PM, said:

Why aren't you simply copying the directory?
MSDN CopyDirectory

View Postfixo, on 11 August 2011 - 12:14 PM, said:


I am looking at those right now, but have checked them out in the past. I cannot figure out how to implement them into the SaveFileDialog though. For example
My.Computer.FileSystem.CopyDirectory("sourcePath, SAVEFILEDIALOG SLECTED PATH GOES HERE SOMEHOW, True)
I am not sure how to get the user selected save location.

View Postmodi123_1, on 11 August 2011 - 12:16 PM, said:

Quote

But when I run the program, it works, but it does not copy the entire folder in the application startup location..

Quote

No, it is not moving ANY of those files..

Okay, odd way of phrasing it - it works but doesn't. :sigh:

Back to my other question - is any application currently using those files? Do you an error and a crash?

Exactly. And no I do not get an error or a crash, and no those files are not in use.
Was This Post Helpful? 0
  • +
  • -

#12 fixo  Icon User is offline

  • D.I.C Regular

Reputation: 85
  • View blog
  • Posts: 335
  • Joined: 10-May 09

Re: How can I copy an entire folder to a user selected location?

Posted 11 August 2011 - 02:22 PM

Here is working code mostly from MSDN examples
Form
Imports System
Imports System.IO
Public Class Form1

    Private sourcePath As String
    Private destPath As String

    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        With FolderBrowserDialog1
            .Description = "Hit new folder Button, then enter a name of new folder."
            .RootFolder = Environment.SpecialFolder.Desktop
            .SelectedPath = Environment.SpecialFolder.Desktop
            If .ShowDialog = Windows.Forms.DialogResult.OK Then
                destPath = .SelectedPath
            End If
        End With
        sourcePath = Application.StartupPath & "\SavedFiles\"
        If Not Directory.Exists(sourcePath) Then
            MsgBox("Source folder does not exist")
            Return
        End If
        CopyDir.CopyAll(New DirectoryInfo(sourcePath), New DirectoryInfo(destPath))
    End Sub

End Class


CopyDir.vb

Imports System
Imports System.IO
''from MSDN
Class CopyDir
    Shared Sub CopyAll(ByVal source As DirectoryInfo, ByVal target As DirectoryInfo)
        If (source.FullName.ToLower() = target.FullName.ToLower()) Then
            Return
        End If

        ' Check if the target directory exists, if not, create it.
        If Directory.Exists(target.FullName) = False Then
            Directory.CreateDirectory(target.FullName)
        End If

        ' Copy each file into it's new directory.
        For Each fi As FileInfo In source.GetFiles()
            Console.WriteLine("Copying {0}\{1}", target.FullName, fi.Name)
            fi.CopyTo(Path.Combine(target.ToString(), fi.Name), True)
        Next

        ' Copy each subdirectory using recursion.
        For Each diSourceSubDir As DirectoryInfo In source.GetDirectories()
            Dim nextTargetSubDir As DirectoryInfo = target.CreateSubdirectory(diSourceSubDir.Name)
            CopyAll(diSourceSubDir, nextTargetSubDir)
        Next
    End Sub


End Class


This will create new folder and copy your sorce folder content
in there
Was This Post Helpful? 1
  • +
  • -

#13 NY Andrew  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 81
  • Joined: 25-May 11

Re: How can I copy an entire folder to a user selected location?

Posted 11 August 2011 - 03:35 PM

View Postfixo, on 11 August 2011 - 02:22 PM, said:

Here is working code mostly from MSDN examples
Form
Imports System
Imports System.IO
Public Class Form1

    Private sourcePath As String
    Private destPath As String

    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        With FolderBrowserDialog1
            .Description = "Hit new folder Button, then enter a name of new folder."
            .RootFolder = Environment.SpecialFolder.Desktop
            .SelectedPath = Environment.SpecialFolder.Desktop
            If .ShowDialog = Windows.Forms.DialogResult.OK Then
                destPath = .SelectedPath
            End If
        End With
        sourcePath = Application.StartupPath & "\SavedFiles\"
        If Not Directory.Exists(sourcePath) Then
            MsgBox("Source folder does not exist")
            Return
        End If
        CopyDir.CopyAll(New DirectoryInfo(sourcePath), New DirectoryInfo(destPath))
    End Sub

End Class


CopyDir.vb

Imports System
Imports System.IO
''from MSDN
Class CopyDir
    Shared Sub CopyAll(ByVal source As DirectoryInfo, ByVal target As DirectoryInfo)
        If (source.FullName.ToLower() = target.FullName.ToLower()) Then
            Return
        End If

        ' Check if the target directory exists, if not, create it.
        If Directory.Exists(target.FullName) = False Then
            Directory.CreateDirectory(target.FullName)
        End If

        ' Copy each file into it's new directory.
        For Each fi As FileInfo In source.GetFiles()
            Console.WriteLine("Copying {0}\{1}", target.FullName, fi.Name)
            fi.CopyTo(Path.Combine(target.ToString(), fi.Name), True)
        Next

        ' Copy each subdirectory using recursion.
        For Each diSourceSubDir As DirectoryInfo In source.GetDirectories()
            Dim nextTargetSubDir As DirectoryInfo = target.CreateSubdirectory(diSourceSubDir.Name)
            CopyAll(diSourceSubDir, nextTargetSubDir)
        Next
    End Sub


End Class


This will create new folder and copy your sorce folder content
in there

Hey, thanks a million fixo!! After a little bit of tweaking you code, I successfully got it working! Thank you! The only thing I need to figure out how to do now, is how to create a new folder automatically and call it "SavedFiles" when the user selects the output location.
Was This Post Helpful? 0
  • +
  • -

#14 NY Andrew  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 81
  • Joined: 25-May 11

Re: How can I copy an entire folder to a user selected location?

Posted 11 August 2011 - 04:06 PM

For those who care, I just figured out how to create a new folder called "SavedFiles" and place the copied contents inside of it, AND place that folder where the user chooses.
I added
+ "\SavedFiles\"
to the original code
CopyDir.CopyAll(New DirectoryInfo(sourcePath), New DirectoryInfo(destPath))
to make it look like:
CopyDir.CopyAll(New DirectoryInfo(sourcePath), New DirectoryInfo(destPath + "\SavedFiles\"))


Once again thank you all for your input! :bigsmile:
Was This Post Helpful? 1
  • +
  • -

#15 fixo  Icon User is offline

  • D.I.C Regular

Reputation: 85
  • View blog
  • Posts: 335
  • Joined: 10-May 09

Re: How can I copy an entire folder to a user selected location?

Posted 13 August 2011 - 05:13 AM

Glad, you solved it,
I've forgot to tell, you could be want look at
Path.Combine method for creating path string
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1