Solved

xp_cmdshell copy file and append date to name

Posted on 2003-10-28
6
1,922 Views
Last Modified: 2007-12-19
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
0
Comment
Question by:tbaseflug
  • 3
  • 3
6 Comments
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
Too many Open questions.
0
 

Author Comment

by:tbaseflug
Comment Utility
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
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:tbaseflug
Comment Utility
wow - works great!  How would I format the date to something like:
mm_dd_yy
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 125 total points
Comment Utility
REPLACE(CONVERT(CHAR(8), GETDATE(), 1), '/', '_')
0
 

Author Comment

by:tbaseflug
Comment Utility
Awesome!  Thanks!!!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now