Find files on Windows modified after a given date using the command line

前端 未结 7 1265
南旧
南旧 2021-02-04 07:40

I need to search a file on my disk modified after a given date using the command line.

For example:

   dir /S /B WHERE modified date > 12/07/2013
         


        
7条回答
  •  花落未央
    2021-02-04 08:27

    I had a similar challenge. I used forfiles, however I noticed that it gave >= rather than just >. The input date was a variable, and so I didn't want to go through a bunch of hoops/loops to calculate date + 1 day (think about logic for last day of month or last day of year).

    I created a function that ran forfiles for a particular date. That gives us all files with a modification date that is >= _date_. And for each file I check if the modification date is _date_, and print the output to a file if and only if it is not equal. That means the output file only has entries for files that are > _date_.

    I can then check for existence of the output file to determine if any files in the current directory are greater than the input date. Moreover, if I wanted to know which files are newer, I can view the contents of the output file.

    Finally, my input was parsed from a file that used a date format of MM/DD/YYYY, meaning it would be 07/01/2019 for July first. However, the date from FORFILES does not use leading zeros so I need to translate by dropping leading zeros.

    My first attempt was to use /A thinking it drops the leading zeros. It doesn't it reads the value as octal -- don't repeat the mistake of my first attempt. So I just created a little helper function :getNum that drops leading zeros.

    :: ########################################################################
    :: :findNewerFiles
    ::
    ::     Windowsfile interface will only tell you if a file is
    ::     greater than OR EQUAL to the current date. Had to figure a way
    ::     to get just greater than.
    ::
    ::     Use 'forfiles' to find all files that are >= the specific date, and for
    ::     each file if the files date is not equal to the specific date then echo
    ::     the file information into the new-file-log.
    ::
    ::     If the new-file-log exists after we're all done, then it means there is
    ::     at least one file newer than the specified date.
    ::
    :: PARMS:
    ::
    ::     %1 - MONTH
    ::
    ::     %2 - DAY
    ::
    ::     %3 - YEAR
    ::
    :: ERRORLEVEL:
    ::     0  if no newer files found
    ::     !0 if a newer file was found
    ::
    :findNewerFiles
    SETLOCAL
        CALL :getNum M "%~1"
        CALL :getNum D "%~2"
        CALL :getNum Y "%~3"
    
    
        SET "RETVAL=1"
    
        %bu.callecho% PUSHD %G[3PROOT_UNC]%
    
        ECHO Last build date was: %M%/%D%/%Y%
        del "%G[NEW_FILE_LOG]%" >nul 2>&1
    
        FORFILES /p .\ /S /D +%M%/%D%/%Y% ^
                 /c "cmd /c if /I @ISDIR == false if not @fdate==%M%/%D%/%Y% echo @fdate @path>>%G[NEW_FILE_LOG]%"
    
        IF EXIST "%G[NEW_FILE_LOG]%" (
            SET "RETVAL=0"
            TYPE "%G[NEW_FILE_LOG]%"
        )
    
        %bu.callecho% POPD
    (ENDLOCAL
     EXIT /B %RETVAL%)
    
    
    
    
    
    :: ########################################################################
    :: :getNum
    ::
    ::     Remove leading 0 from input number and place results in output variable
    ::
    :: PARMS:
    ::
    ::     %1 - OUTPUT VARIABLE
    ::          Name of the output variable to be populated
    ::
    ::     %2 - INPUT VARIABLE
    ::          The number to have leading zeros trimmed from
    ::
    :: ERRORLEVEL:
    ::     0  always
    ::
    :getNum
    SETLOCAL
        SET "D=%~1"
        SET "M=%~2"
    
        IF "%M:~0,1%" EQU "0" (
            SET "M=%M:~1%"
        )
    
    (ENDLOCAL
     SET "%D%=%M%"
     EXIT /B 0)
    

提交回复
热议问题