I need help to write the sql script to find the latest backup file from the windows folder to restore the database. filename is like:-
dbnm_2019_4_5_11_30_613.bak
dbnm_2019_4_18_11_32_234.bak
dbnm_2019_4_11_11_37_34.bak
... name is made up using dbnm_year_month_date_hr_min_sec format.
used below script:-
CREATE TABLE #File
( FileName SYSNAME,
Depth TINYINT,
IsFile TINYINT
);
INSERT INTO #File
(FileName, Depth, IsFile)
EXEC xp_DirTree '[file location]',1,1;
is there anyway that I can insert date filed from the network folder to show when the backup file created and do the order by on that field to find the latest file.
when I am using top 1 in select statement, it is showing me 2019_4_5_11_30_613.bak
as latest file which is incorrect.