批处理-在文本文件中搜索指定的字符串,如果找到则将变量设置为1 [英] Batch - Search text file for specified string and if found set variable to 1
问题描述
我似乎找不到答案,所有问题似乎都集中在将文本文件中的内容设置为变量上.我只想查看我提供的字符串是否存在于文本文件中的任何位置. (单独一行,不与其他字符串共享一行)
I can't seem to find a answer for this, all the questions seem focused on setting whats found in the text file as the variable. I just want to see if the string I have provided exists anywhere in the text file. (on its own line, not sharing a line with another string)
一个例子:
%usrname%=鱼
%usrname%= fish
%banfile%=只是其中没有任何内容的文本文件.
%banfile%= is just a text file with NOTHING in it.
for /f "usebackq tokens=*" %%f in ('find /c /i "%usrname%" "%banfile%"') do set banned=1
即使文本文件中什么也没有,它仍然设置%banned%= 1.我希望它仅在文本文件内找到一个或多个字符串"fish"时才设置变量%banned%= 1.
Even with nothing in the text file, it still sets %banned%=1. I want it to set the variable %banned%=1 only if it finds one or more of the string "fish" inside the text file.
我才刚刚开始理解for
命令,因此对于大多数人来说,这可能非常简单.感谢您的帮助!
I am just beginning to understand the for
command, so for most people this is probably going to be very simple. Thanks for any help!
推荐答案
您不需要为此的for
循环:
find /i "%usrname%" "%banfile%" >nul 2>&1&&set /a banned=1 || set /a banned=0
echo %banned%
这篇关于批处理-在文本文件中搜索指定的字符串,如果找到则将变量设置为1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!