morciani
asked on
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/rep ort.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!
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-
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!
Are these text files (i.e. can they be read in Notepad?) or are they in Outlook Message Format?
ASKER
They are in Outlook Message Format (.msg files).
Here you go.
Accepts wildcard for the filename and string is case insensitive. Search for how many lines has the string.
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%
)
ASKER
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
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
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
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%
)
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%"
)
ASKER
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!!!
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!!!
11.17.2008 at 02:37PM PST, ID: 22980311
This one should handle spaces for any inputs and for the output file.
This one should handle spaces for any inputs and for the output file.
ASKER
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
Reports2.zip
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Excellent! This does the job exactly as I need it to. Thank you!
ASKER
Excellent responses- this does the job just as I need it to. Thanks!
Thanks for the grade!