xp_cmdshell copy file and append date to name

I am trying to copy a file and append the date to the name - I do not want the time, date only - using the xp_cmdshell (unless there is a better way) - I cannot get the date concatentaion for the life of me?

declare @rc int, @varDate sysname
SET @varDate = getdate()
exec @rc = master..xp_cmdshell 'copy c:\test\test_error_log.txt c:\test\test_error_log_"+@varDate+".txt', no_output
select @rc
tbaseflugAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Scott PletcherConnect With a Mentor Senior DBACommented:
REPLACE(CONVERT(CHAR(8), GETDATE(), 1), '/', '_')
0
 
Scott PletcherSenior DBACommented:
Too many Open questions.
0
 
tbaseflugAuthor Commented:
I know - I am making the effort today to go and close some of them out - have been out of town for the past few months and have not had chance for cleanup
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Scott PletcherSenior DBACommented:
In that case, maybe try something like this:


declare @rc int
DECLARE @cmd VARCHAR(500)
SET @cmd = 'copy c:\test\test_error_log.txt c:\test\test_error_log_' +
      CONVERT(CHAR(8), GETDATE(), 112) + '.txt'
exec @rc = master..xp_cmdshell @cmd, NO_OUTPUT
select @rc

This will put the date in YYYYMMDD format; if you need another format, just let me know :-)
0
 
tbaseflugAuthor Commented:
wow - works great!  How would I format the date to something like:
mm_dd_yy
0
 
tbaseflugAuthor Commented:
Awesome!  Thanks!!!
0
All Courses

From novice to tech pro — start learning today.