Click here to Skip to main content
16,018,904 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
I'm looking to migrate a set of files named in a single format to a location in another drive.

For eg., if I have a set of images, named in a format that contains name and date like:NAME_DDMMYY in a D:// and I want to migrate them to E://., how can I do that?

I would like to use C# with .net framework 4.0.
Posted
Comments
Herman<T>.Instance 24-Jul-13 6:44am    
what have you tried?
Herman<T>.Instance 24-Jul-13 6:46am    
Have you read this?
[no name] 24-Jul-13 6:47am    
Well you would write some code that creates a list of filenames in your format and then "migrate" them to your E drive. Perhaps something in the System.IO namespace could help you out with this....
Maciej Los 24-Jul-13 6:59am    
Sarin VT 24-Jul-13 7:10am    
The same question is asked many time in codeproject itself.

Belowing Code Will Help you............

VB
Private Sub btnBackup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBackup.Click
          Dim sourcepath As String = "C:\temp\test\1.txt"
    Dim DestPath As String = "D:\Data\"
    CopyDirectory(sourcepath, DestPath)

End Sub

Private Shared Sub CopyDirectory(sourcePath As String, destPath As String)
    If Not Directory.Exists(destPath) Then
        Directory.CreateDirectory(destPath)
    End If

    For Each file__1 As String In Directory.GetFiles(sourcePath)
        Dim dest As String = Path.Combine(destPath, Path.GetFileName(file__1))
        File.Copy(file__1, dest)
    Next

    For Each folder As String In Directory.GetDirectories(sourcePath)
        Dim dest As String = Path.Combine(destPath, Path.GetFileName(folder))
        CopyDirectory(folder, dest)
    Next
End Sub
 
Share this answer
 
C#
string[] files = Directory.GetFiles(@"Z:\", "*.bak");foreach (string file in files)
{
File.Copy(file, Path.Combine(@"E:\", Path.GetFileName(file)) , true);
}

Hope this helps you.
 
Share this answer
 
Try this code.

Please read the guidelines for asking a question.
Do a minimum search, before posting a question here.

C#
public class SimpleFileCopy
{
    static void Main()
    {
        string fileName = "test.txt";
        string sourcePath = @"C:\Users\Public\TestFolder";
        string targetPath =  @"C:\Users\Public\TestFolder\SubDir";

        string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
        string destFile = System.IO.Path.Combine(targetPath, fileName);

        if (!System.IO.Directory.Exists(targetPath))
        {
            System.IO.Directory.CreateDirectory(targetPath);
        }

        // To copy a file to another location and
        // overwrite the destination file if it already exists.
        System.IO.File.Copy(sourceFile, destFile, true);

        // To move a file or folder to a new location:
        System.IO.File.Move(sourceFile, destinationFile);

        if (System.IO.Directory.Exists(sourcePath))
        {
            string[] files = System.IO.Directory.GetFiles(sourcePath);

            foreach (string s in files)
            {
                fileName = System.IO.Path.GetFileName(s);
                destFile = System.IO.Path.Combine(targetPath, fileName);
                System.IO.File.Copy(s, destFile, true);
            }
        }
        else
        {
            Console.WriteLine("Source path does not exist!");
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900