Reputation: 2311
I want to write a batch script / vbs that should read a text file line by line and search for a keyword. if the keyword is found, it should redirect to the output file.
The sample log is as follow,
Tue 01/09/2000
06:53 PM
--------C:\Data--------
C:\Data\2009
C:\Data\2010
C:\Data\2011
C:\Data\Filename.txt
C:\Data\Fileusage
C:\Data\Guidline.xml
C:\Data\2009\12
C:\Data\2009\12\01\667e33999.txt
C:\Data\2009\12\01\667e45454999.xml
C:\Data\2009\12\09\667dfder999.pdf
C:\Data\2009\12\09\667e332324.pdf
C:\Data\2009\12\09\867fdfe2323.pdf
C:\Resource\Findings\233.txt
C:\Resource\Findings\234.txt
C:\Resource\Findings\235.txt
C:\Resource\Findings\236.txt
C:\Resource\Findings\237.txt
C:\Resource\Findings\238.txt
C:\Resource\Findings\Lasted\433.txt
C:\Resource\Findings\Lasted\239.txt
C:\Resource\Findings\Lasted\890.txt
C:\Resource\Findings\Lasted\121.txt
C:\Resource\Findings\Lasted\009.txt
C:\Resource\Findings\Lasted\999.txt
Total Files Listed:
12 File(s) 7,234,336 bytes
0 Dir(s) 3,413,392,345 bytes free
Basically i want to capture any line that starts with C:\Data\Year\Month and C:\Resource\Findings\Lasted
The output as below
C:\Data\2009\12\01\667e33999.txt
C:\Data\2009\12\01\667e45454999.xml
C:\Data\2009\12\09\667dfder999.pdf
C:\Data\2009\12\09\667e332324.pdf
C:\Data\2009\12\09\867fdfe2323.pdf
C:\Resource\Findings\Lasted\433.txt
C:\Resource\Findings\Lasted\239.txt
C:\Resource\Findings\Lasted\890.txt
C:\Resource\Findings\Lasted\121.txt
C:\Resource\Findings\Lasted\009.txt
C:\Resource\Findings\Lasted\999.txt
Anyone can give me a hand ?
Upvotes: 0
Views: 692
Reputation: 354834
You can use findstr
:
findstr /b /r "C:\\Resource\\Findings\\Lasted\\ C:\\Data\\[0-9][0-9][0-9][0-9]\\[0-9][0-9]\\." data.dump
findstr
is for filtering lines from a file which is what we're doing here. First we enable regular expression matching with /r
. Don't be deceived, those are very, very minimal regexes that break every now and then so it can be tricky to get them right. /b
is for matching the beginning of a line. Then there is a space-separated list of things to look for, in this case either C:\Resource\Findings\Lasted\
or C:\Data\Year\Month
where the year has four and the month two digits.
Upvotes: 2