Need ActiveX Script To Rename A File

  • I know there is a way to have a VB Script (in an ActiveX Script Task) move and delete files on a hard drive. Is there a way to make it rename a file? If so how?

    I am trying to make a DTS package rename an existing backup file, then create a new one, and then delete the old one after verifying the new one has been created. I know this is similar to the backup part of the maintenance plan. The reason I want to do this is because I need the same file name to be there all the time (I don't want the time stamp tacked onto the name of the file). So if anyone knows a simpler way to do this that is fine too. I couldn't find how to make the backup portion of a maintenance plan use the same file name with no time stamp in the name.

    Robert W. Marda

    SQL Programmer

    bigdough.com

    The world’s leading capital markets contact database and software platform.

    Robert W. Marda
    Billing and OSS Specialist - SQL Programmer
    MCL Systems

  • exec xp_cmdshell 'rename "\\tstsql01\d$\temp\ip.doc" "ipnew.doc"'

  • That is the solution I have implemented. Thanks. From a curious stand point, I would still like to know if there is a rename command for VB Script to do this. Any one know?

    Robert W. Marda

    SQL Programmer

    bigdough.com

    The world’s leading capital markets contact database and software platform.

    Robert W. Marda
    Billing and OSS Specialist - SQL Programmer
    MCL Systems

  • http://www.sqldts.com/default.aspx?292

    Quoted from above article.

    "Move or Rename File

    This shows a simple file move operation, using hardcoded source and destination filenames. There is no explicit rename method in the scripting object, but a move is just the same.

    ' Move File

    Option Explicit

    Function Main()

    Dim oFSO

    Dim sSourceFile

    Dim sDestinationFile

    Set oFSO = CreateObject("Scripting.FileSystemObject")

    sSourceFile = "C:\SourceFile.txt"

    sDestinationFile = "C:\Folder\DestinationFile.txt"

    oFSO.MoveFile sSourceFile, sDestinationFile

    ' Clean Up

    Set oFSO = Nothing

    Main = DTSTaskExecResult_Success

    End Function"

  • Here is a variation of the above if you want to control the move more precisely by separating the copy and delete portions into separate ActiveX tasks.

     
    
    ActiveX Task 1:

    '**********************************************************************
    ' Visual Basic ActiveX Script
    '************************************************************************

    Function Main()

    Orig_FileNm = "C:\SourceFile.txt"

    Dest_FileNm = "C:\Folder\DestinationFile.txt"

    Dim fso

    Set fso = CreateObject("Scripting.FileSystemObject")

    IF (fso.FileExists(Orig_FileNm)) THEN
    fso.CopyFile Orig_FileNm, Dest_FileNm, true
    END IF

    Main = DTSTaskExecResult_Success

    End Function

    ActiveX Task 2:

    '**********************************************************************
    ' Visual Basic ActiveX Script
    '************************************************************************

    Function Main()

    Orig_FileNm = "C:\SourceFile.txt"

    Dim fso

    Set fso = CreateObject("Scripting.FileSystemObject")

    IF (fso.FileExists(Orig_FileNm)) THEN
    fso.DeleteFile(Orig_FileNm)
    END IF

    Main = DTSTaskExecResult_Success

    End Function

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

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