Board logo

标题: [文本处理] [已解决]把行首为指定字符串的行替换成其他内容的批处理执行很慢怎么优化? [打印本页]

作者: gutman1989    时间: 2016-11-11 16:11     标题: [已解决]把行首为指定字符串的行替换成其他内容的批处理执行很慢怎么优化?

本帖最后由 gutman1989 于 2016-11-26 10:29 编辑

查找替换功能很慢怎么优化
  1. @echo off
  2. copy *.scr root.scr
  3. pause
  4. set cf=root.scr
  5. set dt=ucs new 3000,0
  6. setlocal enabledelayedexpansion
  7. pushd %1
  8. for /f "tokens=1,* delims=:" %%i in ('findstr /n ".*" %cf%') do (
  9. set txt=%%j
  10. if "!txt!" == "" (
  11. echo.>>%1root.tmp
  12. )  else if "!txt:~0,4!"=="SAVE" (
  13. set txt=%dt%
  14. echo !txt!>>%1root.tmp
  15. ) else (
  16. echo !txt!>>%1root.tmp
  17. )
  18. )
  19. move /y root.tmp root.scr
复制代码
这个是查找行首是save的行,然后用dt字符串替换的代码,执行很慢。还不如用记事本打开scr,找到那行复制粘贴快。求高手给点建议,初学bat
作者: pcl_test    时间: 2016-11-11 16:20

for
http://www.bathome.net/thread-2189-1-1.html
http://bbs.bathome.net/thread-31727-1-1.html
http://www.bathome.net/thread-5814-1-1.html
http://www.bathome.net/thread-75-1-1.html
作者: gutman1989    时间: 2016-11-11 16:31

回复 2# pcl_test


    我只是想知道怎么优化而已
作者: pcl_test    时间: 2016-11-11 17:10

回复 3# gutman1989

看就是凑的代码,还没走好就开始跑,也就只能借外力了,优化在于细节,基础是很重要
  1. @echo off&setlocal enabledelayedexpansion
  2. set "cf=root.scr"
  3. set "dt=ucs new 3000,0"
  4. copy /b *.scr "%cf%"
  5. (for /f "tokens=1,* delims=:" %%i in ('findstr /n .* "%cf%"') do (
  6.     set "str=%%j"
  7.     if "!str:~,4!" equ "SAVE" (echo;%dt%) else echo;%%j
  8. ))>root.tmp
  9. move /y root.tmp "%cf%"
  10. pause
复制代码

作者: Batcher    时间: 2016-11-11 17:23

  1. @echo off
  2. copy *.scr root.scr
  3. pause
  4. set cf=root.scr
  5. set dt=ucs new 3000,0
  6. setlocal enabledelayedexpansion
  7. pushd %1
  8. (for /f "tokens=1,* delims=:" %%i in ('findstr /n ".*" %cf%') do (
  9.     set txt=%%j
  10.     if "!txt!" == "" (
  11.         echo.
  12.     )  else if "!txt:~0,4!"=="SAVE" (
  13.         set txt=%dt%
  14.         echo !txt!
  15.     ) else (
  16.         echo !txt!
  17.     )
  18. ))>%1root.tmp
  19. move /y root.tmp root.scr
复制代码
把重定向放到 for 循环外面可以提高效率,参考:
http://mp.weixin.qq.com/s?__biz= ... e=4#wechat_redirect
作者: gutman1989    时间: 2016-11-26 10:28

回复 4# pcl_test


    谢谢版主和管理员,前段时间着急用。最近一直在学基础,




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