[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Batch Files That Counts By Date

Posted on 2008-11-13
7
Medium Priority
?
313 Views
Last Modified: 2012-05-05
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.
0
Comment
Question by:sherbug1015
  • 4
  • 3
7 Comments
 
LVL 43

Expert Comment

by:Steve Knight
ID: 22954411
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
 

Author Comment

by:sherbug1015
ID: 22960737
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 22961230
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
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 

Author Comment

by:sherbug1015
ID: 22963467
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
 
LVL 43

Accepted Solution

by:
Steve Knight earned 2000 total points
ID: 22971863
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
 

Author Comment

by:sherbug1015
ID: 22978501
I got it working.  Thanks.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 22979274
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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question