用于在最近文件中搜索字符串的批处理文件
我有很多日志文件需要搜索某些字符串,并且想知道是否可以制作一个批处理文件来自动为我完成这项工作? 我需要它做的就是在某个目录中找到最新的日志,然后搜索该文件中的字符串。
我在这个网站上找到了下面的代码,它非常适合打开最新的日志文件,但不幸的是,我对批处理编程了解不够,无法修改代码以搜索字符串并显示该行。
for /f "usebackq delims=" %%i in (`dir /b /o-d`) do @call "%%i"&goto :eof
任何帮助将非常感激。
I have alot of log files which need searching for certain strings and was wondering if I could make a batch file to automate this job for me? All I need it to do is locate the most recent log in a certain directory then search for the string in that file.
I have found the below code on this website which works great to open the most recent log file but unfortunatly I dont know enough about batch programming to amend the code to search for the string and display the line.
for /f "usebackq delims=" %%i in (`dir /b /o-d`) do @call "%%i"&goto :eof
Any help would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在末尾添加一个 findstr :
这正在执行的操作是搜索“searchforthisstring”通过
Which list files 找到的文件(/b = 只是名称而不是任何其他信息和 /od 反向日期顺序
Add a findstr to the end:
What this is doing is searching for "searchforthisstring" the files found by
Which list files (/b = simply name not any other info and /o-d reverse date order
首先,设置您想要的文件。 如果
/od
不起作用,请尝试/od
...for /f %%i in ('dir \path\to\files\ /b /od') 请设置 myfile=%%i
...并注意,因为
myfile
将没有路径。然后使用
for /f "tokens=*"
完整读取文件的每一行,并使用findstr
搜索您的STRING
...for /f "tokens=*" %%i in (\path\to\files\%myfile%) do (echo %%i | findstr STRING >> OUTPUTFILE)
如果你想要< code>OUTPUTFILE 每次运行代码时都会被覆盖,请使用单个
>
。如果您不需要文件,但在屏幕上看到输出,只需删除
>>; OUTPUTFILE
代码。At first, set which is the file you want. If
/od
doesn't work, try/o-d
...for /f %%i in ('dir \path\to\files\ /b /od') do set myfile=%%i
... and pay attention because
myfile
will come without pathway.Then use
for /f "tokens=*"
to read each line of the file fully, andfindstr
to search for yourSTRING
...for /f "tokens=*" %%i in (\path\to\files\%myfile%) do (echo %%i | findstr STRING >> OUTPUTFILE)
If you want
OUTPUTFILE
to be overwritten everytime you run the code, use a single>
.If you don´t want a file, but see the output on screen, just delete the
>> OUTPUTFILE
code.