• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1342
  • Last Modified:

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!
0
morciani
Asked:
morciani
  • 6
  • 6
1 Solution
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now