Batch Files That Counts By Date

I need to write a batch file that will accept a date parameter and count the number of files in a directory for that date.  Any help is appreciated.
sherbug1015Asked:
Who is Participating?
 
Steve KnightConnect With a Mentor IT ConsultancyCommented:
There is no way it should count the characters....?  What does the dir line output when it shows it in the "if not "%1"=="" line...

Do you really mean c:new.... btw and not c:\new, i.e. at the moment it will work on the subdir new of the dir with the batch in?

Steve
0
 
Steve KnightIT ConsultancyCommented:
Well an easy way which may or not do what you want is:
@echo off
if "%1"=="" echo usage %0 dd/mm/yyyy & goto :eof
dir  | find "%1" /C

If you want to find everything edited in October then you could use 10/2008 as your date for instance and if you want files in all subdirs at /s to the dir command.

Steve
0
 
sherbug1015Author Commented:
This does not seem to be working.  I have attached a batch file that I am working from.  Rather than copy the files to another directory, I just want to count the files that match and write the results to the output file.  
@echo off
cls
SET LOG=GetFiles.out
 
REM ### Get the date
SET YYYYMMDD1=%1
SET YYYYMMDD2=%2
SET YYYYMMDD3=%3
SET YYYYMMDD4=%4
SET YYYYMMDD5=%5
 
 
if "%1"=="" GOTO CMDERROR
 
echo %YYYYMMDD1% >%LOG%
echo %YYYYMMDD2% >>%LOG%
echo %YYYYMMDD3% >>%LOG%
echo %YYYYMMDD4% >>%LOG%
echo %YYYYMMDD5% >>%LOG%
 
 
 
REM ### Copy yesterday's files to the working directory
if NOT "%1"=="" copy c:ABC\A*_%YYYYMMDD1%*.txt . >>%LOG%
 
 
 
echo ###########################
echo # All Done!               #
echo #                         #
echo ###########################
 
GOTO END
 
:CMDERROR
echo #
echo # Please pass the date as a command line argument (YYYYMMDD)
echo #
 
:END
pause

Open in new window

0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
Steve KnightIT ConsultancyCommented:
Doyu just want a count of the files that match this:  c:ABC\A*_%YYYYMMDD1%*.txt

In which case use

dir c:ABC\A*_%YYYYMMDD1%*.txt | find /v /c "xxxxx"

to get that into a variable try

for /f %% a in ('dir c:ABC\A*_%YYYYMMDD1%*.txt ^| find /v /c "xxxxx"') do echo There are %%a files in c:ABC\A*_%YYYYMMDD1%*.txt  >>%log%

(I think) Haven't tested!

Steve
0
 
sherbug1015Author Commented:
I incorporated your solution into my file and rather than counting the number of files, it is counting the number of characters in the filename.

For example if I have a directory called c:\abc.  In that directory I have three files named
ACA_20081112_1456.txt
ACA_20081113_1234.txt
ACA_20081113_5678.txt

I want to count all the files like A*_20081113*.txt
I would like the batch file to return 2

Is there a way I can do that?  I have attached the code with your solution incorporated.  It returns an 8, which is incorrect.

cls
SET LOG=GetFiles.out
 
REM ### Get the date
SET YYYYMMDD1=%1
SET YYYYMMDD2=%2
SET YYYYMMDD3=%3
SET YYYYMMDD4=%4
SET YYYYMMDD5=%5
 
 
if "%1"=="" GOTO CMDERROR
 
echo %YYYYMMDD1% >%LOG%
echo %YYYYMMDD2% >>%LOG%
echo %YYYYMMDD3% >>%LOG%
echo %YYYYMMDD4% >>%LOG%
echo %YYYYMMDD5% >>%LOG%
 
 
 
REM ### Copy yesterday's files to the working directory
if NOT "%1"=="" dir c:new\A*_%YYYYMMDD1%*.txt | find /v /c "xxxxx"
for /f %%a in ('dir c:new\A*_%YYYYMMDD1%*.txt ^| find /v /c "xxxxx"') do echo There are %%a files in c:ABC\A*_%YYYYMMDD1%*.txt . >>%log%
 
 
 
 
 
echo ###########################
echo # All Done!               #
echo #                         #
echo ###########################
 
GOTO END
 
:CMDERROR
echo #
echo # Please pass the date as a command line argument (YYYYMMDD)
echo #
 
:END
pause

Open in new window

0
 
sherbug1015Author Commented:
I got it working.  Thanks.
0
 
Steve KnightIT ConsultancyCommented:
Don't forget to select an answer if it helped and grade it appropriately... and always helpful to post what worked in the end for someone else watching.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.