• 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.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)