Alright, so in yesterday's thread, I asked for the codes to copy the contents of a directory to a separate directory. The code:

My.Computer.FileSystem.CopyDirectory( , , )

worked fine.
However, I have decided that I wish to add a progress bar into the mix so that the user can see the progress of the transfer \ copy. So far:

btn_Quit.Enabled = False
        Dim path As String = Application.StartupPath
        Dim prgpath As String = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86)
        Dim datfldr As String = (path + "\a")
        Dim prgfldr As String = (prgpath + "\1")
        IO.Directory.CreateDirectory(prgfldr)
        Dim prgfldr2 As String = (prgfldr + "\2")
        '' ------------------------------------------
        ProgressBar1.Maximum = IO.Directory.GetFiles(datfldr, "*", IO.SearchOption.AllDirectories).Length
        For Each file In IO.Directory.GetFiles(datfldr, "*", IO.SearchOption.AllDirectories)
            Dim dest As String = prgfldr2
            My.Computer.FileSystem.CopyFile(file, dest)
            ProgressBar1.Value += 1
        Next
        '' ------------------------------------------
        ''My.Computer.FileSystem.CopyDirectory(datfldr, prgfldr2, True)
        btn_Quit.Enabled = True

is my installation button, but it only returns the folder "2" as an empty file with no extension. I have tried using IO.File.Copy aswell, but the instructions on MSDN arent working at all.
Any ideas on this, my newest problem would be appreciated.

Recommended Answers

All 12 Replies

Updated code from your other thread.
1 Button, 1 ProgressBar

Imports System.IO
Public Class Form1
    Private sTemp1, sTemp2 As String '// TEMP.Strings, used as needed.

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim fbd As New FolderBrowserDialog : sTemp1 = "" : sTemp2 = ""
        With fbd
            .Description = "Select a folder to copy the content FROM..." '// Source.Folder
            If .ShowDialog = Windows.Forms.DialogResult.OK Then sTemp1 = .SelectedPath
            .Description = "Select a folder to copy the content TO..." '// Destination.Folder
            If .ShowDialog = Windows.Forms.DialogResult.OK Then sTemp2 = .SelectedPath
            If Not sTemp1 = "" OrElse Not sTemp2 = "" Then copyDirectory(sTemp1, sTemp2, , ProgressBar1)
        End With
    End Sub

    Private Sub copyDirectory(ByVal selCoolSourcePath As String, ByVal selDestinationPath As String, Optional selCoolFileExtension As String = "*.*", Optional selCoolProgressBar As ProgressBar = Nothing)
        sTemp1 = ""
        With selCoolProgressBar
            If Not selCoolProgressBar Is Nothing Then '// if ProgressBar sent, set .Value and .Maximum.
                .Value = 0
                .Maximum = Directory.GetFiles(selCoolSourcePath, selCoolFileExtension, IO.SearchOption.AllDirectories).Length
            End If
            If Not Directory.Exists(selDestinationPath) Then Directory.CreateDirectory(selDestinationPath) '// create Folder if needed.
            For Each myCoolFile As String In My.Computer.FileSystem.GetFiles _
                                                     (selCoolSourcePath, FileIO.SearchOption.SearchAllSubDirectories, selCoolFileExtension) '// loop thru Source.Files.
                sTemp1 = selDestinationPath & "\" & Path.GetFileName(myCoolFile) '// set .DestinationPath w/only the filename+extension.
                If Not File.Exists(sTemp1) Then '// if .File Not.Exist, procced, Else it will error "for.now".
                    File.Copy(myCoolFile, sTemp1) '// copy File.over.
                Else
                    ' MsgBox("File.Exists in Destination.Folder, cannot copy." & vbNewLine & sTemp1, MsgBoxStyle.Information)
                End If
                If Not selCoolProgressBar Is Nothing Then .Value += 1 '// +=1 .Value, if ProgressBar sent.
            Next
        End With
        MsgBox(".done.", MsgBoxStyle.Information)
    End Sub
End Class

.line: 16. Private Sub copyDirectory(...) , has an Optional file.extension. It is currently set to get all files ("*.*") , though it can be set to only get certain file .extension types.

If Not sTemp1 = "" OrElse Not sTemp2 = "" Then copyDirectory(sTemp1, sTemp2, "*.png", ProgressBar1)

Hope this helps. :)

HOORAY FOR CODEORDER!!! If I could +rep you ten I would!
Thanks!

Oh, but wait.....
I just tried it out, and it doesn't keep the folder system.
ex. I copied fldr1 to fldr2

fldr1
/folder
//rndmflash.fla
/textdoc.txt

in fldr2 now,

fldr2
/rndmflash.fla
/textdoc.txt

Ideas on how to fix that?

