标题: [文本处理] [已解决]把行首为指定字符串的行替换成其他内容的批处理执行很慢怎么优化? [打印本页]
作者: gutman1989 时间: 2016-11-11 16:11 标题: [已解决]把行首为指定字符串的行替换成其他内容的批处理执行很慢怎么优化?
本帖最后由 gutman1989 于 2016-11-26 10:29 编辑
查找替换功能很慢怎么优化- @echo off
- copy *.scr root.scr
- pause
- set cf=root.scr
- set dt=ucs new 3000,0
- setlocal enabledelayedexpansion
- pushd %1
- for /f "tokens=1,* delims=:" %%i in ('findstr /n ".*" %cf%') do (
- set txt=%%j
- if "!txt!" == "" (
- echo.>>%1root.tmp
- ) else if "!txt:~0,4!"=="SAVE" (
- set txt=%dt%
- echo !txt!>>%1root.tmp
- ) else (
- echo !txt!>>%1root.tmp
- )
- )
- 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
看就是凑的代码,还没走好就开始跑,也就只能借外力了,优化在于细节,基础是很重要- @echo off&setlocal enabledelayedexpansion
- set "cf=root.scr"
- set "dt=ucs new 3000,0"
- copy /b *.scr "%cf%"
- (for /f "tokens=1,* delims=:" %%i in ('findstr /n .* "%cf%"') do (
- set "str=%%j"
- if "!str:~,4!" equ "SAVE" (echo;%dt%) else echo;%%j
- ))>root.tmp
- move /y root.tmp "%cf%"
- pause
复制代码
作者: Batcher 时间: 2016-11-11 17:23
- @echo off
- copy *.scr root.scr
- pause
- set cf=root.scr
- set dt=ucs new 3000,0
- setlocal enabledelayedexpansion
- pushd %1
- (for /f "tokens=1,* delims=:" %%i in ('findstr /n ".*" %cf%') do (
- set txt=%%j
- if "!txt!" == "" (
- echo.
- ) else if "!txt:~0,4!"=="SAVE" (
- set txt=%dt%
- echo !txt!
- ) else (
- echo !txt!
- )
- ))>%1root.tmp
- 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 |