Renaming a file with Cmdshell

  • Hi all,

    I've got some code that needs to be recrafted so that anything named aspstate_log*.ldf will be renamed with the current date/time added to the end of the name.  I'm just not sure how the logic works, here it is in it's current form.

    declare @s-2 varchar(8000)

    select @s-2 = 'rename "E:\LBBSVS01\LOGS\MSSQL$LBBSQL01\Logs\aspstate_log.LDF" "E:\LBBSVS01\LOGS\MSSQL$LBBSQL01\Logs\aspstate_log(' + convert(varchar, getdate(), 112) + ').LDF"'

    EXEC xp_cmdshell @s-2

     

    Thanks,

     

    JB

  • Your script should work without any problem but....

    You are trying to rename .ldf file which is data base log file... if I am not mistaken...

    You can rename the file while db is active... so you have to kill all the users from the db and detach it using sp_detachdb procedure then rename it...

    I don't understand why you want to rename .ldf file...

    MohammedU
    Microsoft SQL Server MVP

Viewing 2 posts - 1 through 1 (of 1 total)

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