标题: [文件操作] 如何简化查找关键字的批处理代码? [打印本页]
作者: 帝尊 时间: 2022-11-2 15:21 标题: 如何简化查找关键字的批处理代码?
- FIND /I "UP1001" "TEMP\TEMP.TXT" %_NUL_% && SET "NAME=KILL" && SET "EXTERNAL=COCOL" && GOTO :INFO
- FIND /I "ZP1001" "TEMP\TEMP.TXT" %_NUL_% && SET "NAME=HELP" && SET "EXTERNAL=COCOL" && GOTO :INFO
- FIND /I "UP1201" "TEMP\TEMP.TXT" %_NUL_% && SET "NAME=CODE" && SET "EXTERNAL=COCOL" && GOTO :INFO
- FIND /I "UD1001" "TEMP\TEMP.TXT" %_NUL_% && SET "NAME=NICE" && SET "EXTERNAL=COCOL" && GOTO :INFO
- FIND /I "UP1901" "TEMP\TEMP.TXT" %_NUL_% && SET "NAME=CERA" && SET "EXTERNAL=COCOL" && GOTO :INFO
- FIND /I "U31001" "TEMP\TEMP.TXT" %_NUL_% && SET "NAME=GOOD" && SET "EXTERNAL=COCOL" && GOTO :INFO
- FIND /I "UP1001" "TEMP\TEMP.TXT" %_NUL_% && SET "NAME=PASS" && SET "EXTERNAL=COCOL" && GOTO :INFO
- FIND /I "UP1070" "TEMP\TEMP.TXT" %_NUL_% && SET "NAME=FAIL" && SET "EXTERNAL=COCOL" && GOTO :INFO
- FIND /I "RP1001" "TEMP\TEMP.TXT" %_NUL_% && SET "NAME=NONO" && SET "EXTERNAL=COCOL" && GOTO :INFO
- FIND /I "UX1001" "TEMP\TEMP.TXT" %_NUL_% && SET "NAME=YEEL" && SET "EXTERNAL=COCOL" && GOTO :INFO
- FIND /I "TP1001" "TEMP\TEMP.TXT" %_NUL_% && SET "NAME=KPCE" && SET "EXTERNAL=COCOL" && GOTO :INFO
- FIND /I "UP1301" "TEMP\TEMP.TXT" %_NUL_% && SET "NAME=YOBU" && SET "EXTERNAL=COCOL" && GOTO :INFO
- FIND /I "BB1001" "TEMP\TEMP.TXT" %_NUL_% && SET "NAME=FYOU" && SET "EXTERNAL=COCOL" && GOTO :INFO
复制代码
像这样的代码,可以简化吗?
作者: hlzj88 时间: 2022-11-2 19:42
- for /f "tokens=1,2 delims= " %%i in (test.txt) do FIND /I "%%i" "TEMP\TEMP.TXT" %_NUL_% && SET "NAME=%%j" && SET "EXTERNAL=COCOL" && GOTO :INFO
复制代码
test.txt的内容如下
UP1001 KILL
ZP1001 HELP
UP1201 CODE
UD1001 NICE
UP1901 CERA
作者: Batcher 时间: 2022-11-2 21:13
回复 1# 帝尊 - @echo off
- for /f "delims=:" %%i in ('findstr /n "^:DynamicScript$" "%~f0"') do (
- for /f "tokens=1-2" %%a in ('more +%%i "%~f0"') do (
- FIND /I "%%a" "TEMP\TEMP.TXT" >nul 2>&1 && SET "NAME=KILL" && SET "EXTERNAL=COCOL" && GOTO :INFO
- )
- )
- goto :End
-
- :INFO
- echo Hello World
- goto :End
-
- :End
- pause
- goto :eof
-
- :DynamicScript
- UP1001 KILL
- ZP1001 HELP
- UP1201 CODE
- UD1001 NICE
- UP1901 CERA
- U31001 GOOD
- UP1001 PASS
- UP1070 FAIL
- RP1001 NONO
- UX1001 YEEL
- TP1001 KPCE
- UP1301 YOBU
- BB1001 FYOU
复制代码
作者: 帝尊 时间: 2022-11-3 08:24
回复 3# Batcher
回复 2# hlzj88
非常感谢,不愧是大佬。
作者: 帝尊 时间: 2022-11-3 09:07
本帖最后由 帝尊 于 2022-11-3 09:42 编辑
再次感谢。。。。
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |