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