用于在最近文件中搜索字符串的批处理文件

发布于 2024-07-18 07:14:22 字数 296 浏览 9 评论 0原文

我有很多日志文件需要搜索某些字符串,并且想知道是否可以制作一个批处理文件来自动为我完成这项工作? 我需要它做的就是在某个目录中找到最新的日志,然后搜索该文件中的字符串。

我在这个网站上找到了下面的代码,它非常适合打开最新的日志文件,但不幸的是,我对批处理编程了解不够,无法修改代码以搜索字符串并显示该行。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅气称霸 2024-07-25 07:14:22

在末尾添加一个 findstr :

or /f "usebackq delims=" %%i in (`dir /b /o-d`) do findstr searchforthisstring %%i

这正在执行的操作是搜索“searchforthisstring”通过

dir /b/o-d 

Which list files 找到的文件(/b = 只是名称而不是任何其他信息和 /od 反向日期顺序

Add a findstr to the end:

or /f "usebackq delims=" %%i in (`dir /b /o-d`) do findstr searchforthisstring %%i

What this is doing is searching for "searchforthisstring" the files found by

dir /b/o-d 

Which list files (/b = simply name not any other info and /o-d reverse date order

谈下烟灰 2024-07-25 07:14:22

首先,设置您想要的文件。 如果 /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, and findstr to search for your STRING...

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文