Find Files Older than XX/XX/XX

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Find Files Older than XX/XX/XX

#1 Post by Samir » 18 Jan 2014 12:45

I was looking at the output of an xcopy from one drive to another to update newer files and started thinking of how to simply query a drive for a list of files older than xx/xx/xx. Find can be used to find files of a certain date easily with

Code: Select all

dir /s|find /i "xx-xx-xxxx"
but I want something that can also find files newer than that date.

Any assistance appreciated.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Find Files Older than XX/XX/XX

#2 Post by Squashman » 18 Jan 2014 13:07

Forfiles command.

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Find Files Older than XX/XX/XX

#3 Post by Endoro » 18 Jan 2014 18:51


penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Find Files Older than XX/XX/XX

#4 Post by penpen » 19 Jan 2014 04:27

It is also possible to use findstr with its regular expression capability:
Assumed:
- date format: DD.MM.YYYY
- minimum date: 01.01.1900

If you want to list all date <= 23.11.2013:
- 01.01.1900 - 31.12.1999: "..[.]..[.]19"
- 01.01.2000 - 31.12.2009: "..[.]..[.]200"
- 01.01.2010 - 31.12.2012: "..[.]..[.]201[0-2]"
- 01.01.2013 - 30.09.2013: "..[.]0[1-9][.]2013"
- 01.10.2013 - 31.10.2013: "..[.]10[.]2013"
- 01.11.2013 - 19.11.2013: "[0-1][1-9][.]11[.]2013"
- 20.11.2013 - 23.11.2013: "2[0-3][.]11[.]2013"

Code: Select all

dir /s | findstr /R /C:"..[.]..[.]19" /C:"..[.]..[.]200" /C:"..[.]..[.]201[0-2]" /C:"..[.]0[1-9][.]2013" /C:"..[.]10[.]2013" /C:"[0-1][1-9][.]11[.]2013" /C:"2[0-3][.]11[.]2013"


If you want to list all date >= 24.11.2013 (additionally assumed maxium date is somewhen in 2014):
- 24.11.2013 - 29.11.2013: "2[4-9][.]11[.]2013"
- 30.11.2013 - 30.11.2013: "30[.]11[.]2013"
- 01.12.2013 - 31.12.2013: "..[.]12[.]2013"
- 01.01.2014 - 31.12.2014: "..[.]..[.]2014"

Code: Select all

dir /s | findstr /R /C:"2[4-9][.]11[.]2013" /C:"30[.]11[.]2013" /C:"..[.]12[.]2013" /C:"..[.]..[.]2014"

penpen

PS: You may shorten the needed params by different regular expressions, for example you may inverse the second search to simulate the first one (second solution with the findstr option "/V").

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Find Files Older than XX/XX/XX

#5 Post by Samir » 27 Feb 2014 11:08

Thank you for the replies!
Squashman wrote:Forfiles command.
Where would I find this?
penpen wrote:

Code: Select all

dir /s | findstr /R /C:"2[4-9][.]11[.]2013" /C:"30[.]11[.]2013" /C:"..[.]12[.]2013" /C:"..[.]..[.]2014"
I tried this, but it didn't work. The resulting output was nothing even though there's a couple of hundred 2014 files in that tree. :?:

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Find Files Older than XX/XX/XX

#6 Post by Squashman » 27 Feb 2014 11:25

Samir wrote:Thank you for the replies!
Squashman wrote:Forfiles command.
Where would I find this?

Windows Vista and above have it installed by default. If you are running Windows XP just google search for the download. We have posted the link to the download on this site a couple of times.

Code: Select all

H:\>forfiles /?

FORFILES [/P pathname] [/M searchmask] [/S]
         [/C command] [/D [+ | -] {MM/dd/yyyy | dd}]

Description:
    Selects a file (or set of files) and executes a
    command on that file. This is helpful for batch jobs.

