DOS:查找一个字符串,如果找到则运行另一个脚本

发布于 2024-08-17 06:12:55 字数 137 浏览 5 评论 0原文

我想使用 DOS 在文件中查找字符串:

例如

查找“字符串”status.txt

当找到它时,我想运行一个批处理文件。

最好的方法是什么?

I want to find a string in a file using DOS:

For example

find "string" status.txt

And when it is found, I want to run a batch file.

What is the best way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

很酷不放纵 2024-08-24 06:12:55

我已经有一段时间没有对批处理文件做过任何事情了,但我认为以下方法有效:

find /c "string" file
if %errorlevel% equ 1 goto notfound
echo found
goto done
:notfound
echo notfound
goto done
:done

这确实是一个概念证明;根据您的需要进行清理。关键是,如果 string 不在 filefind 将返回 1errorlevel >。在这种情况下,我们分支到 notfound,否则我们处理 found 情况。

It's been awhile since I've done anything with batch files but I think that the following works:

find /c "string" file
if %errorlevel% equ 1 goto notfound
echo found
goto done
:notfound
echo notfound
goto done
:done

This is really a proof of concept; clean up as it suits your needs. The key is that find returns an errorlevel of 1 if string is not in file. We branch to notfound in this case otherwise we handle the found case.

舂唻埖巳落 2024-08-24 06:12:55
C:\test>find /c "string" file | find ": 0" 1>nul && echo "execute command here"
C:\test>find /c "string" file | find ": 0" 1>nul && echo "execute command here"
椒妓 2024-08-24 06:12:55
@echo off
cls
MD %homedrive%\TEMPBBDVD\
CLS
TIMEOUT /T 1 >NUL
CLS
systeminfo >%homedrive%\TEMPBBDVD\info.txt
cls
timeout /t 3 >nul
cls
find "x64-based PC" %homedrive%\TEMPBBDVD\info.txt >nul
if %errorlevel% equ 1 goto 32bitsok
goto 64bitsok
cls

:commandlineerror
cls
echo error, command failed or you not are using windows OS.
pause >nul
cls
exit

:64bitsok
cls
echo done, system of 64 bits
pause >nul
cls
del /q /f %homedrive%\TEMPBBDVD\info.txt >nul
cls
timeout /t 1 >nul
cls
RD %homedrive%\TEMPBBDVD\ >nul
cls
exit

:32bitsok
cls
echo done, system of 32 bits
pause >nul
cls
del /q /f %homedrive%\TEMPBBDVD\info.txt >nul
cls
timeout /t 1 >nul
cls
RD %homedrive%\TEMPBBDVD\ >nul
cls
exit
@echo off
cls
MD %homedrive%\TEMPBBDVD\
CLS
TIMEOUT /T 1 >NUL
CLS
systeminfo >%homedrive%\TEMPBBDVD\info.txt
cls
timeout /t 3 >nul
cls
find "x64-based PC" %homedrive%\TEMPBBDVD\info.txt >nul
if %errorlevel% equ 1 goto 32bitsok
goto 64bitsok
cls

:commandlineerror
cls
echo error, command failed or you not are using windows OS.
pause >nul
cls
exit

:64bitsok
cls
echo done, system of 64 bits
pause >nul
cls
del /q /f %homedrive%\TEMPBBDVD\info.txt >nul
cls
timeout /t 1 >nul
cls
RD %homedrive%\TEMPBBDVD\ >nul
cls
exit

:32bitsok
cls
echo done, system of 32 bits
pause >nul
cls
del /q /f %homedrive%\TEMPBBDVD\info.txt >nul
cls
timeout /t 1 >nul
cls
RD %homedrive%\TEMPBBDVD\ >nul
cls
exit
策马西风 2024-08-24 06:12:55

我们有两个命令,第一个是“condition_command”,第二个是“result_command”。
如果我们需要在“condition_command”成功时运行“result_command”(错误级别=0):

condition_command && result_command

如果我们需要在“condition_command”失败时运行“result_command”:

condition_command || result_command

因此,如果文件中有“string”,则运行“some_command” “status.txt”:

find "string" status.txt 1>nul && some_command

如果文件“status.txt”中没有“string”:

find "string" status.txt 1>nul || some_command

We have two commands, first is "condition_command", second is "result_command".
If we need run "result_command" when "condition_command" is successful (errorlevel=0):

condition_command && result_command

If we need run "result_command" when "condition_command" is fail:

condition_command || result_command

Therefore for run "some_command" in case when we have "string" in the file "status.txt":

find "string" status.txt 1>nul && some_command

in case when we have not "string" in the file "status.txt":

find "string" status.txt 1>nul || some_command
不离久伴 2024-08-24 06:12:55

由于答案被标记为正确,因此它是一个 Windows Dos 提示脚本,这也将起作用:

find "string" status.txt >nul && call "my batch file.bat"

As the answer is marked correct then it's a Windows Dos prompt script and this will work too:

find "string" status.txt >nul && call "my batch file.bat"
罗罗贝儿 2024-08-24 06:12:55

这更简单:

find /c "string" file
if %errorlevel% not equ 1 goto something

That's more straightforward:

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