2016-11-14 283 views
1

我需要找到多个字符串,但是我查找标记的字符串在批处理中。我搜索字符串隐身和批量包括它的标志。我如何防止这种情况?如何从FindStr中排除某些内容?


编辑:这是我当前代码的一个片段。基本上它打开文本文件并在其中显示这个批处理文件和其他的东西,这很好,但我需要它不要标记这个批处理文件。

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,这只是一个片段。


编辑2:感谢编辑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:由于渔业部!

我的问题:需要获取文本文件中的项目以显示在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

目前尚不清楚你要求什么。请发布您的当前代码和明确的问题陈述。请参阅[如何提问](https://stackoverflow.com/help/how-to-ask) –

+0

你是什么意思“标记在批处理本身”。显示输出结果和预期结果 –

+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其输出所有不以搜索的批处理文件名称结尾的行作为区分大小写的文字字符串。

FOR命令用于检查输出文件的文件大小。

如果输出文件是空文件,即文件大小等于0字节,则除批处理文件本身外,在任何文件中都找不到字符串Obligatory,从而导致删除输出文件并打印相应的消息。

否则,在打印找到该字符串的消息后,会在与文件扩展名.txt关联的应用程序中打开输出文件。

以上批处理文件已在子目录上进行了测试,而不是在整个驱动器C上进行了测试,因为完成C:操作可能需要很长时间。驱动器C上有数十万个文件:当使用*.*作为文件名称模式并以驱动器C:的根目录开始时,总共有几十个GiB来搜索字符串。


EDIT 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,但它不包含此字符串,只是Obligatory


编辑2:定义字符串,找到省略转义字符

这里是一个多解决方案,其中多个字符串找到的中间被分配到环境变量与转义字符^任何地方搜索字符串以排除批处理文件。

^被Windows命令解释程序删除,将搜索字符串分配给环境变量,因为在双引号字符串中未找到搜索字符串。请注意,这个解决方案需要线

set SearchX=S^earch String 

不具有1个或多个尾随空格/制表符为结尾的空白(S)将也被分配到环境变量。如预期的那样,搜索将无法继续,因为在搜索字符串的一部分中,这些行的末尾不可见空间或水平制表符。

此外,该批次的代码首先输出由含有FINDSTR的6个搜索串中的一个到控制台窗口和下一重定向文件名到输出文本文件中的每个文件的名称。

@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

0

只需通过另一次检查脚本名称来管理结果。

这个替换您的第一线,findstr /i /s /m "Obligatory" *.* > str-unfixed.txt

findstr/ism "Obligatory" *.*|findstr/ixvc:"%~nx0">str-unfixed.txt 

我会然而补充一点,我会强烈建议不要通过对C的每一个文件搜索:驱动器寻找一个特定的搜索词。你真的需要通过改变你的文件通配符/掩码来过滤它。

相关问题