Copy the whlole folder recrusive

mcerk

Well-known member
Joined
Nov 1, 2004
Messages
78
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
 
[VB]
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;
}
}
}
[/VB]
 
This is what I used (Visual Basic)

[VB]

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

[/VB]

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.
 
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
Code:
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 ;).
 
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
 
I kinda wonder why they implemented a directory.move and not a directory.copy...
 

Similar threads

A
Replies
0
Views
78
Ashkan Satarpour
A
J
Replies
0
Views
59
Jalil Sear [MCPD SharePoint]
J
Back
Top