Search code examples
windowsbatch-filecommand-linebatch-processingforfiles

How to identify/get the file by its timestamp in a batch file?


I have a list of csv files with date and time appended like "Account_data_yyyymmdd.csv" which are added daily along with its timestamp to source dir .I have to identify latest file ie.'Account_data_2020_08_05.csv' and set the value in variable . so i can pass it as argument

Files in source dir

  • Account_data_2020_08_05.csv
  • Account_data_2020_08_04.csv
  • Account_data_2020_08_03.csv

I have to find the recently placed file based on its timestamp & pass it as input for calling another batch process. Highlighted text is the argument to batch file.How to find latest file based on its timestamp and pass it as argument for

echo "start"
call process.bat "C:\CSVDataLod"  AccntDataloadprocess ***"dataAccess.name=C:\SourceDir\ Account_data_%year%_%month%_%date%.csv"***

Solution

  • The easiest and fastest method to get name of CSV file with newest date in file name is using command DIR with option /O-N to get the CSV file names output ordered by name in reverse order. The file name with newest name is output first by DIR in this case. The output of DIR has to be captured and processed with FOR. The FOR loop is exited after running the other batch file with first file name output by DIR.

    @echo off
    setlocal EnableExtensions DisableDelayedExpansion
    set "FileFound="
    set "FileNamePattern=Account_data_20??_??_??.csv"
    if /I "%~x1" == ".csv" set "FileNamePattern=%~nx1"
    for /F "delims=" %%I in ('dir "C:\SourceDir\%FileNamePattern%" /A-D /B /O-N 2^>nul') do (
        echo Processing file %%I ...
        call process.bat "C:\CSVDataLod" AccntDataloadprocess "dataAccess.name=C:\SourceDir\%%I"
        if /I not "%~1" == "/A" goto EndBatch
        set "FileFound=1"
    )
    if not defined FileFound echo There is no file "%FileNamePattern%" in directory "C:\SourceDir".
    :EndBatch
    endlocal
    

    I recommend opening a command prompt and running:

    dir "C:\SourceDir\Account_data_20??_??_??.csv" /A-D /B /O-N
    

    Then you know which lines are processed by FOR. Next run

    dir "C:\SourceDir\Account_data_20??_??_??.csv" /A-D /B
    dir "C:\SourceDir\Account_data_20??_??_??.csv" /A-D /B /ON
    

    to see how DIR outputs the CSV file names without specifying a specific order resulting in printing the file names as returned by the file system and explicitly ordered by name in alphabetical order instead of reversed alphabetical order.

    The file system NTFS returns a list of file names matched by a wildcard pattern in local specific alphabetic order while FAT file systems like FAT16, FAT32, exFAT return the file names not ordered at all. In real all file systems return the file names in order as stored in the table of the file system. The file systems use just different methods on how to add a file name to table of the file system. The FAT file systems append a new file name always at end of the table of a directory while NTFS inserts a new file name in table of a directory using a local specific alphabetic sort algorithm.

    Read the Microsoft documentation about Using command redirection operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background with %ComSpec% /c and the command line within ' appended as additional arguments.

    Edit:

    The batch file can be run with /a or /A as argument to process all CSV files matching the wildcard pattern from newest to oldest instead of just the newest. The batch file can be also run with name of a .csv file in source directory to process this specific CSV file instead of the newest CSV file.

    To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

    • call /?
    • dir /?
    • echo /?
    • endlocal /?
    • for /?
    • goto /?
    • setlocal /?