7 Replies - 15820 Views - Last Post: 02 November 2010 - 11:58 AM Rate Topic: -----

#1 koolazngy94  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 119
  • Joined: 29-July 08

Move all files in a folder to another folder?

Posted 01 November 2010 - 08:16 PM

So, I'm really stuck at the moment.

I have 2 textbox, 2 OpenFolderDialog, and 3 buttons.

Here the following code I have so far

Imports System.IO
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        FolderBrowserDialog1.ShowDialog()
        TextBox1.Text = FolderBrowserDialog1.SelectedPath
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        FolderBrowserDialog2.ShowDialog()
        TextBox2.Text = FolderBrowserDialog2.SelectedPath
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        Dim path As String = TextBox2.Text
        Dim destination As String = TextBox1.Text
        For Each hfile In Directory.GetFiles(path)
            File.Copy(path & "/" & hfile, destination)
        Next

    End Sub
End Class


So I'm really stuck at this moment, I want to move all the file in TextBox2 to TextBox1 but it doesn't really copy and move it.

Is This A Good Question/Topic? 0
  • +

Replies To: Move all files in a folder to another folder?

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1030
  • View blog
  • Posts: 4,011
  • Joined: 02-July 08

Re: Move all files in a folder to another folder?

Posted 01 November 2010 - 08:50 PM

I may have the textboxes in the wrong order - consider renaming them to something more meaningful, i.e. txtDestFolder, txtSourceFolder. Or combining this into one function and no need for the textboxes if you store the selectedpaths in variables.

For Each fi As FileInfo In New DirectoryInfo(textbox1.Text).GetFiles
   File.Move(fi.FullName ,Path.Combine(textbox2.Text, fi.Name))
Next 

This post has been edited by hawkvalley1: 01 November 2010 - 08:55 PM

Was This Post Helpful? 0
  • +
  • -

#3 koolazngy94  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 119
  • Joined: 29-July 08

Re: Move all files in a folder to another folder?

Posted 01 November 2010 - 08:59 PM

Well that worked, but there 1 problem left, say there a folder inside the folder, how will I make it move with the files also?

Sorry for the trouble too anyways.
Was This Post Helpful? 0
  • +
  • -

#4 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1030
  • View blog
  • Posts: 4,011
  • Joined: 02-July 08

Re: Move all files in a folder to another folder?

Posted 01 November 2010 - 09:02 PM

You have to iterate thru the first directory to look for other directories then move it.

This post has been edited by hawkvalley1: 01 November 2010 - 09:05 PM

Was This Post Helpful? 0
  • +
  • -

#5 koolazngy94  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 119
  • Joined: 29-July 08

Re: Move all files in a folder to another folder?

Posted 01 November 2010 - 09:07 PM

So basically, go inside the folder and checks if there another directory and repeats the same process as above?
Was This Post Helpful? 0
  • +
  • -

#6 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1030
  • View blog
  • Posts: 4,011
  • Joined: 02-July 08

Re: Move all files in a folder to another folder?

Posted 01 November 2010 - 09:20 PM

Well, you use a loop with the GetDirectories method and then use the Move function just like the other Files.
Was This Post Helpful? 0
  • +
  • -

#7 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 183
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: Move all files in a folder to another folder?

Posted 02 November 2010 - 04:05 AM

So basically something like this:

    For Each hfolder In Directory.GetDirectories(path)
           My.Computer.FileSystem.CopyDirectory(path & "/" & hfolder, destination)
        Next


Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4312
  • View blog
  • Posts: 7,467
  • Joined: 08-June 10

Re: Move all files in a folder to another folder?

Posted 02 November 2010 - 11:58 AM

Let's just bring this all together in one method:
Sub MoveAllItems(ByVal fromPath As String, ByVal toPath As String)
    ''Create the target directory if necessary
    Dim toPathInfo = New DirectoryInfo(toPath)
    If (Not toPathInfo.Exists) Then
        toPathInfo.Create()
    End If
    Dim fromPathInfo = New DirectoryInfo(fromPath)
    ''move all files
    For Each file As FileInfo In fromPathInfo.GetFiles()
        file.MoveTo(Path.Combine(toPath, file.Name))
    Next
    ''move all folders
    For Each dir As DirectoryInfo In fromPathInfo.GetDirectories()
        dir.MoveTo(Path.Combine(toPath, dir.Name))
    Next
End Sub


Or even nicer, an Extension method:
Imports System.Runtime.CompilerServices
Imports System.IO

Module Extensions
    <Extension()>
    Public Sub MoveAllItemsTo(ByVal fromPathInfo As DirectoryInfo, ByVal toPath As String)
        ''Create the target directory if necessary
        Dim toPathInfo = New DirectoryInfo(toPath)
        If (Not toPathInfo.Exists) Then
            toPathInfo.Create()
        End If
        ''move all files
        For Each file As FileInfo In fromPathInfo.GetFiles()
            file.MoveTo(Path.Combine(toPath, file.Name))
        Next
        ''move all folders
        For Each dir As DirectoryInfo In fromPathInfo.GetDirectories()
            dir.MoveTo(Path.Combine(toPath, dir.Name))
        Next
    End Sub
End Module


And now it can be called like this:
Dim fromDir = New DirectoryInfo("C:\dev\folder2")
fromDir.MoveAllItemsTo("C:\dev\folder1")

This post has been edited by insertAlias: 02 November 2010 - 12:03 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1