You are on page 1of 3

27/1/2018 Moving and renaming files using xp_cmdshell - SQL Server Q&A from the SQL Server Central

the SQL Server Central community

Bienvenido a AnswerHub. Encuentra preguntas y respuestas sobre tecnología.

Preguntas Etiquetas Usuarios Logros Por Responder Hacer una pregunta

Buscar

Preguntas Etiquetas Usuarios

Moving and renaming files using xp_cmdshell

I need to move and rename a file but for reasons I'm not going into can't use SSIS or DTS. I'm trying to use xp_cmdshell an
getting the message "The syntax of the command is incorrect". I'm using examples I've found on line and am confused as to
1 what I've done wrong. The rename should add the date onto the filename, for example EandWTest.txt should be moved to a
archive folder and renamed EandWTest_0110523_16_07.txt.

This is the code I've found and am trying to fit my needs, I'm not actually using the variables to stipulate the filename and pa
but will do that when I've got the basic code working:

DECLARE
@TodayDate as varchar(40),
@TodayHour as varchar(40),
@TodayMinu as varchar(40),
5. @NewFileName as varchar(100),
@cmdstr as varchar(128),
@cmd VARCHAR(255),
@sFileName AS VARCHAR(255),
@sPath AS VARCHAR(255)
10.
SET @sFileName = 'EandWTest.txt'
SET @sPath = 'C:\Auto Import\Auto Import\'

SELECT @TodayDate = CONVERT(varchar(10), GETDATE(), 112)


15. SELECT @TodayHour = DATEPART(hh,GETDATE())
SELECT @TodayMinu = DATEPART(mi,GETDATE())
SELECT @NewFileName = @sFileName + '_' + @TodayDate + '_' + @TodayHour + '_' + @TodayMinu + '.txt'
print @NewFileName
set @cmdstr='MOVE /Y C:\Auto Import\Auto Import\EandWTest.txt C:\Auto Import\Auto Import\Archive\' + @Ne
20. print @cmdstr
EXEC master..xp_cmdshell @cmdstr

Many thanks.

sql xp_cmdshell rename move

Más ▼ Preguntada 23 May 2011 a 07: 55 en


Default

Mrs_Fatherjack
5.2k ● 66 ● 69 ● 77

1 Respuesta: sort Más votadas primero ▼

https://ask.sqlservercentral.com/questions/40365/moving-and-renaming-files-using-xp-cmdshell.html 1/3
27/1/2018 Moving and renaming files using xp_cmdshell - SQL Server Q&A from the SQL Server Central community

You are missing quotes around


Bienvenido the path. You
a AnswerHub. have spaces
Encuentra in the folder
preguntas names and
y respuestas windows
sobre command line doesn't like tha
tecnología.
Just put double quotes around the filenames to fix that.
3
Also the value of @NewFileName is a bit messed up, just running the print command returned:

MOVE /Y C:\Auto Import\Auto Import\EandWTest.txt C:\Auto Import\Auto Import\Archive\EandWTest.txt_20110523_17_13

So the .txt part at the end is not quite right.


x
EDIT:

So you want the command to end up looking like this:

MOVE /Y "C:\Auto Import\Auto Import\EandWTest.txt" "C:\Auto Import\Auto Import\Archive\EAndWTest.200110523

Más ▼ Respondido 23 May 2011 a 07: 59

WilliamD
26.2k ● 18 ● 37 ● 48

@WilliamD Awesome, that's fantastic many thanks.

23 May 2011 a

Mrs_Fath

You're welcome.

23 May 2011 a

Your answer

[Ocultar previsualización]

Regístrate o accede para publicar tu respuesta

Copyright 2018 Redgate Software. Privacy Policy


Empresarial
Preguntas y
Respuestas Sociales

https://ask.sqlservercentral.com/questions/40365/moving-and-renaming-files-using-xp-cmdshell.html 2/3
27/1/2018 Moving and renaming files using xp_cmdshell - SQL Server Q&A from the SQL Server Central community

https://ask.sqlservercentral.com/questions/40365/moving-and-renaming-files-using-xp-cmdshell.html 3/3

You might also like