标题: [文本处理] [已解决]批处理开启变量延迟后重定向到文本的问题 [打印本页]
作者: freesoft00 时间: 2016-2-27 15:20 标题: [已解决]批处理开启变量延迟后重定向到文本的问题
- @echo off
- setlocal enableDelayedExpansion
- for /f "tokens=1*" %%i in ('dir ^| findstr /i "好的"') do (
- if "%%i"=="200" set str=%%j
- del !str!
- )
复制代码
原代码部分如上。现在想将for部分重定向到新文本中,如下:- @echo off
- setlocal enableDelayedExpansion
- echo for /f "tokens=1*" %%i in ('dir ^| findstr /i "好的"') do ( >33.txt
- echo if "%%i"=="200" set str=%%j >>33.txt
- echo del !str! >>33.txt
- echo ) >>33.txt
复制代码
这个代码肯定不正确。修改了一下如下:- @echo off
- setlocal enableDelayedExpansion
- echo for /f "tokens=1*" %%%%i in ('dir ^^^| findstr /i "好的"') do ( >33.txt
- echo if "%%%%i"=="200" set str=%%%%j >>33.txt
- echo del !str! >>33.txt
- echo ^) >>33.txt
复制代码
但是因为开启了变量延迟,!str!不会被重定向到33.txt中
应该如何解决呢?或者说代码中还有哪里是错的。
作者: ivor 时间: 2016-2-27 15:43
回复 1# freesoft00
既然不想命令行解释!str! 去掉setlocal enableDelayedExpansion
作者: freesoft00 时间: 2016-2-27 16:26
本帖最后由 freesoft00 于 2016-2-27 16:50 编辑
因为其他部分的代码还需要这个变量延迟。这个有方法解决吗.比如如:- @echo off
- setlocal enableDelayedExpansion
- if exist www (
- set ipt=c:\33.txt
- echo for /f "tokens=1*" %%%%i in ('dir ^^^| findstr /i "好的"') do ( >!ipt!
- echo if "%%%%i"=="200" set str=%%%%j >>!ipt!
- echo del !str! >>!ipt!
- echo ^) >>!ipt!
- )
复制代码
作者: bailong360 时间: 2016-2-27 18:21
- @echo off
- setlocal enableDelayedExpansion
- echo for /f "tokens=1*" %%%%i in ('dir ^^^| findstr /i "好的"') do ( >33.txt
- echo if "%%%%i"=="200" set str=%%%%j >>33.txt
- echo del ^^!str^^! >>33.txt
- echo ^) >>33.txt
复制代码
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |