Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Get ALL files from folder based on lastDateModifed Expand / Collapse
Author
Message
Posted Wednesday, May 7, 2014 10:35 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, July 28, 2014 4:37 PM
Points: 70, Visits: 355
I need to get ALL the files from a folder based on the DateModifed and load into a SQL table.

I have the scripttask code to get the Most Recent Last Modifed file but I need it based on the Date for all the files.

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;



namespace ST_2650e9fc7f2347b2826459c2dce1b5be.csproj
{
[System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{

#region VSTA generated code
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion

public void Main()
{

// TODO: Add your code here
var directory = new DirectoryInfo(Dts.Variables["User::VarFolderPath"].Value.ToString());

FileInfo[] files = directory.GetFiles();
DateTime LastModified = DateTime.MinValue;


foreach (FileInfo file in files)
{
if (file.LastWriteTime.Date > LastModified.Date)
{
LastModified = file.LastWriteTime;
Dts.Variables["User::VarFileName"].Value = file.ToString();
}
}

MessageBox.Show(Dts.Variables["User::VarFileName"].Value.ToString());

Dts.TaskResult = (int)ScriptResults.Success;
}
}
}

Post #1568598
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse