SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


how to get filename of latest file in a directory with t/sql?


how to get filename of latest file in a directory with t/sql?

Author
Message
sqlguy-736318
sqlguy-736318
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1553 Visits: 449
I'm in the process of creating a database restore script for my staging server.

Whenever I restore a database on my staging server I always restore a bak file with the latest timestamp from a specific filesystem directory. Therefore, I'd like to automate this process in my restore script.

Can you show me the T/SQL I should use to get the filename of the bak file with the latest timestamp from a specific filesystem directory?
sql_dba123
sql_dba123
Old Hand
Old Hand (399 reputation)Old Hand (399 reputation)Old Hand (399 reputation)Old Hand (399 reputation)Old Hand (399 reputation)Old Hand (399 reputation)Old Hand (399 reputation)Old Hand (399 reputation)

Group: General Forum Members
Points: 399 Visits: 538
Check these links:

http://www.sqlservercentral.com/Forums/Topic595224-357-1.aspx
http://www.sqlservercentral.com/scripts/Backup+%2F+Restore/31995/
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)

Group: General Forum Members
Points: 433587 Visits: 43500
sqlguy-736318 (11/3/2011)
I'm in the process of creating a database restore script for my staging server.

Whenever I restore a database on my staging server I always restore a bak file with the latest timestamp from a specific filesystem directory. Therefore, I'd like to automate this process in my restore script.

Can you show me the T/SQL I should use to get the filename of the bak file with the latest timestamp from a specific filesystem directory?


What is the format of the filenames that you have? I ask because if they look like the following (has an ISO-style date and time embedded in the name), there is a VERY simple way to do this without using xp_CmdShell, SQLCLR, VBScripts, Powershell, or any other non-built-in method...

dbname_backup_200810292330.bak

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
sqlguy-736318
sqlguy-736318
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1553 Visits: 449
Jeff Moden (11/4/2011)
sqlguy-736318 (11/3/2011)
I'm in the process of creating a database restore script for my staging server.

Whenever I restore a database on my staging server I always restore a bak file with the latest timestamp from a specific filesystem directory. Therefore, I'd like to automate this process in my restore script.

Can you show me the T/SQL I should use to get the filename of the bak file with the latest timestamp from a specific filesystem directory?


What is the format of the filenames that you have? I ask because if they look like the following (has an ISO-style date and time embedded in the name), there is a VERY simple way to do this without using xp_CmdShell, SQLCLR, VBScripts, Powershell, or any other non-built-in method...

dbname_backup_200810292330.bak



Hi Jeff - The format of the file is MyDB_20111105_1030.bak.
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)SSC Guru (433K reputation)

Group: General Forum Members
Points: 433587 Visits: 43500
Oh, shoot. My apologies. I lost track of this thread. Here's the solution given the file name pattern you posted. I hope I'm not too terribly late with it.

DROP TABLE #File
GO
--===== Create a holding table for the file names
CREATE TABLE #File
(
FileName SYSNAME,
Depth TINYINT,
IsFile TINYINT
)
;
--===== Capture the names in the desired directory
-- (Change "C:\Temp" to the directory of your choice)
INSERT INTO #File
(FileName, Depth, IsFile)
EXEC xp_DirTree 'C:\Temp\',1,1
;
--===== Find the latest file using the "constant" characters
-- in the file name and the ISO style date.
SELECT TOP 1
FileName
FROM #File
WHERE IsFile = 1
AND FileName LIKE 'MyDB__20[0-9][0-9][0-1][0-9][0-3][0-9]__[0-2][0-9][0-5][0-9].bak' ESCAPE '_'
ORDER BY FileName DESC
;




--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search