title: How to get latest file name from folder?
body: <p>I have used below to get the list of files from folder. For the list I need to get only the latest file name. </p>
  
  <p>Please suggest, where do I need to change the code to get the latest file name?</p>
  
  <pre><code>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 N'\\Backup-server\Include\Monthly\Sales\',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 'Sales_backup_*.bak' ESCAPE '_'
    ORDER BY FileName DESC
  
    select *from #file
  </code></pre>