[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文件操作] 如何简化查找关键字的批处理代码?

  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
复制代码
像这样的代码,可以简化吗?

  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
目的,学习批处理

TOP

回复 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
复制代码
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

回复 3# Batcher
回复 2# hlzj88
非常感谢,不愧是大佬。

TOP

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

再次感谢。。。。

TOP

返回列表