Parameter List:
    /P    pathname      Indicates the path to start searching.
                        The default folder is the current working
                        directory (.).

    /M    searchmask    Searches files according to a searchmask.
                        The default searchmask is '*' .

    /S                  Instructs forfiles to recurse into
                        subdirectories. Like "DIR /S".

    /C    command       Indicates the command to execute for each file.
                        Command strings should be wrapped in double
                        quotes.

                        The default command is "cmd /c echo @file".

                        The following variables can be used in the
                        command string:
                        @file    - returns the name of the file.
                        @fname   - returns the file name without
                                   extension.
                        @ext     - returns only the extension of the
                                   file.
                        @path    - returns the full path of the file.
                        @relpath - returns the relative path of the
                                   file.
                        @isdir   - returns "TRUE" if a file type is
                                   a directory, and "FALSE" for files.
                        @fsize   - returns the size of the file in
                                   bytes.
                        @fdate   - returns the last modified date of the
                                   file.
                        @ftime   - returns the last modified time of the
                                   file.

                        To include special characters in the command
                        line, use the hexadecimal code for the character
                        in 0xHH format (ex. 0x09 for tab). Internal
                        CMD.exe commands should be preceded with
                        "cmd /c".

    /D    date          Selects files with a last modified date greater
                        than or equal to (+), or less than or equal to
                        (-), the specified date using the
                        "MM/dd/yyyy" format; or selects files with a
                        last modified date greater than or equal to (+)
                        the current date plus "dd" days, or less than or
                        equal to (-) the current date minus "dd" days. A
                        valid "dd" number of days can be any number in
                        the range of 0 - 32768.
                        "+" is taken as default sign if not specified.

    /?                  Displays this help message.

Examples:
    FORFILES /?
    FORFILES
    FORFILES /P C:\WINDOWS /S /M DNS*.*
    FORFILES /S /M *.txt /C "cmd /c type @file | more"
    FORFILES /P C:\ /S /M *.bat
    FORFILES /D -30 /M *.exe
             /C "cmd /c echo @path 0x09 was changed 30 days ago"
    FORFILES /D 01/01/2001
             /C "cmd /c echo @fname is new since Jan 1st 2001"
    FORFILES /D +2/27/2014 /C "cmd /c echo @fname is new today"
    FORFILES /M *.exe /D +1
    FORFILES /S /M *.doc /C "cmd /c echo @fsize"
    FORFILES /M *.txt /C "cmd /c if @isdir==FALSE notepad.exe @file"

H:\>

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Find Files Older than XX/XX/XX

#7 Post by penpen » 27 Feb 2014 13:42

Samir wrote:
penpen wrote:

Code: Select all

dir /s | findstr /R /C:"2[4-9][.]11[.]2013" /C:"30[.]11[.]2013" /C:"..[.]12[.]2013" /C:"..[.]..[.]2014"
I tried this, but it didn't work. The resulting output was nothing even though there's a couple of hundred 2014 files in that tree. :?:

It should work (tested on Win7 and WinXP).
Is your date format DD.MM.YYYY?
Another possibility is, that you want to use another date in dir (option /P).

If that all is not the cause, please use some sample files in a directory where only some should be listed,
then post their names, their dates, and the dir /s output.

penpen

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Find Files Older than XX/XX/XX

#8 Post by Samir » 26 Aug 2014 15:03

penpen wrote:
Samir wrote:
penpen wrote:

Code: Select all

dir /s | findstr /R /C:"2[4-9][.]11[.]2013" /C:"30[.]11[.]2013" /C:"..[.]12[.]2013" /C:"..[.]..[.]2014"
I tried this, but it didn't work. The resulting output was nothing even though there's a couple of hundred 2014 files in that tree. :?:

It should work (tested on Win7 and WinXP).
Is your date format DD.MM.YYYY?
Another possibility is, that you want to use another date in dir (option /P).

If that all is not the cause, please use some sample files in a directory where only some should be listed,
then post their names, their dates, and the dir /s output.

penpen
My date format is MM.DD.YYYY so that was probably it.

But after exploring forfiles.exe, that solves the issue. I should be able to use it to easily do what I want. 8)

Thank you all for your help!

Post Reply