question

artistlover avatar image
artistlover asked

Copy file and add date

How can i copy a file and then add a date via job in tsql?
tsql
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Dave_Green avatar image Dave_Green ♦ commented ·
Do you mean to literally copy the file but change the file name to include the date, or add a date to the data inside the file?
0 Likes 0 ·
artistlover avatar image artistlover commented ·
Literally copy the file and change the name to include date.
0 Likes 0 ·
sqlaj 1 avatar image
sqlaj 1 answered
Does it absolutly need to be within T-SQL script? What about using Powershell to copy the file and rename? Copy-Item C:\logfiles\daily_file.txt -Destination D:\Archives\SQL $today = get-date -Displayhint DateTime rename-item -path D:\Archives\SQL\daily_file.txt -newname daily_file_$today.txt Copy-Item: http://technet.microsoft.com/en-us/library/hh849793.aspx Rename-Item: http://technet.microsoft.com/en-us/library/hh849763.aspx
3 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

artistlover avatar image artistlover commented ·
how can i have powershell run at certain time like a job would on sql server and would i do this on my local machine? I have never used power shell and literally i just want it done asap so i can move on to something else.
0 Likes 0 ·
sqlaj 1 avatar image sqlaj 1 commented ·
Powershell can be heavy but for something like this it can make life so much easier. Create a domain account with access to the required resources and use a Windows schedule job with that user the owner on the serer where the files live.
0 Likes 0 ·
artistlover avatar image artistlover commented ·
i don't even know where to begin
0 Likes 0 ·
artistlover avatar image
artistlover answered
declare @cmdstring varchar(1000) set @cmdstring = 'copy \\snap4\share1\xx\GMProductionReports(2).csv \\snap4\share1\xx\GMProductionReports(2)'+replace(convert(nvarchar(50), getdate(), 120), ' ',':')+'.csv' exec master..xp_cmdshell @cmdstring i am getting the below response when i execute The filename, directory name, or volume label syntax is incorrect.
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Dave_Green avatar image Dave_Green ♦ commented ·
Generally speaking, that's what I'd use - although I'd look at the use of colons in a file name - not sure it's allowed, per http://stackoverflow.com/questions/1665724/colon-asterisk-as-a-filename-delimiter/1665767#1665767. I'd also consider putting double quotes (") around each file name.
1 Like 1 ·
Jeff Moden avatar image
Jeff Moden answered
I realize that this is an old post but I didn't see an answer for what the OP actually wanted done. Considering that the OP responded to the PowerShell answer with an "I don't even know where to begin", I betting that a moderator marked it as the accepted answer and, with apologies to the author of that answer, I wish the moderators would stop marking answers just to have one. As for me, even if I had to do this through PowerShell, I wouldn't do it through a PowerShell prompt nor a Windows Scheduler job. As a "hybrid" DBA (combination System and Application DBA) as well as a Developer, I have more than enough to do without spreading my work across hell's half acre. I want it all in one spot so I can monitor it and that spot is the SQL Server Agent. ;-) It's bloody well bad enough that I have Web Methods running against my databases (not my choice, either). To wit, I strongly disagree with the moderators marking that as the accepted answer because I actually wouldn't tolerate that in my shop. ;-) On to the problem at hand... even though it's an old post... > declare @cmdstring varchar(1000) > > set @cmdstring = 'copy \snap4\share1\xx\GMProductionReports(2).csv \snap4\share1\xx\GMProductionReports(2)'+replace(convert(nvarchar(50), getdate(), 120), ' ',':')+'.csv' > exec master..xp_cmdshell @cmdstring That command might not even work from a CMD (DOS) window unless the current drive just happened to be the right one because it's missing the drive letter. Never trust defaults or the DOS "PATH" environment variable for such an important thing. It can and will make it necessary for you to quickly update your resume. ;-) That's something else that you have to understand. Any drive letters that you use must be on the SQL Server itself. If it’s anywhere else, you need to use the full UNC even if you’ve mapped a drive letter (unless it’s a drive letter that SQL Server has been assigned to attach as a resource). The UNC can be a bit tricky, as well. It has to be a “share” that the login for the SQL Server Service or the SQL Server Agent Service can actually “see” and has privs to. Further, as Dave Green already identified, you cannot use colons in your file names, period. It’s just not allowed because colons are reserved separators for drive letters or for labels in batch files (some use them for remarks... like me). Last but not least, I think it’s a waste of time to convert DOS commands to NVARCHAR() in most cases. That might only be because I’ve not had the pleasure of working in a CMD window where English wasn’t the assigned language. All of that brings us to a code example. If the name of the machine that the file is on is “SomeMachine” and the name of the share on that machine is “SomeShare”, then your code needs to look like the following… DECLARE @CMD VARCHAR(8000) ; SELECT @CMD = 'copy ' + '\\SomeMachine\SomeShare\snap4\share1\xx\GMProductionReports(2).csv ' + '\\SomeMachine\SomeShare\snap4\share1\xx\GMProductionReports(2)_' + REPLACE(REPLACE(REPLACE(CONVERT(CHAR(19),GETDATE(),120),':',''),' ','_'),'-','') + '.csv' ; EXEC xp_cmdshell @CMD ; Personally, I wouldn't do this copy. I'd simply do a file rename to prevent the duplication of data.
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.