Get file create date and time from within SSIS
The subject line says it all. I need to be able to do a comparison on a file's date and time to see if it is stale or not.
October 4th, 2006 8:49pm

Use a Script task - and inside check for Last Update On attribute:Also - you can check out this article for accessing file attributes: http://msdn2.microsoft.com/en-us/library/system.io.file.getattributes.aspx
Free Windows Admin Tool Kit Click here and download it now
October 4th, 2006 9:02pm

Was hoping to avoid VB, but I guess I'll have to dust that skill off and see if I can get it to work.Thanks...
October 4th, 2006 9:23pm

Yes - have to script - File System Task doesnt seem to have capability to check for those attributes
Free Windows Admin Tool Kit Click here and download it now
October 4th, 2006 9:26pm

So far, it looks like you'll have to script in some way, shape, or form. Here's another option that I'll throw into the ring. Create the file, getfiledate.vbs, with the following contents: set fso = CreateObject("Scripting.FileSystemObject")set file = fso.GetFile("your-file-here")WScript.echo file.DateCreated Then, createa batch file, rungetfiledate.bat, with the following contents: @echo offcscript //nologo getfiledate.vbs Now, throw an "Execute Process Task" into your workflow and call the batch file above. Create a variable to hold the output of the batch file in the execute process task and then you're free to work with that variable however you like. Perhaps write it to the database for later comparison. From there, it's rather trivial.
October 4th, 2006 9:54pm

This did the trick... thanks for the idea.... Dim path As String = "G:\Data\encrypted\XYZ.DAT" If File.Exists(path) = False Then Dts.TaskResult = Dts.Results.Failure Return End If Dim current As Date = DateTime.Now Dim fileCreateDate As Date = File.GetCreationTime(path) If current.ToShortDateString <> fileCreateDate.ToShortDateString Then Dts.TaskResult = Dts.Results.Failure Else Dts.TaskResult = Dts.Results.Success End If
Free Windows Admin Tool Kit Click here and download it now
October 4th, 2006 10:12pm

hi , Could u pls explain how did u handle the the dts.results.failure case. in general if control flows to failure part, this is considered as an error in Ssis. how to handle this Bhararth
October 17th, 2006 4:31pm

I had exactly this same problem. Setting Dts.ExecutionValue to False always results in the package "failing", when all you want to do is end execution if the file isn't newer. The trick is to set a boolean variable ("Go_NoGo" in the following code) to False if thetimestamp isn't newer, and set Dts.Execution to True. That way, when you exit the procedure, you don't get an error, but you've got a variable indicating not to proceed. Then, check the variable on the constraint between the Script Task and the next task in the Control Flow. Here's my VB code: If (CurrentDateTimeStamp > PreviousDateTimeStamp) Then WriteVariable("Go_NoGo", True) 'Execution will fall through to the next step in this process...Else WriteVariable("Go_NoGo", False) Dts.TaskResult = Dts.Results.Success Exit SubEnd If I'm using Daniel Read's WriteVariable procedure, but you could just reference Go_NoGo as a ReadWriteVariable to the Script Task: Private Sub WriteVariable(ByVal varName As String, ByVal varValue As Object) Try Dim vars As Variables Dts.VariableDispenser.LockForWrite(varName) Dts.VariableDispenser.GetVariables(vars)Try vars(varName).Value = varValue Catch ex As Exception Throw ex Finally vars.Unlock() End Try Catch ex As Exception Throw ex End TryEnd Sub 'WriteVariable Here's the expression I'm using on the constraint between the Script Task and the next task to test the value of Go_NoGo: (@[User::Go_NoGo]==True) I set the Constraint's "Evaluation operation" to "Expression", and the Go_NoGo allowsexecution to proceed only when Go_NoGo is True. Hope this helps! - Mike
Free Windows Admin Tool Kit Click here and download it now
October 17th, 2006 6:48pm

I need to capture the "file creattoin date" in a variable and pass it to the table along with data as a column.I used a "script component" for transformation in my data flow and connected input file and output table to it. I played using "file timestamp" to get file date but that's not meaningful.Then, here is the script I wrote and hoping to get it run but running into an error after another... it's failing with "dts is not declared" error. I changed to "me.variables" instead but it errored out as "variables can't be indexed because no default property assigned".1. I have 2 variables in the package - FileDate (date) and FlatFile (string) - both have been assigned some default value.2. here is the code - Imports System Imports System.DataImports System.MathImports Microsoft.SqlServer.Dts.Runtime Public Class ScriptMainPublic Sub MAIN() Dim File As System.IO.File Dim FileToTest As String = DTS.Variables("FlatFile").Value.ToString() Dim FileDate As Date If File.Exists(FileToTest) Then FileDate = File.GetCreationTime(FileToTest) End If End Sub End Class3. readonlyvariable is set to FlatFile and readwrite variable is set to FileDate in script component.what do I do to get it run?Thanks
February 6th, 2009 12:20am

You may want to try theFile Properties Task from PragmaticWorks.Your code should be able to reference your variables by using theDts.Variables.FileDate and Dts.Variables.FlatFile properties... (<-- this is correct for the Script Task, but not the Script Component as the OP was talking about...)
Free Windows Admin Tool Kit Click here and download it now
February 6th, 2009 2:17am

To reference a variable in a script component (if that's what you really want to do), you'd use Me.Variables.NameOfVariable.John Welch | www.mariner-usa.com | www.agilebi.com
February 6th, 2009 3:37am

I've also just made a File Properties Task (open source) that lets you read the file creation date.
Free Windows Admin Tool Kit Click here and download it now
August 6th, 2009 6:15pm

The subject line says it all. I need to be able to do a comparison on a file's date and time to see if it is stale or not. Here some more C# examples to solve this with a Script Task: http://microsoft-ssis.blogspot.com/2011/01/use-filedates-in-ssis.html Loop through a folder and get the newest file Loop through a folder and remove files older than 5 days
January 15th, 2011 2:31am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics