locked
c# code to move files from source to destination for below requirement RRS feed

  • Question

  • I have following structure in source folder

    Source(folder)

    In that below are subfolder

    App(inside abc subfolder is there)

    Slv(inside abc subfolder is there)

    One(inside abc subfolder is there)

    bah(inside abc subfolder is there)

    I have following structure in destination folder

    destination(folder)

    In that below are subfolder

    App(inside abc subfolder is there)

    Slv(inside abc subfolder is there)

    One(inside abc subfolder is there)

    bah(inside abc subfolder is there)

    my task is I need to move xml file from source folder to destination folder i.e

    suppose one.xml is there in abc subfolder in one subfolder of source folder then the file(one.xml) only should move to

     abc subfolder in one subfolder of destination folder

    suppose if gcl\abc\gcl.xml file is there(here gcl is a new folder which contains abc subfolder which contains gcl.xml) in source folder which is not in destination folder. Then the same structure should be created in destination folder like gcl folder with abc subfolder inside and in it gcl.xml file should be there

    I need to do this using script task(c#code)


    tsrkreddy

    Wednesday, June 17, 2015 10:34 AM

Answers

  • hey actually i checed in internet and finally i have written this code which worked fine for me

    public void Main()
            {
                // TODO: Add your code here
                string sourcepath = @"D:\siva\source";
                //string[] files = System.IO.Directory.GetFiles(sourcepath);
                string[] files = Directory.GetFiles(sourcepath, "*.*",
                                             SearchOption.AllDirectories);
                try
                {
                    foreach (string s in files)
                    {
                        // Use static Path methods to extract only the file name from the path.
                        //string fileName = System.IO.Path.GetFileName(s);
                        //string destFile = System.IO.Path.Combine(sourcepath, fileName);
                        MessageBox.Show(s);
                        string s1 = s.Replace(@"D:\siva\source", @"D:\siva\destination");
                        string s2 = Path.GetDirectoryName(s1);
                  
                        MessageBox.Show(s1);
                        MessageBox.Show(s2);
                        if (!System.IO.Directory.Exists(s2))
                        {
                            System.IO.Directory.CreateDirectory(s2);
                        }
                        System.IO.File.Copy(s, s1, true);
                    }
                }

                catch (Exception e)
                {
                    //Console.WriteLine("The process failed: {0}", e.ToString());
                    MessageBox.Show("the process failed", e.ToString());
                }
            }


    tsrkreddy

    • Proposed as answer by Kristin Xie Wednesday, June 24, 2015 2:10 AM
    • Marked as answer by Kristin Xie Thursday, June 25, 2015 9:40 AM
    Monday, June 22, 2015 1:00 PM

All replies

  • Hi sivarama

    >>I need to do this using script task(c#code)

    Based on your description, script task seems also related SSIS.  

    In C#, we can use File.Move Method to move a specified file to a new location.Here is a sample from MSDN,

     public static void Main() 
        {
            string path = @"c:\temp\MyTest.txt";
            string path2 = @"c:\temp2\MyTest.txt";
            try 
            {
                if (!File.Exists(path)) 
                {
                    // This statement ensures that the file is created, 
                    // but the handle is not kept. 
                    using (FileStream fs = File.Create(path)) {}
                }
    
                // Ensure that the target does not exist. 
                if (File.Exists(path2))	
                File.Delete(path2);
    
                // Move the file.
                File.Move(path, path2);
                Console.WriteLine("{0} was moved to {1}.", path, path2);
    
                // See if the original exists now. 
                if (File.Exists(path)) 
                {
                    Console.WriteLine("The original file still exists, which is unexpected.");
                } 
                else 
                {
                    Console.WriteLine("The original file no longer exists, which is expected.");
                }			
    
            } 
            catch (Exception e) 
            {
                Console.WriteLine("The process failed: {0}", e.ToString());
            }
        }

    You also can check http://pranavkumarsql.blogspot.jp/2013/10/ssis-script-task-to-copy-files-from-one.html for more details. Even this thread is discuss about Copy files. Per my understanding, it is the same as move files.

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. 

    Microsoft does not control these sites and has not tested any software or information found on these sites;Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.

    There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Kristin Xie Thursday, June 18, 2015 3:37 AM
    Thursday, June 18, 2015 3:36 AM
  • thanks it helps upto some extent actually we are bi develoeprs and we need to use script task and for that we need to write c# code

    here we write for soruce folder exists or not.

    but here in our scinario we need to check destination subfolder is there or not and if not it need to create and move in it

    suppose

    source\skm\skm.xml this is file location.

    it need to move destination\skm\skm.xml

    here problem is skm folder is not there in destination folder how to create skm subfolder dynamically and move file in skm subfolder in destination folder.

    i tried in above code, but not working


    tsrkreddy

    Thursday, June 18, 2015 5:55 AM
  • Hi sivarama,

    C# is not script language, As part of the Microsoft  .NET Framework, the Common Language Runtime (CLR) is the programming that manages the execution of programs written in any language that uses the .NET Framework, for example C#, VB.Net, F# and so on.

    In C#, there are examples show how to copy, move, and delete files and folders in a synchronous manner by using the System.IO.File, System.IO.Directory, System.IO.FileInfo, and System.IO.DirectoryInfo classes from the System.IO namespace.

    Reference

    How to: Copy, Delete, and Move Files and Folders (C# Programming Guide)

    In addition, have you check SSIS: Script task to copy files from one location to another location as i posted before? This sample should suitable for you.

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, June 19, 2015 8:04 AM
  • hey actually i checed in internet and finally i have written this code which worked fine for me

    public void Main()
            {
                // TODO: Add your code here
                string sourcepath = @"D:\siva\source";
                //string[] files = System.IO.Directory.GetFiles(sourcepath);
                string[] files = Directory.GetFiles(sourcepath, "*.*",
                                             SearchOption.AllDirectories);
                try
                {
                    foreach (string s in files)
                    {
                        // Use static Path methods to extract only the file name from the path.
                        //string fileName = System.IO.Path.GetFileName(s);
                        //string destFile = System.IO.Path.Combine(sourcepath, fileName);
                        MessageBox.Show(s);
                        string s1 = s.Replace(@"D:\siva\source", @"D:\siva\destination");
                        string s2 = Path.GetDirectoryName(s1);
                  
                        MessageBox.Show(s1);
                        MessageBox.Show(s2);
                        if (!System.IO.Directory.Exists(s2))
                        {
                            System.IO.Directory.CreateDirectory(s2);
                        }
                        System.IO.File.Copy(s, s1, true);
                    }
                }

                catch (Exception e)
                {
                    //Console.WriteLine("The process failed: {0}", e.ToString());
                    MessageBox.Show("the process failed", e.ToString());
                }
            }


    tsrkreddy

    • Proposed as answer by Kristin Xie Wednesday, June 24, 2015 2:10 AM
    • Marked as answer by Kristin Xie Thursday, June 25, 2015 9:40 AM
    Monday, June 22, 2015 1:00 PM
  • hey actually i checed in internet and finally i have written this code which worked fine for me

    public void Main()
            {
                // TODO: Add your code here
                string sourcepath = @"D:\siva\source";
                //string[] files = System.IO.Directory.GetFiles(sourcepath);
                string[] files = Directory.GetFiles(sourcepath, "*.*",
                                             SearchOption.AllDirectories);
                try
                {
                    foreach (string s in files)
                    {
                        // Use static Path methods to extract only the file name from the path.
                        //string fileName = System.IO.Path.GetFileName(s);
                        //string destFile = System.IO.Path.Combine(sourcepath, fileName);
                        MessageBox.Show(s);
                        string s1 = s.Replace(@"D:\siva\source", @"D:\siva\destination");
                        string s2 = Path.GetDirectoryName(s1);
                  
                        MessageBox.Show(s1);
                        MessageBox.Show(s2);
                        if (!System.IO.Directory.Exists(s2))
                        {
                            System.IO.Directory.CreateDirectory(s2);
                        }
                        System.IO.File.Copy(s, s1, true);
                    }
                }

                catch (Exception e)
                {
                    //Console.WriteLine("The process failed: {0}", e.ToString());
                    MessageBox.Show("the process failed", e.ToString());
                }
            }


    tsrkreddy

    Hi sivarama,

    Glad to know you worked it out. Thanks for sharing your solution.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, June 24, 2015 2:15 AM
  • how to get list of files in folder(including subfolders) from ftp server.

    i want complete c# code to use in script task.

    basically i am msbi developer. can anybody give that code


    tsrkreddy

    Friday, June 26, 2015 6:07 AM
  • how to get list of files in folder(including subfolders) from ftp server.

    i want complete c# code to use in script task.

    basically i am msbi developer. can anybody give that code


    tsrkreddy

    Hi,

    This case was closed now and the orginal issue was resolved. I suggest that you reopen a new thread for the new issue. So it is easier for others to undertand the issue and you will get more effective response.

    Best  regards,



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Friday, June 26, 2015 6:31 AM