Search code examples
sql-serversql-server-2008ssisbcp

Need to change the file name (excel) using xp_cmdshell


In a file path (c:\Test\), I have an EXCEL file named as "SundayReport(WTD and MTD).xls"

I need to rename the file to "SundayReport_WTD_MTD).xls" using xp_cmdshell.

Kindly suggest me how to change the name.


Solution

    1. The RENAME dos-command would do. Type RENAME /? in Windows command shell (cmd.exe), see how you should run the command.

    2. Then format the command as you need it in an VARCHAR (eg @cmd) and execute it as EXEC xp_cmdshell @cmd;.


    Eg

    DECLARE @cmd VARCHAR(8000);
    SET @cmd='RENAME "C:\Test\SundayReport(WTD and MTD).xls" "SundayReport_WTD_MTD).xls"';
    EXEC xp_cmdshell @cmd;