windows通过批处理怎么查找关键字 ,脚本怎么写?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了windows通过批处理怎么查找关键字 ,脚本怎么写?相关的知识,希望对你有一定的参考价值。

我以前写的找文件的批,借你参考下
@echo off & setlocal enabledelayedexpansion
title by:chf
color 0a
set num=0
if exist %temp%\find.lst del %temp%\find.lst
:star
cls
echo.
echo ..........................................................
echo ^| by:chf ^|
echo ^| 1.模糊查找 2.普通查找 3.精确查找 ^|
echo ..........................................................
echo.
set /p chioce=请选择:
echo %chioce%|findstr /i "[^-0-9]" && goto error
if "%chioce%"=="" goto error
if %chioce% gtr 3 goto error
if %chioce% lss 1 goto error
set /p file=请输入关键字:
for /f "skip=1 tokens=*" %%i in ('wmic logicaldisk where "drivetype=3"
get name') do call :loop %%i
:loop
set m=%1
if not defined m goto exit
cls
if %chioce%==1 goto str1
if %chioce%==2 goto str2
if %chioce%==3 goto str3
:str
echo 文件"%file%"正在%m%盘中进行查找,请稍候,已经找到%num%个相关文件...
if exist %temp%\find.lst type %temp%\find.lst
for /f "tokens=*" %%j in ('dir %1\%filestr% /s /a /b') do (if not %%j==找不到文件 echo %%j>>%temp%\find.lst && set /a num+=1)
for /f "tokens=*" %%k in ('type %temp%\find.lst') do echo %%k
cls
goto :eof
:exit
cls
if %num%==0 (set/p=未找到与关键字'%file%'相关文件,<nul) else (echo 查找完成,共找到与%file%相关文件%num%个,路径如下:)
if exist %temp%\find.lst type %temp%\find.lst |more && del %temp%\find.lst
echo 按任意键退出。。。 & pause>nul & exit

:str1
if exist %temp%\str.lst del %temp%\str.lst
set sz=0
:xh
set file%sz%=!file:~%sz%,1!
if "!file%sz%!"==" " set file%sz%=*
if "!file%sz%!"=="" goto end
set /a sz+=1
goto xh
:end
set /a ssz=%sz%-1
for /l %%l in (0 1 %ssz%) do set/p a=!file%%l!*<nul>>%temp%\str.lst
for /f "tokens=*" %%m in ('type %temp%\str.lst') do set filestr=%%m
goto str

:str2
set filestr=*%file%*
goto str

:str3
set filestr=%file%
goto str

:error
cls
echo 选择有误,请重新选择...
ping /n 2 127.0.0.1>nul
goto star

::结束
参考技术A 问题不是很清楚。如果你是要从一个文本文件中提取包括某个关键字的行,语法如下:
findstr "test" test.txt>out.txt
结果会输出到out.txt文档中。findstr可以附带很多参数来查找,具体可以在命令行输入"findstr/?"看看。追问

我想同时查找很多关键字,这些关键字我不知道在哪个分区里,这些关键字可能是文件名,也可能是文件里面的内容,我想从整个磁盘搜索。

追答

例如要查找整个本地磁盘中名称含有某个关键字的文件,并将文件列表(含路径)输出到一个文本,如下代码
@echo off&setlocal enabledelayedexpansion
for /f "skip=1 tokens=1,2 delims=\" %%a in ('fsutil fsinfo drives^|find /v ""') do (
set aa=%%a
set aa=!aa:~-2!
for /f "tokens=1 " %%i in ('fsutil fsinfo drivetype !aa! ^|find "固定"') do (
for /f %%l in (keyword.txt) do dir /s /b "%%i\*%%l*">>keywordfile.txt
)
)
以上要先建立一个文本文件keyword.txt,每行输入一个关键字或词。
时间不够,先给一半答案

以上是关于windows通过批处理怎么查找关键字 ,脚本怎么写?的主要内容,如果未能解决你的问题,请参考以下文章

windows 批处理脚本怎么执行

在 Windows 批处理脚本的 for 循环中未通过“查找”命令获得所需的输出

通过组策略,启动脚本执行批处理,怎么客户端不生效

secureCRT脚本如何实现查找当前屏显文本中最后一个出现的关键字如sd*并获取该关键字呢?

zabbix server怎么让win执行批处理脚本?

Shell脚本处理文本换行问题