![](/img/trans.png)
[英]How to get last modified date on Windows command line for a set of files?
[英]Find files on Windows modified after a given date using the command line
我需要使用命令行搜索在给定日期之后修改的磁盘上的文件。
例如:
dir /S /B WHERE modified date > 12/07/2013
forfiles
命令无需借助 PowerShell 即可工作。 文章在这里:
Microsoft Technet 文档: forfiles
对于上面的例子:
forfiles /P <dir> /S /D +12/07/2013
您可以使用 PowerShell 执行此操作。 尝试:
Get-ChildItem -Recurse | Where-Object { $_.LastWriteTime -ge "12/27/2016" }
我是在文件大小改变之后没有 PowerShell。 我使用了以下内容,但线索来自其他帖子:
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%
您可以使用 XCOPY 搜索在给定日期之后修改的文件,如本例所示,查找自 2018 年最后一天以来的文件:
xcopy *.* c:\temp\*.* /D:12-31-2018 /L /S
在此示例中,您位于搜索开始的目录中。
C:\\temp*.* 只是语法要求,不会复制任何内容。
/D:12-31-2018 指定您要查找的文件修改日期,包括指定日期。
/L:显示文件名,包括驱动器和路径,也使 XCOPY 不复制任何文件。
/S
: 在子目录中搜索。
我遇到了同样的问题,所以我使用 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
)
结果如下,这正是我想要的。
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
如果您决定使用 PowerShell,这也适用于时间和日期范围:
Get-ChildItem -Recurse | Where-Object {($_.LastWriteTime -ge "04/15/2018 20:00:00") -and ($
_.LastWriteTime -lt "04/15/2018 21:00:00")}
要使用编程日期或区域设置不可知的日期时间:
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))}
日期和时间以增加的时间特异性顺序输入的地方:
年、月、日、时、分、秒
我也遇到过类似的挑战。 我使用了forfiles
,但是我注意到它给出了 >= 而不仅仅是 >。 输入日期是一个变量,所以我不想通过一堆箍/循环来计算date + 1 day
(想想一个月的最后一天或一年的最后一天的逻辑)。
我创建了一个为特定日期运行forfiles
的函数。 这为我们提供了修改日期为>= _date_
所有文件。 对于每个文件,我检查修改日期是否为_date_
,并且当且仅当它不相等时才将输出打印到文件中。 这意味着输出文件只有> _date_
文件条目。
然后我可以检查输出文件是否存在,以确定当前目录中是否有任何文件大于输入日期。 此外,如果我想知道哪些文件较新,我可以查看输出文件的内容。
最后,我的输入是从使用 MM/DD/YYYY 日期格式的文件中解析出来的,这意味着 7 月第一天是 07/01/2019。 但是,来自 FORFILES 的日期不使用前导零,因此我需要通过删除前导零来进行翻译。
我的第一次尝试是使用 /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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.