1

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%
mandeep
  • 21
  • 1
  • 5

2 Answers2

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

aschipfl
  • 31,767
  • 12
  • 51
  • 89
  • pl answer one more question @aschipfl, link is https://stackoverflow.com/questions/49448609/how-to-get-todays-modified-folders-in-a-directory-using-batch-file – mandeep Mar 26 '18 at 11:58
0

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
Compo
  • 34,184
  • 4
  • 22
  • 36
  • this is count files on folder itself but not counting files created or modified in subfolders as well? – mandeep Mar 26 '18 at 09:45
  • This returns files that are at most 24 hours old, which is not the same as today's files... – aschipfl Mar 26 '18 at 10:01
  • @mandeep, take a look at your question, you mentioned only `folder` and used only code for a `folder` not `subfolders`! If you had asked for `subfolders` or recursive, I'd have formulated my answer accordingly. I've now edited my question to do that, _by including the `/S` option in the `RoboCopy` command_. – Compo Mar 26 '18 at 11:39
  • Could you please test it and provide feedback, thank you. – Compo Mar 26 '18 at 11:57