Solved

VB.net System.IO Copy all files from a folder to another folder

Posted on 2014-04-10
2
3,157 Views
Last Modified: 2014-04-10
Hi

What VB.net code would I use to move all the files in a folder to another folder using
System.IO?

Thanks
0
Comment
Question by:murbro
2 Comments
 
LVL 27

Accepted Solution

by:
MacroShadow earned 500 total points
ID: 39993328
        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

Open in new window


Call it like this:
	Dim fromDir = New DirectoryInfo("C:\dev\folder2")
	fromDir.MoveAllItemsTo("C:\dev\folder1")

Open in new window


http://www.dreamincode.net/forums/topic/197897-move-all-files-in-a-folder-to-another-folder/page__view__findpost__p__1157022?s=8cdb5dc39aa26dec600e40e0dc94b3c3
0
 

Author Closing Comment

by:murbro
ID: 39993435
Thanks very much
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
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 …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now