| @echo off&setlocal enabledelayedexpansion |
| :Start |
| title 批处理文本检索 Batch Text Retrieval V1.1.4 |
| cls&echo;&echo; |
| echo; 【使用说明】 |
| echo; ——————————————————————————————————————— |
| echo; 将文档资料转换为ANSI编码的TXT文件,并置于本程序所在目录下。 |
| echo; 使用关键词搜索,多个关键词用空格分开,如“美女 野兽 恋爱”。 |
| echo; 可用命令[Cmd、Dir、Find、Findstr]自定义搜索,如:findstr /? |
| echo; |
| echo; [1]高级选项; [2]文件管理; |
| echo; [B Words]百度搜索。 |
| echo; |
| echo; ——————————————————————————————————————— |
| echo;&echo;&echo; |
| set input= |
| set /p "input= 请输入:" |
| if "%input%"=="" goto Start |
| set/a input1=input2=input3= |
| for /f "tokens=1,2*" %%a in ("%input%") do set "input1=%%a"&set "input2=%%b"&set "input3=%%c" |
| if /i "%input1%"=="cmd" cmd /c %input%&goto Start |
| if /i "%input1%"=="dir" cmd /c %input%&pause&goto Start |
| if /i "%input1%"=="find" cmd /c %input%&pause&goto Start |
| if /i "%input1%"=="findstr" cmd /c %input%&pause&goto Start |
| if /i "%input1%"=="B" start "" IEXPLORE.EXE "http://www.baidu.com/s?wd=%input2%+%input3%"&goto Start |
| if /i "%input%"=="1" call :Advanced&goto Start |
| if /i "%input%"=="2" ( |
| :find |
| cls&echo;&echo; |
| echo [1]列出所有文件名 |
| echo [2]导出所有文件名 |
| echo [3]建立目录树 |
| echo [4]建立文件树 |
| echo [5]导出目录树和文件树 |
| echo 搜索指定文件[^<FileName^>] |
| echo 定位指定文件[^<FilePath^>] |
| echo 返回[^<Enter^>] |
| echo; |
| set inputs= |
| set /p "inputs= 搜索文件:" |
| if "!inputs!"=="" goto Start |
| if /i "!inputs!"=="3" echo;&echo [目录树]&tree&echo;&pause&goto find |
| if /i "!inputs!"=="4" echo;&echo [文件树]&tree /f&echo;&pause&goto find |
| if /i "!inputs!"=="5" cd.>文件目录.txt&tree>>文件目录.txt&tree /f>>文件目录.txt&start "" 文件目录.txt&goto find |
| if /i "!inputs!"=="1" ( |
| :list |
| echo [文件列表] |
| dir /b /a /s|findstr /n .* |
| echo; |
| for /f "tokens=1 delims=:" %%a in ('dir /a /s ^|findstr /n "所列文件总数:"') do dir /a /s |more +%%a |
| call :FileSearch "dir /b /a /s" |
| ) |
| if /i "!inputs!"=="2" (for /f "tokens=1 delims=:" %%a in ('dir /a /s ^|findstr /n "所列文件总数:"') do dir /a /s |more +%%a>所有文件.txt |
| echo [所有文件]&dir /a /b /s>>所有文件.txt&start "" 所有文件.txt |
| ) |
| if exist "!inputs!" start explorer /select,"!inputs!"&goto find |
| echo; |
| dir /a /b /s "*!inputs!*"|findstr /n .* |
| echo; |
| call :FileSearch "dir /a /b /s "*!inputs!*"" |
| goto find |
| ) |
| set "F=findstr /spina:1c "%input%" "%~dp0\*.*"" |
| for /f "tokens=1,2*" %%a in ("%input%") do call :TextSearch "%input%" %%a %%b %%c |
| goto Start |
| :TextSearch |
| set "F=!F:\\=\! |
| title 搜索 %1 . . . |
| echo;&echo 正在搜索 %1 ,请稍候 . . .&echo 目录文件越多,需要的时间越长,请耐心等待 . . .&echo; |
| set num=0 |
| echo ^<meta http-equiv="Content-Type" content="text/html; charset=GBK" /^>^<title^>批处理文本检索 - 批处理之家^</title^>>查询结果.htm |
| echo ^<h1^> %1 的查询结果:^</h1^>^<small^>[快速查看]复制关键词,点击链接进入文本页面,按快捷键 Ctrl+F 调出查找对话框,粘贴关键词即可查找具体内容。建议使用 Internet Explorer 8 以上浏览器。^</small^>^<hr /^>>>查询结果.htm |
| for /f "tokens=1-3* delims=:" %%a in ('!F!') do ( |
| set " |
| set "#S=!#S:%~2=<span style="BACKGROUND-COLOR: CC0000">%2</span>!" |
| if "%~3" neq "" set "#S=!#S:%~3=<span style="BACKGROUND-COLOR: FFFF00">%3</span>!" |
| if "%~4" neq "" set "#S=!#S:%~4=<span style="BACKGROUND-COLOR: 33FF00">%4</span>!" |
| if "%~5" neq "" set "#S=!#S:%~5=<span style="BACKGROUND-COLOR: 1E90FF">%5</span>!" |
| if "%~6" neq "" set "#S=!#S:%~6=<span style="BACKGROUND-COLOR: 708090">%6</span>!" |
| if "%%~nxb" neq "查询结果.htm" (set /a num+=1 |
| echo ^<p^>^ ^ ^ ^ ^<strong^>!num!.^</strong^>! |
| ) |
| )>>查询结果.htm |
| echo ^<hr /^>^<p align="center"^>^<small^>^<a href="http://hi.baidu.com/cjiabing"^>甲兵时代 ^ |
| http://hi.baidu.com/cjiabing^</a^>^<br /^>^<a href="http://www.bathome.net"^>批处理之家 ^ |
| http://www.bathome.net^</a^>^<br /^>%date% %time%^</small^>^</p^>>>查询结果.htm |
| start 查询结果.htm |
| echo;&echo 搜索完成!请使用 Internet Explorer 8 以上浏览器浏览查询结果!&echo; |
| :TextSearchs |
| set inputs= |
| set /p "inputs= 返回[<Enter>]二次搜索[Words]:" |
| if "%inputs%"=="" goto :eof |
| echo;&echo;[查询结果]:&echo; |
| ::%F%|findstr /i "%inputs%"|findstr /ibvna:1c "查询结果.htm" |
| for /f "tokens=* delims=<>" %%a in ('findstr /i "%inputs%" 查询结果.htm^|findstr /ibnv "查询结果.htm"') do ( |
| set "var=%%a" |
| set "var=!var:<p> <strong>=!" |
| set "var=!var:</strong>=!" |
| set "var=!var:<span style=!" |
| set "var=!var:"BACKGROUND-COLOR: CC0000">=!" |
| set "var=!var:"BACKGROUND-COLOR: FFFF00">=!" |
| set "var=!var:"BACKGROUND-COLOR: 33FF00">=!" |
| set "var=!var:"BACKGROUND-COLOR: 1E90FF">=!" |
| set "var=!var:"BACKGROUND-COLOR: 708090">=!" |
| set "var=!var:</span>=!" |
| set "var=!var:<small><a href=!" |
| set "var=!var:</small></p>=!" |
| set "var=!var:</a>=!" |
| echo !var!&echo;&echo; |
| ) |
| echo;&goto TextSearchs |
| goto :eof |
| ::修正二次搜索,避免重复执行%F%。处理"查询结果.htm"是必须考虑的办法。 |
| 打开[Num] |
| if "%inputs%" neq " " (for /f "tokens=1,2 delims=:" %%a in (查询结果.htm) do ( |
| if "%%b"=="%inputs%" start "" "%~dp0%%~a"&echo;&echo; 文件已经打开!在IE或TXT中“查找”[Ctrl+F]在TXT中“转到”[Ctrl+G]&echo;&pause>nul&goto TextSearchs |
| ) |
| :Advanced |
| cls&echo;&echo; |
| echo; 【高级搜索】 |
| echo; ——————————————————————————————————————— |
| echo; 1.模糊搜索可以获得更多的信息,具体搜索可以获得更准确的信息。 |
| echo; 2.多个关键词使用空格分开,如“美女 吃饭 衣服”。 |
| echo; 3.使用“二次搜索”以显示同时包含多个关键词的搜索结果 |
| echo; 4.支持文件类型:*.txt *.htm (部分支持其他文档类型) |
| echo; 5.导出搜索结果:查询结果.htm |
| echo; 6.快捷键:在IE或TXT中“查找”[Ctrl+F]在TXT中“转到”[Ctrl+G] |
| echo; 7.跳过[^<Enter^>];返回[0] |
| echo; ——————————————————————————————————————— |
| echo 【搜索关键词】 |
| echo 1.关键词,如:小说 美女 爱情 |
| set input_words= |
| set input_wordc= |
| set input_wordv= |
| set input_types= |
| set F1= |
| set /p "input_words= 请输入:" |
| if "!input_words!"=="" set "input_words=.*" |
| if /i "!input_words!"=="0" goto Start |
| echo;&echo 2.排除的、不包含的关键词 |
| set /p "input_wordv= 请输入:" |
| if "!input_wordv!"=="" (set F1=) else (set "F1=^|findstr /i/v "!input_wordv!"") |
| if /i "!input_wordv!"=="0" goto Advanced |
| echo;&echo 3.将字符串作为一个关键词搜索[Y]任意搜索[Enter] |
| set /p "input_wordc= 请输入:" |
| if /i "!input_wordc!"=="0" goto Advanced |
| echo;&echo 【搜索文件类型】 |
| ::echo 可自定义,多个使用空格分开; |
| echo 如:[1]*.txt [2]*.htm [3]*.doc [4]*.vcf |
| set /p "input_types= 请输入:" |
| if "!input_types!"=="" set "input_types=*.*" |
| if "!input_types!"=="1" set "input_types=*.txt" |
| if "!input_types!"=="2" set "input_types=*.htm" |
| if "!input_types!"=="3" set "input_types=*.doc" |
| if "!input_types!"=="4" set "input_types=*.vcf" |
| if /i "!input_types!"=="0" goto Advanced |
| set input_paths= |
| set input_pathv= |
| echo;&echo 【搜索位置】 |
| echo 1.多个使用空格分开;含有空格的路径使用双引号引住。 |
| echo 如:E:\电子书 "C:\Program Files" |
| set /p "input_paths= 请输入:" |
| if "!input_paths!"=="" set "input_paths="%~dp0"" |
| if /i "!input_paths!"=="0" goto Advanced |
| echo;&echo 2.排除的、不包含的目录;排除当前目录[Y] |
| set /p "input_pathv= 请输入:" |
| if "!input_pathv!"=="" set "input_pathv=." |
| if /i "!input_pathv!"=="Y" set "input_pathv="%~dp0"" |
| if /i "!input_pathv!"=="0" goto Advanced |
| echo;&echo; |
| echo 你准备搜索关键词 "!input_words!" ,但排除 "!input_wordv!"; |
| echo 将字符串作为一个关键词搜索 "!input_wordc!" ,或任意搜索 [Enter] ; |
| echo 你准备搜索文件类型 "!input_types!",但排除 "!input_typev!"; |
| echo 你准备在位置 "!input_paths!" 进行搜索,但排除位置 "!input_pathv!"。 |
| echo;&echo; |
| for %%x in (!input_paths!) do for %%i in (!input_pathv!) do if /i "%%~x" neq "%%~i" ( |
| if /i "!input_wordc!"=="Y" ( |
| set "F=findstr /spina:1c /c:"!input_words!" "%%~x\!input_types!" !F1!" |
| for /f "tokens=1,2*" %%a in ("!input_words!") do call :TextSearch "!input_words!" %%a %%b %%c |
| ) else ( |
| set "F=findstr /spina:1c "!input_words!" "%%~x\!input_types!" !F1!" |
| for /f "tokens=1,2*" %%a in ("!input_words!") do call :TextSearch "!input_words!" %%a %%b %%c |
| ) |
| ) else ( |
| set "F=findstr /spina:1c "!input_words!" "%~dp0\*.*"" |
| for /f "tokens=1,2*" %%a in ("!input_words!") do call :TextSearch "!input_words!" %%a %%b %%c |
| ) |
| goto :eof |
| :FileSearch |
| echo; 打开[1 Num]定位[2 Num]搜索指定文件内容[Num Words] |
| echo; Num 指搜索结果前的数字,即行数。返回[^<Enter^>] |
| set input= |
| set /p "input= 请输入:" |
| if "!input!"=="" goto find |
| set/a input1=input2=. |
| for /f "tokens=1*" %%a in ("!input!") do set "input1=%%a"&set "input2=%%b" |
| for /f "tokens=1* delims=:" %%a in ('%~1^|findstr /n .*') do ( |
| if "!input1!"=="1" if !input2!==%%a echo;打开文件:%%b&start "" "%%~b"&pause&goto find |
| if "!input1!"=="2" if !input2!==%%a echo;定位文件:%%b&if exist "%%~b" start explorer /select,"%%~b"&pause&goto find |
| if !input1! gtr 2 if !input1!==%%a ( |
| set "F=findstr /spina:1c "!input2!" "%%~b"" |
| for /f "tokens=1,2*" %%i in ("!input2!") do call :TextSearch "!input2!" %%i %%j %%k |
| goto find |
| ) |
| ) |
| goto :eof |
| COPY |