Search code examples
batch-filedir

display files between 2 dates in batch scripting


I need to display files between 2 dates (e-g. dates 23-sep-2012 to 30-sep-2012) in a folder using a batch script.

There are approximately a half million files in the folder.

How can I do this?


Solution

  • Working with file timestamps is a pain in batch. The easiest solution is to use WMIC.

    Here is a simple script that will do what you want. It can easily be modified to use CreationDate instead of LastModified date. You can also add and subtract additional columns to the output. Type wmic datafile get /? to get a list of available columns (remove the spaces from the names). The output can be piped to SORT if you want the results sorted by date.

    The dates must be specified in YYYYMMDD format.

    @echo off
    :listFilesBetweenDates  Path  StartDate  EndDate
    ::
    ::  List files found in directory Path with last modified date between
    ::  StartDate and EndDate
    ::
    ::  path = Directory to list. Use . for current directory
    ::
    ::  startDate = YYYYMMDD format
    ::
    ::  endDate = YYYYMMDD format
    ::
    setlocal
    set "drive=%~d1"
    set "folder=%~pnx1\"
    set /a "start=%~2, end=%~3+1"
    wmic datafile where "drive='%drive%' and path='%folder:\=\\%' and lastModified>='%start%' and lastModified<'%end%'" get lastModified, name
    


    Sample usage - list files in current directory between 07-SEP-2012 and 17-SEP-2012

    D:\test>listFilesBetweenDates . 20120907 20120917
    LastModified               Name
    20120917155412.408435-240  d:\test\idlist.txt
    20120910160033.573531-240  d:\test\input.txt
    20120907180451.138275-240  d:\test\myurls.txt
    20120911115129.949457-240  d:\test\replace.bat
    20120917155853.541247-240  d:\test\temp1.txt
    20120917162208.635197-240  d:\test\temp2.txt
    20120917162217.595287-240  d:\test\temp3.txt
    20120911141557.105798-240  d:\test\test,2.txt
    20120910160622.664376-240  d:\test\test.vbs
    20120907161948.016121-240  d:\test\test3.bat
    20120910153717.558353-240  d:\test\test3.txt
    


    Sample usage: List files in temp directory between 26-SEP-2012 and 27-SEP-2012 sorted by last modified date in ascending order

    D:\test>listFilesBetweenDates "%temp%" 20100926 20120927 | sort
    
    20120926085420.368034-240  d:\users\xxxxxx\temp\~df8a4e7af0245496b2.tmp
    20120926085422.630049-240  d:\users\xxxxxx\temp\fxsapidebuglogfile.txt
    20120926085433.781136-240  d:\users\xxxxxx\temp\sof8436.tmp
    20120926085436.861167-240  d:\users\xxxxxx\temp\cvr941b.tmp.cvr
    20120926103842.351663-240  d:\users\xxxxxx\temp\~dfe34cb61c66a1dcd7.tmp
    20120926181118.453780-240  d:\users\xxxxxx\temp\~dfd0009f24c8c5eb31.tmp
    20120927100903.382946-240  d:\users\xxxxxx\temp\~df559b9f2b2763ac3f.tmp
    20120927100915.210126-240  d:\users\xxxxxx\temp\cvr3d51.tmp.cvr
    20120927100918.680196-240  d:\users\xxxxxx\temp\sof47cf.tmp
    20120927144827.689200-240  d:\users\xxxxxx\temp\tmpdbc8.tmp
    20120927144828.169209-240  d:\users\xxxxxx\temp\tmpddad.tmp
    20120927184908.066005-240  d:\users\xxxxxx\temp\~df401234aa9008c5e1.tmp
    LastModified               Name