批处理/vbs读取文本文件
我想编写一个批处理脚本/vbs,它应该逐行读取文本文件并搜索关键字。如果找到关键字,它应该重定向到输出文件。
示例日志如下,
星期二 01/09/2000 06:53 下午
--------C:\数据--------
C:\Data\2009
C:\Data\2010
C:\Data\2011
C:\Data\文件名.txt
C:\Data\Fileusage
C:\Data\Guidline.xml
C:\数据\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
列出的文件总数:
12 个文件 7,234,336 字节
0 Dir(s) 3,413,392,345 bytes free
基本上我想捕获以 C:\Data\Year\Month 和 C:\Resource\Findings\Lasted 开头的任何行
输出如下
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
有人可以帮我吗?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
findstr
:findstr
用于过滤文件中的行,这就是我们在这里所做的。首先我们使用/r
启用正则表达式匹配。不要被欺骗,这些都是非常非常小的正则表达式,它们时不时就会被破坏,所以要让它们正确是很困难的。/b
用于匹配行的开头。然后是一个以空格分隔的要查找的内容列表,在本例中为C:\Resource\Findings\Lasted\
或C:\Data\Year\Month
其中年份有四位数,月份有两位数。You can use
findstr
: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 eitherC:\Resource\Findings\Lasted\
orC:\Data\Year\Month
where the year has four and the month two digits.