Solved

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

Posted on 2014-04-10
2
3,204 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

813 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

9 Experts available now in Live!

Get 1:1 Help Now