Reputation: 823
string[] s = Directory.GetFiles(t, "*.txt",SearchOption.AllDirectories);
for (int i = 0; i < s.Length; i++)
{
File.Copy(s[i],
}
File.Copy will copy the files to another file name. I want to keep the same files names just copy them from one directory to another directory.
Upvotes: 1
Views: 7059
Reputation: 7448
You can do it nicely like this:
Directory.GetFiles("c:\\temp", "*.txt", SearchOption.AllDirectories) // get the files
.Select(c => new FileInfo(c)) // project each filename into a fileinfo
.ToList() // convert to list
.ForEach(c => c.CopyTo("d:\\temp\\" + c.Name)); // foreach fileinfo, copy to the desired path + the actual file name
Upvotes: 3
Reputation: 4221
You could view this post, which should help;
Or this MSDN link:
Code snippet:
var sourceDir = @"c:\sourcedir";
var destDir = @"c:\targetdir";
var pattern = "*.txt";
foreach (var file in new DirectoryInfo(sourceDir).GetFiles(pattern))
{
file.CopyTo(Path.Combine(destDir, file.Name));
}
Hope this helps?
Upvotes: 1
Reputation: 3386
Use this:
File.Copy(s[i], "c:\\anotherFolder\\" + Path.GetFileName(s[i]));
Upvotes: 5
Reputation: 3109
You are doing it the right way..
See example from microsoft
http://msdn.microsoft.com/en-us/library/bb762914.aspx
another solution might be to call the xcopy command ...
Upvotes: 0