Actually, I think I have it alnost figured out....
I have modified your script a bit, but am having problems getting one variable to return the proper value.

Private Sub copyDirectory(ByVal selCoolSourcePath As String, ByVal selDestinationPath As String, Optional ByVal selCoolFileExtension As String = "*.*", Optional ByVal selCoolProgressBar As ProgressBar = Nothing)
        sTemp1 = ""
        sTemp2 = ""
        With selCoolProgressBar
            If Not selCoolProgressBar Is Nothing Then '// if ProgressBar sent, set .Value and .Maximum.
                .Value = 0
                .Maximum = Directory.GetFiles(selCoolSourcePath, selCoolFileExtension, IO.SearchOption.AllDirectories).Length
            End If
            If Not Directory.Exists(selDestinationPath) Then Directory.CreateDirectory(selDestinationPath) '// create Folder if needed.
            For Each subdir As String In My.Computer.FileSystem.GetDirectories(selCoolSourcePath, FileIO.SearchOption.SearchAllSubDirectories)
                sTemp2 = selDestinationPath & "\" & Path.GetDirectoryName(subdir)
                If Not Directory.Exists(sTemp2) Then
                    Directory.CreateDirectory(sTemp2)
                End If
                For Each myCoolFile As String In My.Computer.FileSystem.GetFiles _
(subdir, FileIO.SearchOption.SearchTopLevelOnly, selCoolFileExtension) '// loop thru Source.Files.
                    sTemp1 = selDestinationPath & "\" & Path.GetFileName(myCoolFile) '// set .DestinationPath w/only the filename+extension.
                    If Not File.Exists(sTemp1) Then '// if .File Not.Exist, procced, Else it will error "for.now".
                        File.Copy(myCoolFile, sTemp1) '// copy File.over.
                    Else
                        ' MsgBox("File.Exists in Destination.Folder, cannot copy." & vbNewLine & sTemp1, MsgBoxStyle.Information)
                    End If
                    If Not selCoolProgressBar Is Nothing Then .Value += 1 '// +=1 .Value, if ProgressBar sent.
                Next
            Next
        End With
        MsgBox(".done.", MsgBoxStyle.Information)
    End Sub

The problem I am having is with

Path.GetDirectoryName(subdir)

on line 11. I was hoping that it would return the folders name, and not the path...
Help?

Add this just below line 11.

'/////////////////
                With Path.GetDirectoryName(subdir)
                    MsgBox(.Substring(.LastIndexOf("\") + 1)) '// getFolderName only. :)
                    '//////
                    End '// TERMINATE APPLICATION (FOR TESTING)
                End With
                '/////////////////

So this above line 11 should work?

''///////////////
With Path.GetDirectoryName(subdir)
    Dim sTemp3 as String = .Substring(.LastIndexOf("\") + 1)
End With
sTemp2 = selDestinationPath & "\" & sTemp3

Augghhh! I don't even think I want this Progress Bar anymore...
The Transfer now works, however, it pastes files in the wrong locations...
I think I'm just going to drop the progress bar and say it's done.

q.q.(quick.question)
.what exactly are you trying to do? GetFolderName And Then what???
If it is to create the Directory and place the File in it, see if this helps.

Public Sub xNewCoolFolder(ByVal selCoolFolder As String)
        If Not Directory.Exists(selCoolFolder) Then Directory.CreateDirectory(selCoolFolder)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        xNewCoolFolder(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\myCoolFolder")
    End Sub

Might help if you create a new Directory andThen .Move.File.over.

I am trying to have a folder with all of it's sub-folders and files copied to a new directory.
I am attempting to have this/these actions tracked by a Progress Bar.
I want to have the script create the folder, then move the files in to it. Then have that process repeated with all of the sub-folders.

I have currently made a new, simpler version without the progress bar for now, but I am still very interested in fixing this because this is difficult and I am relatively new.
I understand that to copy these files I will need to use a 'For' statement, and I understand how to get the progress bar working with that now, but the main problem for this at the moment is finding a file copying method that will work with the 'For' loop AND either save the folder system or work well with a Directory.CreateDirectory method.

All of the methods I have tried thus far have either
A) Lost the folder system
B) Told me that my path format was incorrect (couldn't find a single thing wrong with it)
or C) Didn't work with the progress bar at all.

I am curious now, after writing this if a "With" statement attached to the Progress Bar containing one of the copy methods would properly track these actions.
I will test this later, but as for now, I have other work that needs doing, and will check back in a couple of days, if not sooner.

From the code posted and the link to get a ListBox loaded w/the sub.directories, I think that, sec...

Might help if you create a new Directory andThen .Move.File.over.

...into that New.Directory.

.my.apologies "vb.noob":D, I left some info out.:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.