How to get count of files created/modified today i

2019-08-22 08:20发布

问题:

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