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
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: Wednesday, September 30, 2015 3:57 PM
Points: 74, Visits: 432
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

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();


Dts.TaskResult = (int)ScriptResults.Success;

Post #1568598
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse