troubleshooting Question

Batch script to count total files by date and directory

Avatar of InvoiceInsight
InvoiceInsight asked on
Windows BatchMicrosoft DOS
20 Comments1 Solution12810 ViewsLast Modified:
I am trying to create a batch script that will provide some rudimentary reporting on the number of files that are placed in directories on a daily basis, weekly and monthly basis.  The script will be scheduled to run after hours using windows scheduler.  

For example:

If today's date is the 9-18-2007

If these are my files\directories:

d:\test\group1\cust1\9-17-07\file1
d:\test\group1\cust2\9-17-07\file1
d:\test\group1\cust1\9-18-07\file1
d:\test\group1\cust2\9-18-07\file1
d:\test\group1\cust2\9-18-07\file2
d:\test\group2\cust1\9-17-07\file1
d:\test\group2\cust2\9-17-07\file1
d:\test\group2\cust1\9-18-07\file1
d:\test\group2\cust2\9-18-07\file1
d:\test\group2\cust2\9-18-07\file2

I need a daily report that would tell me that a total of 6 new files were created this day, 2 for cust1 and 4 for cust 2

I need a weekly report that would tell me that the total number of files created for the week was 10, and detail the number of files per customer.

Same this for my monthly report.

I have logic that can determine the date already and I can get a listing of the number of files per directory, but I am stumped on how to get the subtotals and totals I am looking for.

Here is what I have so far (this example is for the "daily" report):

The output from the script below looks like this:

"E:\test\group1\cust1\09-17-07\file1"=1
"E:\test\group1\cust1\09-18-07\file1"=1
"E:\test\group1\cust2\09-17-07\file1"=1
"E:\test\group1\cust2\09-18-07\file1"=1
"E:\test\group1\cust2\09-18-07\file2"=1

@echo off

setlocal

set srcDir=E:\test
set dirMask=*.*

if not "%1"=="" set srcDir=%1

if not exist "%srcDir%" echo Directory %srcDir% does not exist.&goto :EOF

call :GETPARTS "%date%

call :SUBTRACTDAYS 1

set cutoffDate=%yy%/%mm%/%dd%

pushd.

cd /D %srcDir%

for /f "delims=" %%a in ('dir /b /a-d /S %dirMask%') do call :PROCESS "%%a" %%~ta

popd

goto :EOF

:PROCESS

call :GETPARTS %2

REM ================================================================================
REM This is where I get the totals for each directory, but I can seem to add them up
REM ================================================================================
if /i "%cutoffDate%" GTR "%yy%/%mm%/%dd%" dir %1
FOR /F "TOKENS=1" %%F IN ('DIR %1 /S^|FIND /i "file(s)"') DO (SET TOTAL=%%F)
Echo %1=%TOTAL%
REM ================================================================================

goto :EOF

:SUBTRACTDAYS

set dayCnt=%1

if "%dayCnt%"=="" set dayCnt=1

REM Substract your days here
set /A dd=1%dd% - 100 - %dayCnt%
set /A mm=1%mm% - 100

:CHKDAY

if /I %dd% GTR 0 goto DONESUBTRACT

set /A mm=%mm% - 1

if /I %mm% GTR 0 goto ADJUSTDAY

set /A mm=12
set /A yy=%yy% - 1

:ADJUSTDAY

if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
REM ** Month 12 falls through

:SET31

set /A dd=31 + %dd%

goto CHKDAY

:SET30

set /A dd=30 + %dd%

goto CHKDAY

:LEAPCHK

set /A tt=%yy% %% 4

if not %tt%==0 goto SET28

set /A tt=%yy% %% 100

if not %tt%==0 goto SET29

set /A tt=%yy% %% 400

if %tt%==0 goto SET29

:SET28

set /A dd=28 + %dd%

goto CHKDAY

:SET29

set /A dd=29 + %dd%

goto CHKDAY

:DONESUBTRACT

if /I %mm% LSS 10 set mm=0%mm%
if /I %dd% LSS 10 set dd=0%dd%

goto :EOF

:GETPARTS

set dt=%~1
set tok=1-3

if "%dt:~0,1%" GTR "9" set tok=2-4

set yyyy=

for /f "tokens=%tok% delims=.:/-, " %%a in ('echo %~1') do (
  for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do set %%x=%%a&set %%y=%%b&set %%z=%%c
)

if not "%yyyy%"=="" set yy=%yyyy%

if 1%yy% LSS 1000 (if %yy% LSS 70 (set yy=20%yy%) else (set yy=19%yy%))
if 1%mm% LSS 100 set mm=0%mm%
if 1%dd% LSS 100 set dd=0%dd%

goto :EOF
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 20 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 20 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros