locked
Move Files to Archive Folder RRS feed

  • Question

  • Hi everyone,

    I have an ssis package that loads data to different csv files. The package dynamically create new files for each of the destination csv everyday. This works fine but I need a task that moves old files from the current folder to an archive folder as the job runs.

    For instance, if the current folder looks like this, I want the cleanup task to move files that are older than the current date to the archive folder.

    Current Folder (before implementing the cleanup task)

    • FileA_20181111.csv
    • FileA_20181112.csv
    • FileA_20181113.csv
    • FileA_20181114.csv   (today)
    • FileB_20181111.csv
    • FileB_20181112.csv
    • FileB_20181113.csv
    • FileB_20181114.csv   (today)
    • FileC_20181111.csv
    • FileC_20181112.csv
    • FileC_20181113.csv
    • FileC_20181114.csv   (today)

    Current Folder (after implementing the cleanup task)

    • FileA_20181114.csv   (today)
    • FileB_20181114.csv   (today)
    • FileC_20181114.csv   (today)


    Archive Folder (after implementing the cleanup task)

    • FileA_20181111.csv
    • FileA_20181112.csv
    • FileA_20181113.csv
    • FileB_20181111.csv
    • FileB_20181112.csv
    • FileB_20181113.csv
    • FileC_20181111.csv
    • FileC_20181112.csv
    • FileC_20181113.csv



    me

    Wednesday, November 14, 2018 10:56 PM

Answers

  • Hi lilutchay,

    No problem.

    Here is an updated version that handles file names inconsistencies.

    DirectoryInfo d = new DirectoryInfo(@"E:\Temp\Source");
    string destinationDir = @"E:\Temp\Destination";
    
    // search pattern based on the file name
    var files = d.GetFiles("*_2018*.csv");
    
    foreach (var file in files)
    {
    	string FileNameWithoutExtension = Path.GetFileNameWithoutExtension(file.Name);
    	string fileDate = FileNameWithoutExtension.Substring(FileNameWithoutExtension.Length - 8);
    	if (DateTime.ParseExact(fileDate, "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture) < DateTime.Today)
    	{
    		// Move the file
    		File.Move(file.FullName, Path.Combine(destinationDir, file.Name));
    	}
    }



    Thursday, November 15, 2018 11:51 PM

All replies

  • Hi lilutchay,

    You can use the SSIS Script Task to do that.

    Here is the c# implementation:

    DirectoryInfo d = new DirectoryInfo(@"E:\Temp\Source");
    string destinationDir = @"E:\Temp\Destination";
    
    // search pattern based on the file name
    var files = d.GetFiles("*_2018*.csv");
    
    foreach (var file in files)
    {
    	string[] token = file.Name.Split(new Char[] { '_', '.' });
    	if(DateTime.ParseExact(token[1], "yyyyMMdd",System.Globalization.CultureInfo.InvariantCulture) < DateTime.Today)
    	{
    		// Move the file
    		File.Move(file.FullName, Path.Combine(destinationDir, file.Name));
    	}
    }



    • Edited by Yitzhak Khabinsky Thursday, November 15, 2018 12:20 AM
    • Proposed as answer by Yang.Z Thursday, November 15, 2018 1:45 AM
    Thursday, November 15, 2018 12:07 AM
  • Thanks Yitzhak, the code works perfectly fine.

    But I ran into another issue as the index length is not the same for all files

    for instance: if the naming convention of the file is as below, the token[1] returns the date value.

    • FileA_20181111.csv
    • FileA_20181112.csv
    • FileA_20181113.csv
    • FileB_20181111.csv
    • FileB_20181112.csv
    • FileB_20181113.csv
    • FileC_20181111.csv
    • FileC_20181112.csv
    • FileC_20181113.csv

        If there are some inconsistency in the naming convection that will fail because token[1] index will return date value for FileB and FileB while the date value for FileA will be on token[2] : see below as example

        • FileA_B_20181111.csv
        • FileA_B_20181112.csv
        • FileA_B_20181113.csv
        • FileB_20181111.csv
        • FileB_20181112.csv
        • FileB_20181113.csv
        • FileC_20181111.csv
        • FileC_20181112.csv
    • FileC_20181113.csv


    me

    Thursday, November 15, 2018 11:19 PM
  • Hi lilutchay,

    No problem.

    Here is an updated version that handles file names inconsistencies.

    DirectoryInfo d = new DirectoryInfo(@"E:\Temp\Source");
    string destinationDir = @"E:\Temp\Destination";
    
    // search pattern based on the file name
    var files = d.GetFiles("*_2018*.csv");
    
    foreach (var file in files)
    {
    	string FileNameWithoutExtension = Path.GetFileNameWithoutExtension(file.Name);
    	string fileDate = FileNameWithoutExtension.Substring(FileNameWithoutExtension.Length - 8);
    	if (DateTime.ParseExact(fileDate, "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture) < DateTime.Today)
    	{
    		// Move the file
    		File.Move(file.FullName, Path.Combine(destinationDir, file.Name));
    	}
    }



    Thursday, November 15, 2018 11:51 PM
  • Works perfectly. Thanks!

    me

    Friday, November 16, 2018 3:07 PM