Board logo

标题: [文件操作] 如何简化查找关键字的批处理代码? [打印本页]

作者: 帝尊    时间: 2022-11-2 15:21     标题: 如何简化查找关键字的批处理代码?

  1. FIND /I "UP1001" "TEMP\TEMP.TXT" %_NUL_% && SET "NAME=KILL" && SET "EXTERNAL=COCOL" && GOTO :INFO
  2. FIND /I "ZP1001" "TEMP\TEMP.TXT" %_NUL_% && SET "NAME=HELP" && SET "EXTERNAL=COCOL" && GOTO :INFO
  3. FIND /I "UP1201" "TEMP\TEMP.TXT" %_NUL_% && SET "NAME=CODE" && SET "EXTERNAL=COCOL" && GOTO :INFO
  4. FIND /I "UD1001" "TEMP\TEMP.TXT" %_NUL_% && SET "NAME=NICE" && SET "EXTERNAL=COCOL" && GOTO :INFO
  5. FIND /I "UP1901" "TEMP\TEMP.TXT" %_NUL_% && SET "NAME=CERA" && SET "EXTERNAL=COCOL" && GOTO :INFO
  6. FIND /I "U31001" "TEMP\TEMP.TXT" %_NUL_% && SET "NAME=GOOD" && SET "EXTERNAL=COCOL" && GOTO :INFO
  7. FIND /I "UP1001" "TEMP\TEMP.TXT" %_NUL_% && SET "NAME=PASS" && SET "EXTERNAL=COCOL" && GOTO :INFO
  8. FIND /I "UP1070" "TEMP\TEMP.TXT" %_NUL_% && SET "NAME=FAIL" && SET "EXTERNAL=COCOL" && GOTO :INFO
  9. FIND /I "RP1001" "TEMP\TEMP.TXT" %_NUL_% && SET "NAME=NONO" && SET "EXTERNAL=COCOL" && GOTO :INFO
  10. FIND /I "UX1001" "TEMP\TEMP.TXT" %_NUL_% && SET "NAME=YEEL" && SET "EXTERNAL=COCOL" && GOTO :INFO
  11. FIND /I "TP1001" "TEMP\TEMP.TXT" %_NUL_% && SET "NAME=KPCE" && SET "EXTERNAL=COCOL" && GOTO :INFO
  12. FIND /I "UP1301" "TEMP\TEMP.TXT" %_NUL_% && SET "NAME=YOBU" && SET "EXTERNAL=COCOL" && GOTO :INFO
  13. FIND /I "BB1001" "TEMP\TEMP.TXT" %_NUL_% && SET "NAME=FYOU" && SET "EXTERNAL=COCOL" && GOTO :INFO
复制代码
像这样的代码,可以简化吗?
作者: hlzj88    时间: 2022-11-2 19:42

  1. 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# 帝尊
  1. @echo off
  2. for /f "delims=:" %%i in ('findstr /n "^:DynamicScript$" "%~f0"') do (
  3.     for /f "tokens=1-2" %%a in ('more +%%i "%~f0"') do (
  4.         FIND /I "%%a" "TEMP\TEMP.TXT" >nul 2>&1 && SET "NAME=KILL" && SET "EXTERNAL=COCOL" && GOTO :INFO
  5.     )
  6. )
  7. goto :End
  8. :INFO
  9. echo Hello World
  10. goto :End
  11. :End
  12. pause
  13. goto :eof
  14. :DynamicScript
  15. UP1001 KILL
  16. ZP1001 HELP
  17. UP1201 CODE
  18. UD1001 NICE
  19. UP1901 CERA
  20. U31001 GOOD
  21. UP1001 PASS
  22. UP1070 FAIL
  23. RP1001 NONO
  24. UX1001 YEEL
  25. TP1001 KPCE
  26. UP1301 YOBU
  27. BB1001 FYOU
复制代码

作者: 帝尊    时间: 2022-11-3 08:24

回复 3# Batcher
回复 2# hlzj88
非常感谢,不愧是大佬。
作者: 帝尊    时间: 2022-11-3 09:07

本帖最后由 帝尊 于 2022-11-3 09:42 编辑

再次感谢。。。。




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2