File rename saving as @MyFileNametxt

  • I am using the following to rename a fil in a location. It works but renames it as @MyFileNametxt'

    Any ideas what I have done wrong???

    DECLARE @MyFileNametxt varchar(50)

    set @MyFileNametxt = 'PASS_ASR_ALL_' + CAST(DATEPART(YYYY, GETDATE()) AS varCHAR(4)) +'_'+ CAST(DATEPART(MM, GETDATE()) AS varCHAR(2)) +'_'+ CAST(DATEPART(DD, GETDATE()) AS varCHAR(2))+'_'+CAST(DATEPART(HOUR, GETDATE()) AS varCHAR(2)) +'_'+ CAST(DATEPART(MINUTE, GETDATE()) AS varCHAR(2))

    +'_'+ CAST(DATEPART(SECOND, GETDATE()) AS varCHAR(2))+'.txt'

    EXEC master..xp_cmdshell 'RENAME "D:\WebFiles\Web Update File Out\*** Update Files\RASS_ASR_ALL.txt" @MyFileNametxt'

  • try it this way:

    declare @execmd varchar(4000)

    set @execmd = 'execute master..xp_cmdshell ''RENAME "D:\WebFiles\Web Update File Out\*** Update Files\RASS_ASR_ALL.txt" "' + @MyFileNametxt +'"'

    exec(@execmd)

    The probability of survival is inversely proportional to the angle of arrival.

  • your snippet has the string hardcoded, isntead of substituting the variable.

    something like this is what you are after, i think.

    DECLARE @MyFileNametxt varchar(50)

    DECLARE @cmd as varchar(1000)

    set @MyFileNametxt = 'PASS_ASR_ALL_' + CAST(DATEPART(YYYY, GETDATE()) AS varCHAR(4)) +'_'+ CAST(DATEPART(MM, GETDATE()) AS varCHAR(2)) +'_'+ CAST(DATEPART(DD, GETDATE()) AS varCHAR(2))+'_'+CAST(DATEPART(HOUR, GETDATE()) AS varCHAR(2)) +'_'+ CAST(DATEPART(MINUTE, GETDATE()) AS varCHAR(2))

    +'_'+ CAST(DATEPART(SECOND, GETDATE()) AS varCHAR(2))+'.txt'

    SET @cmd = 'RENAME "D:\WebFiles\Web Update File Out\*** Update Files\RASS_ASR_ALL.txt" ' + @MyFileNametxt

    EXEC master..xp_cmdshell @cmd

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Thanks that works perfectly. Thanks for the quick response

  • kyle.doouss (11/30/2011)


    Thanks that works perfectly. Thanks for the quick response

    Wouldn't you like to know the reason of failure of your code? 😛

  • Hello again. What about this one. Can I still apply the same principle as before? Could you tweak it for me?

    DECLARE @MyFileName varchar(50)

    set @MyFileName = 'PASS_ASR_ALL_' + CAST(DATEPART(YYYY, GETDATE()) AS varCHAR(4)) +'_'+ CAST(DATEPART(MM, GETDATE()) AS varCHAR(2)) +'_'+ CAST(DATEPART(DD, GETDATE()) AS varCHAR(2))+'_'+CAST(DATEPART(HOUR, GETDATE()) AS varCHAR(2)) +'_'+ CAST(DATEPART(MINUTE, GETDATE()) AS varCHAR(2))

    +'_'+ CAST(DATEPART(SECOND, GETDATE()) AS varCHAR(2))+'.zip'

    EXEC UDEF_KD_CompressFile 'D:\WebFiles\Web Update File Out\*** Update Files\@MyFileName', 'D:\WebFiles\Web Update File Out\*** Update Files\@MyFileNametxt'

  • It alright, I have sorted. Thanks for your help I am starting to understand the principles now

Viewing 7 posts - 1 through 6 (of 6 total)

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