﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2005 / SQL Server 2005 General Discussion  / How to delete the oldest file in a given path. Assume some .txt file of the format File_MMDDYYYY.txt / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Wed, 22 May 2013 14:03:35 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: How to delete the oldest file in a given path. Assume some .txt file of the format File_MMDDYYYY.txt</title><link>http://www.sqlservercentral.com/Forums/Topic1354271-149-1.aspx</link><description>hi Evil Kraig F,Thanks for the reply, as I'm new to ssis could you please explain in detail or if possible provide an example pkg for the requirement Thanks in advance</description><pubDate>Wed, 05 Sep 2012 15:27:45 GMT</pubDate><dc:creator>SS53217</dc:creator></item><item><title>RE: How to delete the oldest file in a given path. Assume some .txt file of the format File_MMDDYYYY.txt</title><link>http://www.sqlservercentral.com/Forums/Topic1354271-149-1.aspx</link><description>[quote][b]SS53217 (9/4/2012)[/b][hr][code="vb"]Enum ScriptResults		Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success		Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure	End Enum	    Public Sub Main()        Dim FILETOTEST As String = Dts.Variables("CurrentFileName").Value.ToString()        Dim FileDate As Date        If File.Exists(FILETOTEST) Then            FileDate = File.GetCreationTime(FILETOTEST)        End If        If FileDate &amp;lt; Dts.Variables("CurrentFileDateTime").Value.ToString Then            Dts.Variables("CurrentFileDateTime").Value = FileDate            Dts.Variables("OldestFileName").Value = Dts.Variables("CurrentFileName").Value.ToString()        End If        Dts.TaskResult = ScriptResults.Success[/code][/quote][quote]"Error	1 Validation error. File System Task File System Task: Variable "OldestFileName" is used as a source or destination and is empty.  	OLDEST_FILE_DELETE.dtsx	0	0"[/quote]This has to be a code snippet, it's not looping the directory, and you're comparing a datetime from the filesystem to a string... which is hazardous at best.Immediate answer: to deal with that error send the value in the DTSVariable into a local VB Variable and test it before usage.  It'll help you track down the break if nothing else, and DTSVariable calls don't behave as direct requests internal to all functions, I've found it better to transfer to local variables in almost all cases.Longer answer:  There's a LOT of wrapper to this that we're not seeing and we won't be able to troubleshoot incomplete code for you.  There's cut and paste code using filesystem in vb that will archive everything past a certain date, etc etc, that can be modified easier if you take the time to learn all of what they're doing.  Usually they're error trapped too to make sure accidents can't happen with live files.  I'd recommend a bit of googling and research on this.</description><pubDate>Tue, 04 Sep 2012 18:12:47 GMT</pubDate><dc:creator>Evil Kraig F</dc:creator></item><item><title>How to delete the oldest file in a given path. Assume some .txt file of the format File_MMDDYYYY.txt</title><link>http://www.sqlservercentral.com/Forums/Topic1354271-149-1.aspx</link><description>Can someone please help me....i have tried using for each loop, script task and file system taskthis is the script used  to get the oldest file from a given folderEnum ScriptResults		Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success		Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure	End Enum	    Public Sub Main()        Dim FILETOTEST As String = Dts.Variables("CurrentFileName").Value.ToString()        Dim FileDate As Date        If File.Exists(FILETOTEST) Then            FileDate = File.GetCreationTime(FILETOTEST)        End If        If FileDate &amp;lt; Dts.Variables("CurrentFileDateTime").Value.ToString Then            Dts.Variables("CurrentFileDateTime").Value = FileDate            Dts.Variables("OldestFileName").Value = Dts.Variables("CurrentFileName").Value.ToString()        End If        Dts.TaskResult = ScriptResults.Successwhen i assign OldestFileName to File system task as source variable the error it shows"Error	1 Validation error. File System Task File System Task: Variable "OldestFileName" is used as a source or destination and is empty.  	OLDEST_FILE_DELETE.dtsx	0	0"</description><pubDate>Tue, 04 Sep 2012 18:04:32 GMT</pubDate><dc:creator>SS53217</dc:creator></item></channel></rss>