使用命令行查找在给定日期之后修改的 Windows 上的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17616045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 10:42:23  来源:igfitidea点击:

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

windowspowershellsearchcmdfind

提问by Daniel Pe?alba

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

回答by John Jesus

The forfilescommand worked for me on Windows 7. The article is here:

forfiles命令在 Windows 7 上对我有用。文章在这里:

Find files based on modified time

根据修改时间查找文件

Microsoft Technet documentation: ForfilesFor the example above:

Microsoft Technet 文档:Forfiles对于上面的示例:

forfiles /P <dir> /S /D +12/07/2013
  • /P The starting path to search
  • /S Recurse into sub-directories
  • /D Date to search, the "+" means "greater than" or "since"
  • /P 搜索的起始路径
  • /S 递归到子目录
  • /D 要搜索的日期,“+”表示“大于”或“自”

回答by Ralf de Kleine

You can use PowerShell to do this. Try:

您可以使用 PowerShell 执行此操作。尝试:

Get-ChildItem -Recurse | Where-Object { $_.LastWriteTime -ge "12/27/2016" }

回答by richard smallacombe

I was after the size of the files changed and did not have PowerShell. I used the following, but the clue came from other posts:

我是在文件大小改变之后没有 PowerShell。我使用了以下内容,但线索来自其他帖子:

http://www.scotiasystems.com/blog/it-hints-and-tips/quick-way-to-find-recently-changed-files-in-windowsand Windows command for file size only

http://www.scotiasystems.com/blog/it-hints-and-tips/quick-way-to-find-recently-changed-files-in-windows仅用于文件大小的 Windows 命令

set Target=E:\userdata
rem Date format is M-D-YYYY
set date=12-13-2013
set Filelist=d:\temp\filelist.txt
set Sizelist=d:\temp\sizelist%date%.csv

echo Target is %Target%
echo Start date is %date%
echo file list is %Filelist%
echo Sizelist is %sizelist%

Xcopy %Target% /D:%date% /L /S  > %Filelist%
echo FileSize (bytes), FileName > %sizelist%

For /f "tokens=1 delims=;" %%j in (%Filelist%) do (
                call :Size "%%j"
                )
Goto :EOF

:Size
@echo off
echo %~z1, %1 >> %sizelist%

回答by thehme

I had the same problem, so I created a list using XCOPY and the modified-by date I was looking for, used a forloop to traverse the list, and added the date/time information I needed for each file to a log:

我遇到了同样的问题,所以我使用 XCOPY 和我正在寻找的修改日期创建了一个列表,使用for循环遍历列表,并将每个文件所需的日期/时间信息添加到日志中:

xcopy X:\file_*.log X:\temp /D:07-17-2014 /L /Y > X:\files.txt
for /f "tokens=* delims=" %%a in (X:\files.txt ) do (
    @echo %%~ta %%a >> X:\files.log
)

It resulted in something like the following, which is exactly what I wanted.

结果如下,这正是我想要的。

X:\>()
07/17/2014 09:41 AM X:\file_201407170600.log

X:\>()
07/17/2014 09:41 AM X:\file_201407170615.log

X:\>()
07/17/2014 09:23 AM X:\file_201407170630.log

回答by David Hernán

You can search files modified after a given date using XCOPY as in this example, looking for files since last day of 2018:

您可以使用 XCOPY 搜索在给定日期之后修改的文件,如本例所示,查找自 2018 年最后一天以来的文件:

xcopy *.* c:\temp\*.* /D:12-31-2018 /L /S

In this example, you are in the directory where your search begins.

在此示例中,您位于搜索开始的目录中。

C:\temp*.* is only a syntax requisite, and nothing will be copied there.

C:\temp*.* 只是语法要求,不会复制任何内容。

/D:12-31-2018 specifies the files modified date you are looking for, including the specified date.

/D:12-31-2018 指定您要查找的文件修改日期,包括指定日期。

/L: Shows the filenames, including drive and path, also makes XCOPY do not copy any file.

/L:显示文件名,包括驱动器和路径,也使 XCOPY 不复制任何文件。

/S: Search in subdirectories.

/S: 在子目录中搜索。

回答by egallardo

If you decide to use PowerShell, this will also work with time and date ranges:

如果您决定使用 PowerShell,这也适用于时间和日期范围:

Get-ChildItem -Recurse | Where-Object {($_.LastWriteTime -ge "04/15/2018 20:00:00") -and ($
_.LastWriteTime -lt "04/15/2018 21:00:00")}

To use a programmatic date or a Locale agnostic date time:

要使用编程日期或区域设置不可知的日期时间:

Get-ChildItem -Recurse | Where-Object {($_.LastWriteTime -ge (new-object System.DateTime 2018, 1, 10, 12, 30, 00)) -and ($_.LastWriteTime -lt (new-object System.DateTime 2018, 1, 14, 12, 15, 59))}

Where date and time are entered in increasing time specificity order:

日期和时间以增加的时间特异性顺序输入的地方:

Year, Month, Day, Hour, Minute, Second

年、月、日、时、分、秒

回答by John Rocha

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).

我也遇到过类似的挑战。我使用了forfiles,但是我注意到它给出了 >= 而不仅仅是 >。输入日期是一个变量,所以我不想通过一堆箍/循环来计算date + 1 day(想想一个月的最后一天或一年的最后一天的逻辑)。

I created a function that ran forfilesfor 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_.

我创建了forfiles一个在特定日期运行的函数。这为我们提供了修改日期为>= _date_. 对于每个文件,我检查修改日期是否为_date_,并且当且仅当它不相等时才将输出打印到文件中。这意味着输出文件只有> _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.

最后,我的输入是从使用 MM/DD/YYYY 日期格式的文件中解析出来的,这意味着 7 月第一天是 07/01/2019。但是,来自 FORFILES 的日期不使用前导零,因此我需要通过删除前导零来进行翻译。

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.

我的第一次尝试是使用 /A 认为它会删除前导零。它不会将值读取为八进制——不要重复我第一次尝试的错误。所以我刚刚创建了一个小辅助函数 :getNum 来删除前导零。

:: ########################################################################
:: :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)