Monday, February 3, 2014

Windows - find newer and older files in command line or search file by date

1) Example, if today is 2014 Feb, 03 and I want to find files created in one week in folder
 C:\Users\jiansen\Desktop\bike, including sub-directories in Windows.

First type cmd in start menu, and get command prompt in Windows.7
forfiles /p  C:\Users\jiansen\Desktop\bike /s  /D +2014-1-27
Windows 2003 server, a little different:
forfiles /p  C:\Users\jiansen\Desktop\bike /s  /D +1/27/2014

Note: /p follows folder name, /s including sub-directories, +2014-1-27, means files newer than  2014-1-27

2) Find file created today
forfiles /p  C:\Users\jiansen\Desktop\bike /s  /D +0

3)Find file older than 7 days
forfiles /p  C:\Users\jiansen\Desktop\bike /s  /D -7

 ... The above commands only returns file name without path name. If output including full pathnames and file names, the command will be
forfiles /p  C:\Users\jiansen\Desktop\bike /s  /D -7 /C "cmd /c echo @path"
  Summary of forfiles command:
FORFILES [/P pathname] [/M searchmask] [/S]
         [/C command] [/D [+ | -] {yyyy-MM-dd | dd}]

    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

                        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
                        @ext     - returns only the extension of the
                        @path    - returns the full path of the file.
                        @relpath - returns the relative path of the
                        @isdir   - returns "TRUE" if a file type is
                                   a directory, and "FALSE" for files.
                        @fsize   - returns the size of the file in
                        @fdate   - returns the last modified date of the
                        @ftime   - returns the last modified time of the

                        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
                        "yyyy-MM-dd" 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.

    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 2001-01-01
             /C "cmd /c echo @fname is new since Jan 1st 2001"
    FORFILES /D +2014-2-3 /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"
Video:  Windows - find newer and older files in command line, search file by date

No comments:

Post a Comment