Click here to monitor SSC
SQLServerCentral is supported by Redgate
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
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, September 23, 2016 12:56 PM
Points: 80, Visits: 517
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