VBScript or batch file to search a directory for a certain file name, count the occurances of a string within each file

Hello,
I have a number of reports that are emailed and saved as .msg files to a directory on a daily basis.   I'm looking for some VBScript that will do the following:

1.  Search the directory (lets say '\\directory') for all files named 'report.msg'.  Note that the directory is structured as follows:

directory
      2008
            1-2008
                  1-1-08
                  1-2-08
                  1-3-08
                  1-4-08
                  ...
            2-2008
            3-2008
            ...
      2007
            1-2007
            2-2007
            3-2007
            ...
      2006
            1-2006
            2-2006
            3-2006
            ...
      2005
      ...

Files named 'report.msg' could lie anywhere within the directory structure.

2.  For every 'report.msg' file, count the occurances of a particular string, say 'xyz'.  The end result would be a Results.txt file looking something like:

<FilePath>, <SearchString>, <NumberOfOccurances>
Ex.
//directory/2008/1-2008/1-1-2008/report.msg, xyz, 10

3.  It would be best if the script could prompt the user for the directory (ex. '//directory'), file name (ex. 'report.msg'), and search string (ex. 'xyz').

-Thanks for the help!
morcianiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shift-3Commented:
Are these text files (i.e. can they be read in Notepad?) or are they in Outlook Message Format?
0
morcianiAuthor Commented:
They are in Outlook Message Format (.msg files).
0
AmazingTechCommented:
Here you go.

Accepts wildcard for the filename and string is case insensitive. Search for how many lines has the string.
SETLOCAL ENABLEDELAYEDEXPANSION
SET Output=C:\Output.csv
SET /p SearchPath=Enter full path to search from: 
SET /p Filename=Enter the filename to search for: 
SET /p String=Enter the string to search for: 
 
IF NOT DEFINED SearchPath ECHO "No path specified" & PAUSE & GOTO :EOF
IF NOT DEFINED Filename ECHO "No filename specified" & PAUSE & GOTO :EOF
IF NOT DEFINED String ECHO "No string specified" & PAUSE & GOTO :EOF
 
ECHO "File Path","Search String","Number of Occurances">%Output%
FOR /r "%SearchPath%" %%a in (%Filename%) DO (
    Set NumOccurances=
    FOR /f %%b in ('TYPE "%%a" ^| FIND /c /i "%String%"') DO SET NumOccurances=%%b
    ECHO "%%a","%String%","!NumOccurances!">>%Output%
)

Open in new window

0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

morcianiAuthor Commented:
Hello- I can't seem to get this to work.  Please see the sample data I created in the attached Reports.zip file.  The data that I'm working with is in a 'C:\Documents and Settings\Test' folder.  

The routine does run correctly, and creates the Output.csv file, but I don't get the expected results in the file.

The I've included the Batch.cmd (renamed Batch.txt) file and what I would expect for results (and the actual results) as well.  Thanks for the help!
Reports.zip
0
AmazingTechCommented:
OK. Try this one instead. It was also getting the directory into the listing.

Your examples in the reports for folder 2 and 3 the txt file is not named ABC renaming does produce the expected output with this script.

So your inputs should be:

C:\DistributedReports
abc.txt
r03


SETLOCAL ENABLEDELAYEDEXPANSION
SET Output=C:\Output.csv
SET /p SearchPath=Enter full path to search from: 
SET /p Filename=Enter the filename to search for: 
SET /p String=Enter the string to search for: 
 
IF NOT DEFINED SearchPath ECHO "No path specified" & PAUSE & GOTO :EOF
IF NOT DEFINED Filename ECHO "No filename specified" & PAUSE & GOTO :EOF
IF NOT DEFINED String ECHO "No string specified" & PAUSE & GOTO :EOF
 
ECHO "File Path","Search String","Number of Occurances">%Output%
FOR /f %%a in ('dir /b /a-d /s "%SearchPath%\%Filename%"') DO (
    Set NumOccurances=
    FOR /f %%b in ('TYPE "%%a" ^| FIND /c /i "%String%"') DO SET NumOccurances=%%b
    ECHO "%%a","%String%","!NumOccurances!">>%Output%
)

Open in new window

0
AmazingTechCommented:
Oh. Use this one if your output will have spaces in it.
SETLOCAL ENABLEDELAYEDEXPANSION
SET Output=C:\Output.csv
SET /p SearchPath=Enter full path to search from: 
SET /p Filename=Enter the filename to search for: 
SET /p String=Enter the string to search for: 
 
IF NOT DEFINED SearchPath ECHO "No path specified" & PAUSE & GOTO :EOF
IF NOT DEFINED Filename ECHO "No filename specified" & PAUSE & GOTO :EOF
IF NOT DEFINED String ECHO "No string specified" & PAUSE & GOTO :EOF
 
ECHO "File Path","Search String","Number of Occurances">"%Output%"
FOR /f %%a in ('dir /b /a-d /s "%SearchPath%\%Filename%"') DO (
    Set NumOccurances=
    FOR /f %%b in ('TYPE "%%a" ^| FIND /c /i "%String%"') DO SET NumOccurances=%%b
    ECHO "%%a","%String%","!NumOccurances!">>"%Output%"
)

Open in new window

0
morcianiAuthor Commented:
Thank you very much for helping with this!  I am seeing it work when there are no spaces in the Directory input and Filename input.  My real world inputs do include spaces:

Directory: G:\Distributed File Reports
File names: Distributed File Report.msg

Is there anyway to deal with spaces in both the 'search' directory and file name inputs?  Many thanks!!!
0
AmazingTechCommented:
11.17.2008 at 02:37PM PST, ID: 22980311

This one should handle spaces for any inputs and for the output file.
0
morcianiAuthor Commented:
Hi- check out the attached zip file (it includes test input files, with and without spaces and the batch file).  It works when an input of 'a_bc.txt' is used for the file name, but not when 'a bc.txt' is used for the file name.  Any help is appreciated!!!
Reports2.zip
0
AmazingTechCommented:
Oh. Yes you are right the for command is delimiting the dir.

Sorry try this.
SETLOCAL ENABLEDELAYEDEXPANSION
SET Output=C:\Output.csv
SET /p SearchPath=Enter full path to search from: 
SET /p Filename=Enter the filename to search for: 
SET /p String=Enter the string to search for: 
 
IF NOT DEFINED SearchPath ECHO "No path specified" & PAUSE & GOTO :EOF
IF NOT DEFINED Filename ECHO "No filename specified" & PAUSE & GOTO :EOF
IF NOT DEFINED String ECHO "No string specified" & PAUSE & GOTO :EOF
 
ECHO "File Path","Search String","Number of Occurances">"%Output%"
FOR /f "tokens=*" %%a in ('dir /b /a-d /s "%SearchPath%\%Filename%"') DO (
    Set NumOccurances=
    FOR /f %%b in ('TYPE "%%a" ^| FIND /c /i "%String%"') DO SET NumOccurances=%%b
    ECHO "%%a","%String%","!NumOccurances!">>"%Output%"
)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
morcianiAuthor Commented:
Excellent!  This does the job exactly as I need it to.  Thank you!
0
morcianiAuthor Commented:
Excellent responses- this does the job just as I need it to.  Thanks!
0
AmazingTechCommented:
Thanks for the grade!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

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.