none
Move files from One location to another RRS feed

  • Question

  • I need to move file from Source Folder to Destination folder only which contains matching ID passed 

    public static void Move(string SourceFolderPath, string destinationPath, string Id)
    
            {
                string[] fileArray = Directory.GetFiles(SourceFolderPath);
    
                for (int i = 0; i < fileArray.Length; i++)
                {
                    if (fileArray[i].Contains(Id))
                    {
                        File.Move(Path.Combine(fileArray[i], destinationPath);                  
    
                    }
                }
                
            }

    RH


    • Edited by Dan2890 Tuesday, November 5, 2019 9:44 PM a
    Tuesday, November 5, 2019 9:43 PM

Answers

  • Here is code that works

    public void MoveFile(string sourceFolderPath, string destinationPath, string id)
    {
        if (!Directory.Exists(sourceFolderPath) || !Directory.Exists(destinationPath)) return;
    
        var fileArray = Directory.GetFiles(sourceFolderPath)
            .Where(file => Path.GetFileName(file).Contains(id))
            .Select(file => file);
    
        foreach (var fileName in fileArray)
        {
            File.Move(
                fileName, 
                Path.Combine(destinationPath, Path.GetFileName(fileName)));
        }
    }

    Demo

    private  void button1_Click(object sender, EventArgs e)
    {
        MoveFile(@"C:\Temp", @"C:\Temp\OtherFolder", "10");
    }

    The code in your question will not compile as File.Move requires two arguments, from and to.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, November 6, 2019 12:33 AM
    Moderator
  • Hi Dan2890, 

    Thank you for posting here.

    You can also use the following code to move all files which contains files included in subfolders from one location to another.

            public static void FindAndMoveAllFiles(string sourceFolderPath, string destinationPath, string id)
            {
                if (!Directory.Exists(sourceFolderPath) || !Directory.Exists(destinationPath)) return;
                DirectoryInfo Dir = new DirectoryInfo(sourceFolderPath);
                try
                {
                    foreach (FileInfo f in Dir.GetFiles()) 
                    {
                        if (f.Name.Contains(id))
                        {
                            File.Move(f.FullName, Path.Combine(destinationPath, f.Name));
                        }
                    }
                    var s = Dir.GetDirectories();
                    if (s.Length == 0)
                    {
                        return;
                    }
                    foreach (DirectoryInfo d in s)
                    {
                        string str = d.FullName + "\\";
                        FindAndMoveAllFiles(str, destinationPath, id);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }

    Main function:

            static void Main(string[] args)
            {
                FindAndMoveAllFiles(@"D:\TestFile",@"D:\testData", "1");
            }

    Source Folder:

    Result:

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 6, 2019 5:29 AM
    Moderator

All replies

  • So what is the issue ???

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, November 5, 2019 10:22 PM
    Moderator
  • The issue is my code is not correct. I receive error as Cannot find the file C:\temp

    SourceFolder : C:\temp

    DestinationFolder: C:\temp\LatestFileFolder

    There are 10 files in SourceFolder. When debugging above code, I get the error as :

    I receive error as Cannot find the file C:\temp

    How can I add correct logic to my code to move file Depending on ID from folder to another ?



    RH

    Tuesday, November 5, 2019 10:57 PM
  • Here is code that works

    public void MoveFile(string sourceFolderPath, string destinationPath, string id)
    {
        if (!Directory.Exists(sourceFolderPath) || !Directory.Exists(destinationPath)) return;
    
        var fileArray = Directory.GetFiles(sourceFolderPath)
            .Where(file => Path.GetFileName(file).Contains(id))
            .Select(file => file);
    
        foreach (var fileName in fileArray)
        {
            File.Move(
                fileName, 
                Path.Combine(destinationPath, Path.GetFileName(fileName)));
        }
    }

    Demo

    private  void button1_Click(object sender, EventArgs e)
    {
        MoveFile(@"C:\Temp", @"C:\Temp\OtherFolder", "10");
    }

    The code in your question will not compile as File.Move requires two arguments, from and to.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, November 6, 2019 12:33 AM
    Moderator
  • Hi Dan2890, 

    Thank you for posting here.

    You can also use the following code to move all files which contains files included in subfolders from one location to another.

            public static void FindAndMoveAllFiles(string sourceFolderPath, string destinationPath, string id)
            {
                if (!Directory.Exists(sourceFolderPath) || !Directory.Exists(destinationPath)) return;
                DirectoryInfo Dir = new DirectoryInfo(sourceFolderPath);
                try
                {
                    foreach (FileInfo f in Dir.GetFiles()) 
                    {
                        if (f.Name.Contains(id))
                        {
                            File.Move(f.FullName, Path.Combine(destinationPath, f.Name));
                        }
                    }
                    var s = Dir.GetDirectories();
                    if (s.Length == 0)
                    {
                        return;
                    }
                    foreach (DirectoryInfo d in s)
                    {
                        string str = d.FullName + "\\";
                        FindAndMoveAllFiles(str, destinationPath, id);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }

    Main function:

            static void Main(string[] args)
            {
                FindAndMoveAllFiles(@"D:\TestFile",@"D:\testData", "1");
            }

    Source Folder:

    Result:

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 6, 2019 5:29 AM
    Moderator