Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count files in specific month batch file

Tags:

batch-file

I am very new in batch file. what I have now is when i run the .bat it will generate the number of files from the 6 different folders.

@echo off &setlocal

echo Please Enter Cycle Month (MM format)
set /p month=
echo.

echo In the month of %month%,
echo.

set "startfolder1=\\L99016\SBM3276\Backup"
set /a counter1=0
for /r "%startfolder1%" %%i in (*.txt) do set /a counter1+=1
echo SBM3276 have %counter1% files.
echo. 

set "startfolder2=\\L99016\SBM3277\Backup"
set /a counter2=0
for /r "%startfolder2%" %%i in (*.txt) do set /a counter2+=1
echo SBM3277 have %counter2% files.
echo.

set "startfolder3=\\L99016\SBM3461\Backup"
set /a counter3=0
for /r "%startfolder3%" %%i in (*.txt) do set /a counter3+=1
echo SBM3461 have %counter3% files.
echo. 

set "startfolder4=\\L99016\SBM3462\Backup"
set /a counter4=0
for /r "%startfolder4%" %%i in (*.txt) do set /a counter4+=1
echo SBM3462 have %counter4% files.
echo.

set "startfolder5=\\L99016\SBM3697\Backup"
set /a counter5=0
for /r "%startfolder5%" %%i in (*.txt) do set /a counter5+=1
echo SBM3697 have %counter5% files.
echo. 

set "startfolder6=\\L99016\SBM3934\Backup"
set /a counter6=0
for /r "%startfolder6%" %%i in (*.txt) do set /a counter6+=1
echo SBM3934 have %counter6% files.
echo.

pause

the output on the cmd.exe will be:

Please Enter Cycle Month (MM format)
01

In the month of 01,
SBM3276 have 6560 files.
SBM3277 have 6372 files. 
SBM3461 have 6228 files. 
SBM3462 have 6179 files. 
SBM3697 have 6372 files.
SBM3934 have 6372 files. 

The question is, how do i filter out to only count the number of files in the specified month which is from the user's input?

Please help.

Thank you very much in advance!

***Note: the code above count all files in the folder with all dates. I only want a specific month. Please help!

like image 937
Haniz Avatar asked Dec 30 '25 08:12

Haniz


1 Answers

@echo off
setlocal enabledelayedexpansion
set /p "inp=Month [MM]: "
FOR %%i in (*) do (
  set "dt=%%~ti"
  if "!dt:~3,2!"=="%inp%" echo %%i
)  

the ~t modifier gives you the date. Extract the month-part to compare.

You can read about modifiers in for /?

As aschipfl notes: "Note that the format of the date given by the ~t modifier depends on the region/locale settings of your system, so the sub-string expansion code might need to be adapted accordingly..."

If you need a solution independent of regional settings, you can adapt the last part of this answer. (although wmic is much slower than just getting a substring)

like image 153
Stephan Avatar answered Jan 05 '26 12:01

Stephan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!