Rename a file with xp_cmdshell

  • Hi,

    I am trying to rename a file through SQL Server.

    I always receive an error saying that the sintax is incorrect.

    Can someone please help?

    Thank you.

    Command:

    declare @command varchar(8000)

    set @command = 'rename c:\sgrs\SGRS_SGCTCentral_7_20111111102846.rar c:\sgrs\verificadoSGRS_SGCTCentral_7_20111111102846.rar'

    exec master.dbo.xp_cmdshell @command

  • Is that syntax error coming from SQL Server or from the Cmd Exec? Please post the exact error message.

    Thanks

    John

  • declare @command varchar(8000)

    set @command = 'rename c:\sgrs\SGRS_SGCTCentral_7_20111111102846.rar verificadoSGRS_SGCTCentral_7_20111111102846.rar'

    exec master.dbo.xp_cmdshell @command

    __________________________
    Allzu viel ist ungesund...

  • river1 (11/17/2011)


    I am trying to rename a file through SQL Server.

    I don't want to irritate you with useless so-called advice, but... why are you doing it in T-SQL?

    Don't you think that a batch script, VBScript or any other programming language would be a better choice for this task?

    -- Gianluca Sartori

  • I need to do it in t-sql.

  • When i run the t-sql, it gives me backup a table with a column named output.

    This grid as two rows.

    The firts says : The syntax of the command is incorrect.

    The secund : NULL

  • Remove the path from the target name for the file, so that you specify only the file name itself.

    John

  • I made as you told me to:

    declare @cmd as varchar(8000)

    set @cmd = ' ren c:\sgrs\SGRS_SGCTCentral_7_20111111102846.rar

    verificado1.rar'

    EXEC master..xp_cmdshell @cmd

    Same error....

    🙁

  • river1 (11/17/2011)


    I need to do it in t-sql.

    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!

  • Looks correct. The only thing I can think of is that you have a carriage return character in your command.

    John

  • "carriage return character" sorry, what's that? (My english is not very good)

  • It's what you get when you press the "Return" or "Enter" key on your keyboard.

    John

  • make sure the string is a single line when sent to the cmd :

    create table #Results (

    ID int identity(1,1) NOT NULL,

    TheOutput varchar(1000))

    declare @cmd as varchar(8000)

    set @cmd = ' ren c:\sgrs\SGRS_SGCTCentral_7_20111111102846.rar verificado1.rar'

    insert into #Results (TheOutput)

    EXEC master..xp_cmdshell @cmd

    select * from #Results

    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!

  • Problem solved with Lowel suggestion.

    Thank you very much for the help.

  • It had to be in a single line!

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

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