﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Discuss Content Posted by Alex Kersha / Article Discussions / Article Discussions by Author  / DTS and Global Variables / 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>Sun, 19 May 2013 22:58:03 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: DTS and Global Variables</title><link>http://www.sqlservercentral.com/Forums/Topic254822-267-1.aspx</link><description>great detail, that was big help!</description><pubDate>Thu, 08 Feb 2007 07:17:00 GMT</pubDate><dc:creator>Henry_Lee</dc:creator></item><item><title>RE: DTS and Global Variables</title><link>http://www.sqlservercentral.com/Forums/Topic254822-267-1.aspx</link><description>Hi,I also get the Object Required error at line 64 Error:  -2147220482 (800403FE); Provider Error:  0 (0)   Error string:  Error Code: 0Error Source= Microsoft VBScript runtime errorError Description: Object required: 'C:\temp\RubbishFile_'This is not the full filename, it appears to be truncated.Update: Pasted in the 'live' version of Function Main() and it worked !David</description><pubDate>Sun, 26 Feb 2006 06:21:00 GMT</pubDate><dc:creator>David le Quesne</dc:creator></item><item><title>RE: DTS and Global Variables</title><link>http://www.sqlservercentral.com/Forums/Topic254822-267-1.aspx</link><description>&lt;P&gt;Here is why there were (4) files kept when I only wanted to keep (2) as specified using param /AgvNumber:3=2 in the Command:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;For m = 4 To UBOUND(arrFileList)&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;When I change this line of code to use the input variable of the number of files to keep, it works as expected.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;For m = varNumber To UBOUND(arrFileList)&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;This is a really nice tool and an easy implementation.&lt;/P&gt;&lt;P&gt;Thanks again&lt;/P&gt;</description><pubDate>Thu, 23 Feb 2006 06:44:00 GMT</pubDate><dc:creator>Steve DiDomenico-197994</dc:creator></item><item><title>RE: DTS and Global Variables</title><link>http://www.sqlservercentral.com/Forums/Topic254822-267-1.aspx</link><description>&lt;P&gt;My last test was interesting. Earlier I was testing with (4) files I created using Notepad. I was unable to get these files to delete. Then I created (3) files by backing up the NorthWind Database. So here are the files I had:&lt;/P&gt;&lt;P&gt;TestFile1.bakTestFile2.bakTestFile3.bakTestFile4.bakNorthWind1.bakNorthWind2.bakNorthWind3.bak&lt;/P&gt;&lt;P&gt;Then I ran the job again and some files where deleted. Here are the file that were not deleted:&lt;/P&gt;&lt;P&gt;TestFile1.bakNorthWind1.bakNorthWind2.bakNorthWind3.bak&lt;/P&gt;&lt;P&gt;Here is my log file showing a Mask = .bak and Files to keep = 2:&lt;/P&gt;&lt;P&gt;Logging File Purge Activity: 2/22/2006 1:42:55 PM----------------------------------------Path = D:\Production\PROD\Developement\DeleteTest\Mask = .bakFiles to keep = 2 TestFile2.bak 2/22/2006 9:12:00 AM DELETED TestFile3.bak 2/22/2006 9:12:00 AM DELETED TestFile4.bak 2/22/2006 9:12:00 AM DELETED&lt;/P&gt;</description><pubDate>Wed, 22 Feb 2006 12:05:00 GMT</pubDate><dc:creator>Steve DiDomenico-197994</dc:creator></item><item><title>RE: DTS and Global Variables</title><link>http://www.sqlservercentral.com/Forums/Topic254822-267-1.aspx</link><description>Guys, sorry, Im just back from a holiday. Absolutely NOT, those lines need to be UNCOMMENTED in order for the actual deletion operation to work properly. I commented these as a safety net so I wouldn't be deleting files from an environment that was "off-limits".Great catch, I should've mentioned it in the article.I've also verified the script against that which is running on our production box and indeed the files are being deleted and no errors returned.I've included the Function Main() below:Function Main()	Dim objFSO	Dim objFolder	Dim objFileCollection		Dim strSourceFile	Dim varToday	Dim intFileCount	Dim intX	varNumber = DTSGlobalVariables("gvNumber").Value	varFilemask = DTSGlobalVariables("gvFileMask").Value	varFolderPath = DTSGlobalVariables("gvFolderPath").Value		varToday = Date	'Write variables out to debug log	Call DebugLog		Set objFSO = CreateObject("Scripting.FileSystemObject")	Set objFolder = objFSO.GetFolder(varFolderPath)	Set objFileCollection = objFolder.Files	intFileCount = objFileCollection.Count		ReDim arrFileList(intFileCount)	intX = 0	'Fill the array with the files	For Each objFile in objFileCollection		If inStr(1, objFile.name, varFileMask, 1) &gt; 0 Then			Set arrFileList(intX) = objFile			intX = intX + 1		End If	Next	ReDim Preserve arrFileList(intX-1) 	intFileCount = UBOUND(arrFileList)	'If there are more than X files in the folder	If intFileCount &gt; varNumber Then		'Sort the array using Bubble Sort		Dim startingKey		Dim mostRecent		Dim swapPos		Dim ValueToCompare		'Loop thru array and move most recent files to top		For i = 0 To UBOUND(arrFileList) - 1			'use first element as start and most recent  value 			set startingKey = arrFileList(i)			set mostRecent = arrFileList(i)			swap_Pos = i			for k = i + 1 To UBOUND(arrFileList) 				'if most Recent date is earlier the next in array, swap them				if mostRecent.DateLastModified &lt; arrFileList(k).DateLastModified then				                swap_pos = k            					Set mostRecent = arrFileList(k)            	            				End If			next			If swap_Pos &lt;&gt; i Then			            	Set arrFileList (swap_pos) = startingKey            				Set arrFileList(i) = mostRecent        			End If        			        Set startingKey = Nothing			        Set mostRecent = Nothing    		Next		'Delete the files from the folder		For m = 4 To UBOUND(arrFileList)			Call LogFileName(arrFileList(m).name &amp; " " &amp; cStr(arrFileList(m).DateLastModified))			objFSO.DeleteFile arrFileList(m)		Next 	End If 'more than X files in folder	' Clean Up	Set objFile = Nothing	Set objFSO = Nothing	Set objFolder = Nothing	Set objFileCollection = Nothing	'Set arrFileList = Nothing		Main = DTSTaskExecResult_SuccessEnd FunctionHere is the calling SQL step syntax:DTSRUN /SmyServer /N"Purge Files by Number - DTS" /AgvFileMask:8=".bak" /AgvFolderPath:8="C:\myPath\ImportBackup\" /AgvLogPath:8="C:\myPath\ImportBackup\DTSPurgeLog_Number.log" /AgvNumber:3=4 /EHere's an extract from the log file:Logging File Purge Activity: 2/15/2006 10:13:04 AM----------------------------------------Path = C:\Customers\AmericanAirlines\ImportBackup\Mask = .bakFiles to keep = 4 AA_SMPv212_20060209.bak 2/9/2006 5:02:26 AM DELETED AA_SMPv212_20060208.bak 2/8/2006 5:02:28 AM DELETED AA_SMPv212_20060207.bak 2/7/2006 5:02:23 AM DELETED AA_SMPv212_20060206.bak 2/6/2006 5:02:26 AM DELETED</description><pubDate>Wed, 22 Feb 2006 09:15:00 GMT</pubDate><dc:creator>Alex-217289</dc:creator></item><item><title>RE: DTS and Global Variables</title><link>http://www.sqlservercentral.com/Forums/Topic254822-267-1.aspx</link><description>&lt;P&gt;Great article.  &lt;/P&gt;&lt;P&gt;I was able to get everything to work except the actual deleting of files. Also, the log file is created but it does not show a list of deleted files, so this is consistant.&lt;/P&gt;&lt;P&gt;Are these line supposed to be commented in:&lt;/P&gt;&lt;P&gt;'Delete the files from the folder and log their names  For m = 4 To UBOUND(arrFileList)   'Call LogFileName(arrFileList(m).name &amp;amp; " " &amp;amp; cStr(arrFileList(m).DateLastModified))    'objFSO.DeleteFile arrFileList(m)  Next&lt;/P&gt;&lt;P&gt;As a test,I commented them back in, but the delete did not work. &lt;/P&gt;&lt;P&gt;When I try the suggestion from Grant Sutcliffe, I get an error on line # 64. &lt;/P&gt;&lt;P&gt;Here is the command I'm using and there are (4) .bak files in it:&lt;/P&gt;&lt;P&gt;DTSRUN /SUCEASQL /N"Purge Files by Number - DTS" /AgvFileMask:8=".bak" /AgvFolderPath:8="D:\Production\PROD\Developement\DeleteTest" /AgvLogPath:8="D:\Production\PROD\Developement\DeleteTest\Temp\DTSPurgeLog_Number.log" /AgvNumber:3=1 /E&lt;/P&gt;</description><pubDate>Wed, 22 Feb 2006 08:58:00 GMT</pubDate><dc:creator>Steve DiDomenico-197994</dc:creator></item><item><title>RE: DTS and Global Variables</title><link>http://www.sqlservercentral.com/Forums/Topic254822-267-1.aspx</link><description>&lt;P&gt;Perfect Article for a person (like me) starting to dip into DTS. Thanks. &lt;/P&gt;&lt;P&gt;Needed 1 change to be able to delete the files:&lt;/P&gt;&lt;P&gt;Change line: arrFileList(intX) = objFile = intX + 1 &lt;/P&gt;&lt;P&gt;to&lt;/P&gt;&lt;P&gt;arrFileList(intX) = objFile &lt;/P&gt;&lt;P&gt;intX = intX + 1&lt;/P&gt;&lt;P&gt;Code worked when I changed it to VB6 module but there was an error on line 63: set startingKey = arrFileList(i)&lt;/P&gt;&lt;P&gt;Error message for the job: Object required: '[string: "C:\Books\asp.net.pdf"]'&lt;/P&gt;&lt;P&gt;No Problem in VB is used the File Object here. It might be just me??&lt;/P&gt;&lt;P&gt;Thanks again - I learned a lot&lt;/P&gt;</description><pubDate>Tue, 21 Feb 2006 09:22:00 GMT</pubDate><dc:creator>Grant Sutcliffe</dc:creator></item><item><title>RE: DTS and Global Variables</title><link>http://www.sqlservercentral.com/Forums/Topic254822-267-1.aspx</link><description>Great article.</description><pubDate>Mon, 20 Feb 2006 07:07:00 GMT</pubDate><dc:creator>Andy Brons</dc:creator></item><item><title>DTS and Global Variables</title><link>http://www.sqlservercentral.com/Forums/Topic254822-267-1.aspx</link><description>Comments posted to this topic are about the content posted at &lt;A HREF="http://www.sqlservercentral.com/columnists/aKersha/dtsandglobalvariables.asp"&gt;http://www.sqlservercentral.com/columnists/aKersha/dtsandglobalvariables.asp&lt;/A&gt;</description><pubDate>Tue, 31 Jan 2006 18:39:00 GMT</pubDate><dc:creator>Alex-217289</dc:creator></item></channel></rss>