3

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!

Haniz
  • 33
  • 6
  • Your question isn't clear to me, it seems that you already have everything you need. Am i missing something? – Pedro Lobito Apr 22 '16 at 09:38
  • Hi Pedro! I've added a note in the last paragraph. – Haniz Apr 22 '16 at 10:00
  • is it correct that counter5 incremented twice? and you are displaying counter2 value in 5 & 6, too ... – MikeD Apr 22 '16 at 10:43
  • 1
    Instead of copying the same code portion for every directory, you could use a `for` loop instead, containing in once only: `for /D /R "\\L99016" %%D in ("SBM*") do (...)`; in the loop body, use `"%%~fD\Backup"` to access the sub-dir. of the currently iterated directory; you should remove the `/R` argument of your code snippet then; instead, let te `for %%I` loop iterate through `"%%~fD\Backup\*.txt"`... – aschipfl Apr 22 '16 at 14:43

1 Answers1

3
@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)

Community
  • 1
  • 1
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • 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... – aschipfl Apr 22 '16 at 12:27
  • @aschipfl yes - worth to mention. – Stephan Apr 22 '16 at 12:39
  • hi all experts. thank you for sharing the knowledge. @Stephan how do i add your code inside my current source codes? i added your codes inside but it doesnt do anything... – Haniz Apr 25 '16 at 01:45