locked
Move the files from source to destination of specific period RRS feed

  • Question

  • Hi Everyone,

    I've few files which I want to move from one folder to another folder of specific period.(i.e. Around 1 month files are present in the source folder & want to move only last 15 days files in the destination folder)

    Please provide the solution 

    Cheers.................
    Thursday, April 12, 2012 2:44 PM

Answers

  • Something like this.  Add System.IO to your references and then create this function in the script task:  (untested but should be close - mostly sample code from the web)  You can get rid of a bunch of it if you don't want to look in subdirectories.

    const int HowDeepToScan = 4;
            static DateTime Checkdate = DateTime.Now;
    
            public static void ProcessDir(string sourceDir, string destDir, int recursionLvl)
            {
                if (recursionLvl <= HowDeepToScan)
                {
                    // Process the list of files found in the directory. 
                    string[] fileEntries = Directory.GetFiles(sourceDir);
                    foreach (string fileName in fileEntries)
                    {
                        if (File.GetLastWriteTime(fileName).AddDays(15) < Checkdate)
                        {
                            File.Move(fileName, destDir);
                        }
       
                    }
                    
    
                    // Recurse into subdirectories of this directory.
                    string[] subdirEntries = Directory.GetDirectories(sourceDir);
                    foreach (string subdir in subdirEntries)
                        // Do not iterate through reparse points
                        if ((File.GetAttributes(subdir) &
                             FileAttributes.ReparsePoint) !=
                                 FileAttributes.ReparsePoint)
    
                            ProcessDir(subdir, destDir, recursionLvl + 1);
                }
            }

    Then call the function from Main() in your script task, passing in the source and target directories (SSIS variables) and 0 for the recrsionLvl


    Chuck




    • Edited by Chuck Pedretti Thursday, April 12, 2012 3:07 PM
    • Proposed as answer by Raunak J Thursday, April 12, 2012 3:40 PM
    • Marked as answer by Eileen Zhao Thursday, April 19, 2012 7:41 AM
    Thursday, April 12, 2012 3:02 PM

All replies

  • How are you going to identify the files that need to be moved?   Name?  Last Modified Date? Created Date?

    Chuck

    Thursday, April 12, 2012 2:47 PM
  • Based on "Last modified Date"
    Thursday, April 12, 2012 2:48 PM
  • I would just use a script task in the COntrol flow and do the entire operation in c#

    Chuck

    Thursday, April 12, 2012 2:49 PM
  • Something like this.  Add System.IO to your references and then create this function in the script task:  (untested but should be close - mostly sample code from the web)  You can get rid of a bunch of it if you don't want to look in subdirectories.

    const int HowDeepToScan = 4;
            static DateTime Checkdate = DateTime.Now;
    
            public static void ProcessDir(string sourceDir, string destDir, int recursionLvl)
            {
                if (recursionLvl <= HowDeepToScan)
                {
                    // Process the list of files found in the directory. 
                    string[] fileEntries = Directory.GetFiles(sourceDir);
                    foreach (string fileName in fileEntries)
                    {
                        if (File.GetLastWriteTime(fileName).AddDays(15) < Checkdate)
                        {
                            File.Move(fileName, destDir);
                        }
       
                    }
                    
    
                    // Recurse into subdirectories of this directory.
                    string[] subdirEntries = Directory.GetDirectories(sourceDir);
                    foreach (string subdir in subdirEntries)
                        // Do not iterate through reparse points
                        if ((File.GetAttributes(subdir) &
                             FileAttributes.ReparsePoint) !=
                                 FileAttributes.ReparsePoint)
    
                            ProcessDir(subdir, destDir, recursionLvl + 1);
                }
            }

    Then call the function from Main() in your script task, passing in the source and target directories (SSIS variables) and 0 for the recrsionLvl


    Chuck




    • Edited by Chuck Pedretti Thursday, April 12, 2012 3:07 PM
    • Proposed as answer by Raunak J Thursday, April 12, 2012 3:40 PM
    • Marked as answer by Eileen Zhao Thursday, April 19, 2012 7:41 AM
    Thursday, April 12, 2012 3:02 PM