mcerk Posted February 18, 2005 Posted February 18, 2005 Hi. How can I copy all files in folder (and subfolders to another location?) I know how to use FileCopy or Kill functions. tx a lot matej Quote
HJB417 Posted February 18, 2005 Posted February 18, 2005 using System; using System.Collections.Specialized; using System.Text.RegularExpressions; using System.IO; namespace ConsoleApplication1 { public class Class1 { static void Main() { retry_src: Console.Write("Enter src path: "); string path = Console.ReadLine(); if(!Directory.Exists(path)) goto retry_src; Console.Write("Enter dest path: "); string dest = Console.ReadLine(); Regex regex = new Regex("(?i)^" + Regex.Escape(path)); NameValueCollection results = new NameValueCollection(); GetFiles(path, results); foreach(string file in results) { FileInfo destFile = new FileInfo(regex.Replace(file, dest)); //destFile.Directory.Create(); //create the dest directory FileInfo srcFile = new FileInfo(file); //srcFile.CopyTo(destFile.FullName); // copy the file. Console.WriteLine("{0} -> {1}", srcFile, destFile); } } private static void GetFiles(string path, NameValueCollection results) { foreach(string subdirectory in Directory.GetDirectories(path)) GetFiles(subdirectory, results); foreach(string file in Directory.GetFiles(path)) results[file] = file; } } } Quote
SonicBoomAu Posted February 20, 2005 Posted February 20, 2005 This is what I used (Visual Basic) Imports System Imports System.IO Imports System.IO.IsolatedStorage Private Sub Copy_Info() Dim strFolders() As String = IO.Directory.GetDirectories(Me.txtSourceLocation.Text) Dim NewPath As String = strFullAccountTarget Dim strFileExt As String Dim strFileExt1 As String 'Get all the files in the main Directory Dim MainFiles() As String = IO.Directory.GetFiles(Me.txtSourceLocation.Text) Dim FileToCopy As String 'For each file in Source Directory copy to Target Directory For Each FileToCopy In MainFiles Dim f As IO.FileInfo = New IO.FileInfo(FileToCopy) strFileExt = Microsoft.VisualBasic.Left(f.Name, (Len(f.Name) - 3)) strFileExt = Microsoft.VisualBasic.Right(strFileExt, 1) If strFileExt = "h" Then strFileExt = Microsoft.VisualBasic.Left(f.Name, (Len(f.Name) - 4)) strFileExt = Microsoft.VisualBasic.Right(strFileExt1, 1) End If If strFileExt = "." Then IO.File.Copy(FileToCopy, NewPath & f.Name) Else IO.File.Copy(FileToCopy, NewPath & f.Name & f.Extension) End If 'IO.File.Copy(FileToCopy, NewPath & f.Name & f.Extension) Next 'Get all the SubFolders and Copy to next directory Dim Path As String For Each Path In strFolders Dim di As New IO.DirectoryInfo(Path) Dim SubFile As String = NewPath & di.Name & "\" IO.Directory.CreateDirectory(SubFile) 'Get all the files in the Sub folders Dim FilePath As String For Each FilePath In IO.Directory.GetFiles(di.FullName) Dim fi As New IO.FileInfo(FilePath) strFileExt1 = Microsoft.VisualBasic.Left(fi.Name, (Len(fi.Name) - 3)) strFileExt1 = Microsoft.VisualBasic.Right(strFileExt1, 1) If strFileExt1 = "h" Then strFileExt1 = Microsoft.VisualBasic.Left(fi.Name, (Len(fi.Name) - 4)) strFileExt1 = Microsoft.VisualBasic.Right(strFileExt1, 1) End If If strFileExt1 = "." Then IO.File.Copy(FilePath, SubFile & fi.Name) Else IO.File.Copy(FilePath, SubFile & fi.Name & fi.Extension) End If 'IO.File.Copy(FilePath, SubFile & fi.Name & fi.Extension) Next Next End Sub I hope this helps. The reason I did a check on the file extension was that the program was putting the file extension twice on the files. Quote Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -- Rick Cook, The Wizardry Compiled
Wile Posted February 21, 2005 Posted February 21, 2005 Well, with the .net framework your live just became a LOT more easier. The System.IO.Directory class has a Move method that allows you to move a directory with its content: So System.IO.Directory.Move(sourcedir, targetdir) would basicly do the same as the code above, your preference for old-style and long, or short and sweet ;). Quote Nothing is as illusive as 'the last bug'.
stustarz Posted February 21, 2005 Posted February 21, 2005 With directory.move - this actually moves the directory, deleting the source, the question was how to copy the folder and its contents, which would require a longer code to loop through and copy the contents etc Quote Visit: VBSourceSeek - The VB.NET sourcecode library "A mere friend will agree with you, but a real friend will argue."
Leaders snarfblam Posted February 21, 2005 Leaders Posted February 21, 2005 I kinda wonder why they implemented a directory.move and not a directory.copy... Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.