How to rename file and append timestamp using script task

Who is online?  0 guests and 0 members
Home  »  Articles  »  How to rename file and append timestamp using script task

How to rename file and append timestamp using script task

change text size: A A A
Published: 8/31/2011 by  NayanPatel  - Views:  [1990]  

In this article you will learn how to rename file and add time stamp to make file name unique.

1. Create a new SSIS package

2. Add new variable and name it strSourceFilePath. Set value of variable with some valid pale path which you want to rename.

3. Drag new script task from toolbox. Set ReadOnlyVariables property to strSourceFilePath. Select Script Language "Visual Basic.net"

4. Click "Design Script" to edit script and enter the following VB.net script. Once editing is done close script task editor and save and then execute package to test file rename.

	Public Sub Main()
		'
		' Add your code here
        '
        If System.IO.File.Exists(Dts.Variables("strSourceFilePath").Value.ToString()) Then
            '//Rename file
            Dim oldPath As String = Dts.Variables("strSourceFilePath").Value.ToString()

            '//http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

            'yyyymmdd => 20110831
            'yyyyMMMdd => 2011Aug31
            'yyyyMMMdd.hh.nn.ss.fff => 2011Aug31.23.30.59.930 (hour + minute + second + Milisecond)

            Dim newPath As String = DateTime.Now.ToString("yyyymmdd") & "_" & IO.Path.GetFileName(oldPath)  '// C:\Files\20110831_Myfile.txt

            '// C:\Files\Myfile_20110831.txt
            'Dim newPath As String = IO.Path.GetDirectoryName(oldPath) & "\" & IO.Path.GetFileNameWithoutExtension(oldPath) & "_" & DateTime.Now.ToString("yyyymmdd") & IO.Path.GetExtension(oldPath) 

            My.Computer.FileSystem.RenameFile(oldPath, newPath)
        End If

		Dts.TaskResult = Dts.Results.Success
	End Sub


 For more information about date formats please visit http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

 

 
3.88
/5
Avg: 3.88/5: (1 votes)

Comments (1)

daniness
daniness said:
Hello All, Any idea of you'd rename 2 different files with a date stamp in this scenario? Here is what I'm unsuccessfully trying: Public Sub Main() If System.IO.File.Exists (Dts.Variables "SourceFilePath").Value.ToString()) Then Dts.Variables("User::FileSuffix").Value = (Date.Today.Month.ToString) & (Date.Today.Day.ToString) Dts.Variables("User::FileName1").Name = "OutcomesMembers" & Dts.Variables("User::FileSuffix").Value & ".txt" Dts.Variables("User::FileName2").Name = "OutcomesClaims" & Dts.Variables("User::FileSuffix").Value & ".txt" End If
11/21/2012
 · 
 
by

Most Recent Articles