简体   繁体   中英

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

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%

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM