Looking for and renaming a file

  • Posted - 02/27/2008 : 09:09:29

    --------------------------------------------------------------------------------

    I have had a go at using a package with a script to rename and move a file and it works well by using a script task in a package with source and destination variables. See script at bottom

    But in my scenario the file that comes in every day will have a slightly different name. It will be called "System_UT_INCR_BOOKINGHEADER_20080228000000.TXT"

    On the 28th Feb. The date part of the title changes everyday. It will always have the string BOOKINGHEADER

    SO i think i need to adjust my "Source" variable which is currently just a string with a value of "C:\Data\files\imports\System_UT_INCR_BOOKINGHEADER_20080228000000.TXT" So it will currently only ever look for that exact file name.

    I want to be able to search for the string BOOKINGHEADER

    I suspect that its quite easy but I am not a programmer, so for me this kind of stuff is difficult

    +++++++++++++++++++++++++++++++++

    Imports System.IO

    Imports System

    Imports System.Data

    Imports System.Math

    Imports Microsoft.SqlServer.Dts.Runtime

    Public Class ScriptMain

    ' The execution engine calls this method when the task executes.

    ' To access the object model, use the Dts object. Connections, variables, events,

    ' and logging features are available as static members of the Dts class.

    ' Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.

    '

    ' To open Code and Text Editor Help, press F1.

    ' To open Object Browser, press Ctrl+Alt+J.

    Public Sub Main()

    Try

    File.Move(Dts.Variables("Source").Value.ToString, Dts.Variables("Destination").Value.ToString)

    Dts.Events.FireInformation(0, "", "File Moved Succesfully", "", 0, True)

    Catch ex As Exception

    Dts.Events.FireError(1, "", "Source file or destinations does not exist", "", 0)

    End Try

    Dts.TaskResult = Dts.Results.Success

    End Sub

    End Class

  • I'd look at the File System Task. It can move a file easily and doesn't require all that code you have in the Script Task. We use the File System Task to create a daily archive directory and then move the daily export files to that directory after they have been FTP'ed to our MSP. Works well.

    😎

  • Thanks for the tip, I'll give it a go.

    Any tips on looking for a string in a filename?

  • Actually, once you read up on the File System Task, I think you'll find it very easy to use.

    😎

  • Just thought of this, you may want to put the File System Task in a ForEach container using the file enumerator. That is how ours works. We have multiple files, and this allows us to process all the files in the export directory.

    😎

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply