Board logo

标题: [文本处理] [已解决]批处理开启变量延迟后重定向到文本的问题 [打印本页]

作者: freesoft00    时间: 2016-2-27 15:20     标题: [已解决]批处理开启变量延迟后重定向到文本的问题

  1. @echo off
  2. setlocal enableDelayedExpansion
  3. for /f "tokens=1*" %%i in ('dir ^| findstr /i "好的"') do (
  4.     if "%%i"=="200" set str=%%j
  5.     del !str!
  6. )
复制代码
原代码部分如上。现在想将for部分重定向到新文本中,如下:
  1. @echo off
  2. setlocal enableDelayedExpansion
  3. echo for /f "tokens=1*" %%i in ('dir ^| findstr /i "好的"') do ( >33.txt
  4. echo     if "%%i"=="200" set str=%%j >>33.txt
  5. echo    del !str! >>33.txt
  6. echo ) >>33.txt
复制代码
这个代码肯定不正确。修改了一下如下:
  1. @echo off
  2. setlocal enableDelayedExpansion
  3. echo for /f "tokens=1*" %%%%i in ('dir ^^^| findstr /i "好的"') do ( >33.txt
  4. echo    if "%%%%i"=="200" set str=%%%%j >>33.txt
  5. echo    del !str! >>33.txt
  6. 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 编辑
既然不想命令行解释!str! 去掉

因为其他部分的代码还需要这个变量延迟。这个有方法解决吗.比如如:
  1. @echo off
  2. setlocal enableDelayedExpansion
  3. if exist www (
  4. set ipt=c:\33.txt
  5. echo for /f "tokens=1*" %%%%i in ('dir ^^^| findstr /i "好的"') do ( >!ipt!
  6. echo    if "%%%%i"=="200" set str=%%%%j >>!ipt!
  7. echo    del !str! >>!ipt!
  8. echo ^) >>!ipt!
  9. )
复制代码

作者: bailong360    时间: 2016-2-27 18:21

  1. @echo off
  2. setlocal enableDelayedExpansion
  3. echo for /f "tokens=1*" %%%%i in ('dir ^^^| findstr /i "好的"') do ( >33.txt
  4. echo    if "%%%%i"=="200" set str=%%%%j >>33.txt
  5. echo    del ^^!str^^! >>33.txt
  6. echo ^) >>33.txt
复制代码





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