我需要找到多个字符串,但我查找的字符串标记为批处理本身。我搜索字符串Incognito和包含它的批处理标志。我该如何防止这种情况?
编辑:这是我当前代码的一小部分。基本上它打开文本文件并在其中显示这个批处理文件和其他东西也很好,但是我需要它不要标记这个批处理文件。
findstr /i /s /m "Obligatory" *.* > str-unfixed.txt
if %errorlevel%==0 (
Call :TypeWriter "Obligatory Strings Found."
) else (
Call :TypeWriter "No Obligatory Strings Found."
)
start "" "C:\Users\%USERNAME%\Desktop\str-unfixed.txt"
不要担心TypeWriter,这只是一个片段。
Edit2:感谢编辑Mofi,但它在控制台上空白并且没有完成。 完整的脚本。我们走了。
@echo off
goto Next
::*************************************************************
:TypeWriter
echo(
(
echo strText=wscript.arguments(0^)
echo intTextLen = Len(strText^)
echo intPause = 30
echo For x = 1 to intTextLen
echo strTempText = Mid(strText,x,1^)
echo WScript.StdOut.Write strTempText
echo WScript.Sleep intPause
echo Next
)>%tmp%\%~n0.vbs
@cscript.EXE /noLogo "%tmp%\%~n0.vbs" "%~1"
echo(
exit /b
::**************************************************************
:Next
@echo off
set "OutputFile=%USERPROFILE%\Desktop\str-unfixed.txt"
%SystemRoot%\System32\findstr.exe /i /s /m "Vape" *.* | %SystemRoot%\System32\findstr.exe /E /L /V /C:"%~nx0" >"%OutputFile%"
for %%I in ("%OutputFile%") do if %%~zI == 0 goto VapeNoFileFound
for %%I in ("%OutputFile%") do if %%~zI == 1 goto VapeFileFound
:VapeFileFound
Call :TypeWriter "Vape Strings Found."
goto Kurium
:VapeNoFileFound
Call :TypeWriter "No Vape Strings Found."
goto Kurium
:Kurium
set "OutputFile=%USERPROFILE%\Desktop\str-unfixed.txt"
%SystemRoot%\System32\findstr.exe /i /s /m "Kurium" *.* | %SystemRoot%\System32\findstr.exe /E /L /V /C:"%~nx0" >>"%OutputFile%"
for %%I in ("%OutputFile%") do if %%~zI == 0 goto KuriumNoFileFound
for %%I in ("%OutputFile%") do if %%~zI == 1 goto KuriumFileFound
:KuriumFileFound
Call :TypeWriter "Kurium Strings Found."
goto Spook
:KuriumNoFileFound
Call :TypeWriter "No Kurium Strings Found."
goto Spook
:Spook
set "OutputFile=%USERPROFILE%\Desktop\str-unfixed.txt"
%SystemRoot%\System32\findstr.exe /i /s /m "Spook" *.* | %SystemRoot%\System32\findstr.exe /E /L /V /C:"%~nx0" >>"%OutputFile%"
for %%I in ("%OutputFile%") do if %%~zI == 0 goto SpookNoFileFound
for %%I in ("%OutputFile%") do if %%~zI == 1 goto SpookFileFound
:SpookFileFound
Call :TypeWriter "Spook Strings Found."
goto Aimassist
:SpookNoFileFound
Call :TypeWriter "No Spook Strings Found."
goto Aimassist
:Aimassist
set "OutputFile=%USERPROFILE%\Desktop\str-unfixed.txt"
%SystemRoot%\System32\findstr.exe /i /s /m "Aimassist" *.* | %SystemRoot%\System32\findstr.exe /E /L /V /C:"%~nx0" >>"%OutputFile%"
for %%I in ("%OutputFile%") do if %%~zI == 0 goto AANoFileFound
for %%I in ("%OutputFile%") do if %%~zI == 1 goto AAFileFound
:AAFileFound
Call :TypeWriter "Aim Assist Strings Found."
goto Triggerbot
:AANoFileFound
Call :TypeWriter "No Aim Assist Strings Found."
goto Triggerbot
:Triggerbot
set "OutputFile=%USERPROFILE%\Desktop\str-unfixed.txt"
%SystemRoot%\System32\findstr.exe /i /s /m "Triggerbot" *.* | %SystemRoot%\System32\findstr.exe /E /L /V /C:"%~nx0" >>"%OutputFile%"
for %%I in ("%OutputFile%") do if %%~zI == 0 goto TRNoFileFound
for %%I in ("%OutputFile%") do if %%~zI == 1 goto TRFileFound
cscript //nologo "C:\Users\%USERNAME%\Desktop\dedup.vbs" < "C:\Users\%USERNAME%\Desktop\str-unfixed.txt" > "C:\Users\%USERNAME%\Desktop\str-fixed.txt"
del /s /q "C:\Users\%USERNAME%\Desktop\str-unfixed.txt" > nul
:TRFileFound
Call :TypeWriter "Triggerbot Strings Found."
start "" "C:\Users\%USERNAME%\Desktop\str-fixed.txt"
:TRNoFileFound
Call :TypeWriter "No Triggerbot Strings Found."
start "" "C:\Users\%USERNAME%\Desktop\str-fixed.txt"
pause > nul
::************************************************
如果你需要dedup.vbs:
Set Inp = WScript.Stdin
Set Outp = Wscript.Stdout
Set Dict = CreateObject("Scripting.Dictionary")
Do Until Inp.AtEndOfStream
On Error Resume Next
Line=Inp.readline
Dict.Add Line, ""
Loop
For Each thing in Dict.Keys()
Outp.writeline thing
Next
Edit3:谢谢Mofi!
我的问题:需要让文本文档中的项目显示在cmd窗口中。
脚本:
@echo off
goto Next
::*************************************************************
:TypeWriter
echo(
(
echo strText=wscript.arguments(0^)
echo intTextLen = Len(strText^)
echo intPause = 30
echo For x = 1 to intTextLen
echo strTempText = Mid(strText,x,1^)
echo WScript.StdOut.Write strTempText
echo WScript.Sleep intPause
echo Next
)>%tmp%\%~n0.vbs
@cscript.EXE /noLogo "%tmp%\%~n0.vbs" "%~1"
echo(
exit /b
::**************************************************************
:Next
@echo off
findstr /i /s /m /C:"Vape" /C:"Kurium" /C:"Spook" /C:"Aimassist" /C:"Triggerbot" /C:"Smoothaim" *.* > str-unfixed.txt
cscript //nologo "C:\Users\%USERNAME%\Desktop\dedup.vbs" < "C:\Users\%USERNAME%\Desktop\str-unfixed.txt" > "C:\Users\%USERNAME%\Desktop\str-fixed.txt"
if %errorlevel%==0 (
Call :TypeWriter "Blacklisted Strings Found."
start "" "C:\Users\%USERNAME%\Desktop\str-fixed.txt"
) else (
Call :TypeWriter "No Blacklisted Strings Found."
start "" "C:\Users\%USERNAME%\Desktop\str-fixed.txt"
)
pause > nul
::************************************************
答案 0 :(得分:0)
可以使用以下代码过滤掉批处理文件:
@echo off
set "OutputFile=%USERPROFILE%\Desktop\str-unfixed.txt"
%SystemRoot%\System32\findstr.exe /I /M /S "Obligatory" *.* | %SystemRoot%\System32\findstr.exe /E /L /V /C:"%~nx0" >"%OutputFile%"
for %%I in ("%OutputFile%") do if %%~zI == 0 goto NoFileFound
call :TypeWriter "Obligatory strings found."
start "" "%OutputFile%"
set "OutputFile="
goto :EOF
:NoFileFound
del "%OutputFile%"
set "OutputFile="
call :TypeWriter "No obligatory string found."
goto :EOF
:TypeWriter
echo %~1
使用第二个 FINDSTR 过滤第一个 FINDSTR 的输出,该输出将输出所有不以搜索的批处理文件名称结尾的行作为区分大小写的字符串。< / p>
FOR 命令用于检查输出文件的文件大小。
输出文件是否为空文件,即文件大小等于0字节,除了批处理文件本身之外的任何文件中都找不到字符串Obligatory
,导致删除输出文件并打印相应的消息。
否则在打印出找到该字符串的消息后,将在与文件扩展名.txt关联的应用程序中打开输出文件。
上面的批处理文件在子目录上而不是在整个驱动器C上测试了两种情况:因为它可能需要很长时间才能完成C:。驱动器C上有数十万个文件:当使用*.*
作为文件名模式并从驱动器C的根目录开始时,总共有几十个GiB来搜索字符串。
编辑1:定义要分为两部分的字符串
如上所述更好的解决方案是避免搜索字符串在批处理文件中。这可以通过使用字符串的环境变量来完成,分两步找到并定义环境变量的值。
@echo off
setlocal
set "OutputFile=%USERPROFILE%\Desktop\str-unfixed.txt"
set "StringToFind=Obli"
set "StringToFind=%StringToFind%gatory"
%SystemRoot%\System32\findstr.exe /I /M /S "%StringToFind%" *.* >"%OutputFile%"
if errorlevel 1 (
del "%OutputFile%"
call :TypeWriter "No %StringToFind% strings found."
) else (
call :TypeWriter "%StringToFind% strings found."
start "" "%OutputFile%"
)
endlocal
goto :EOF
:TypeWriter
echo %~1
此批处理文件还会搜索Obligatory
,但它不包含此字符串,仅包含Obli
和gatory
。
编辑2:定义要使用省略的转义字符查找的字符串
这是另一个解决方案,其中要查找的多个字符串被分配给在搜索字符串中间的任何位置具有转义字符^
的环境变量,以排除批处理文件。
^
,因为在双引号字符串中找不到。请注意,此解决方案需要行
set SearchX=S^earch String
没有一个或多个尾随空格/制表符,因为尾随空格也会分配给环境变量。搜索将不再像预期的那样工作,这些行的末尾有一个不可见的空格或水平制表符作为搜索字符串的一部分。
此外,此批处理代码首先将包含6个搜索字符串之一的 FINDSTR 找到的每个文件名输出到控制台窗口,然后将文件名重定向到输出文本文件。
@echo off
setlocal EnableExtensions
set "OutputFile=%USERPROFILE%\Desktop\str-unfixed.txt"
rem Define all search strings with omitted escape character.
set Search1=V^ape
set Search2=K^urium
set Search3=S^pook
set Search4=A^imassist
set Search5=T^riggerbot
set Search6=S^moothaim
rem Delete the output file from a previous run if existing at all.
del "%OutputFile%" 2>nul
for /F "delims=" %%I in ('%SystemRoot%\System32\findstr.exe /I /M /S /C:"%Search1%" /C:"%Search2%" /C:"%Search3%" /C:"%Search4%" /C:"%Search5%" /C:"%Search6%" *') do (
echo %%I
echo %%I>>"%OutputFile%"
)
if exist "%OutputFile%" (
call :TypeWriter "Obligatory strings found."
start "" "%OutputFile%"
) else (
call :TypeWriter "No obligatory string found."
)
endlocal
goto :EOF
:TypeWriter
echo %~1
要了解使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。
call /?
del /?
echo /?
endlocal /?
findstr /?
for /?
goto /?
rem /?
set /?
setlocal /?
start /?
另请参阅Microsoft支持文章Testing for a Specific Error Level in Batch Files和Microsoft TechNet文章Using command redirection operators。
答案 1 :(得分:0)
通过另一个检查脚本名称来管道结果。
将第一行findstr /i /s /m "Obligatory" *.* > str-unfixed.txt
替换为:
findstr/ism "Obligatory" *.*|findstr/ixvc:"%~nx0">str-unfixed.txt
我会补充说,我强烈建议不要搜索C:盘上的每个文件,寻找特定的搜索词。你真的需要通过改变你的文件通配符/掩码来以某种方式过滤它。