How to get count of files created/modified today in a folder using batch file

喜你入骨 提交于 2019-12-13 20:25:59

问题


Below command gives me count of files in a folder, I want the count of today's created/modified files in this folder, TIA.

set LocalFolder=D:\Myfolder
SET file_to_be_copied_Cnt=0
for %%o IN (%LocalFolder%/*.*) DO (       
      SET /A file_to_be_copied_Cnt=file_to_be_copied_Cnt + 1   
)
echo %file_to_be_copied_Cnt%

回答1:


What about this:

forfiles /P "%LocalFolder%" /M "*" /D +0 /C "cmd /C if @isdir==FALSE echo _" | find /C "_"

Or this if you want to include sub-directories:

forfiles /S /P "%LocalFolder%" /M "*" /D +0 /C "cmd /C if @isdir==FALSE echo _" | find /C "_"

How it works:

  • forfiles returns a _ character for each file modified today (/D +0 means modified on date of today or newer);
  • the if query avoids directories to be regarded/counted;
  • find counts (/C) the amount of returned lines containing _ characters;

To assign the resulting number to a variable, use a for /F lop:

for /F %%N in ('forfiles /P "%LocalFolder%" /M "*" /D +0 /C "cmd /C if @isdir==FALSE echo _" ^| find /C "_"') do set "NUMBER=%%N"
echo %NUMBER%

Or:

for /F %%N in ('forfiles /S /P "%LocalFolder%" /M "*" /D +0 /C "cmd /C if @isdir==FALSE echo _" ^| find /C "_"') do set "NUMBER=%%N"
echo %NUMBER%

Note the escaped pipe ^|.




回答2:


If you're not actually copying the files, just counting them, you could probably use this:

@Echo Off
Set "LocalFolder=D:\MyFolder"
Set "i=0"
For /F "Tokens=*" %%A In (
    'RoboCopy "%LocalFolder%" Null /S /MaxAge:1 /L /XJ /NS /NC /NDL /NP /NJH /NJS'
) Do Set /A i+=1
Echo %i% files modified today
Pause

If you're also copying the files and need to know the number copied then you could probably use this:

@Echo Off
Set "SrcDir=D:\Projects"
Set "DstDir=E:\Backups"
Set "i=0"
For /F "Tokens=*" %%A In (
    'RoboCopy "%SrcDir%" "DstDir" /S /MaxAge:1 /XJ /NS /NC /NDL /NP /NJH /NJS'
) Do Set /A i+=1
Echo %i% files copied
Pause


来源:https://stackoverflow.com/questions/49485025/how-to-get-count-of-files-created-modified-today-in-a-folder-using-batch-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!