Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted
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;
	}
}
}

Posted

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.

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

Posted

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 ;).

Nothing is as illusive as 'the last bug'.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...