Board logo

标题: [数值计算] 【已解决】批处理命令for /l里面的变量为什么一直是0? [打印本页]

作者: chouxia    时间: 2014-10-8 10:41     标题: 【已解决】批处理命令for /l里面的变量为什么一直是0?

本帖最后由 chouxia 于 2014-10-28 08:41 编辑
  1. set e=0
  2. for /l %%i in (1,1,%i%) do (
  3. set /a e=%e%+1
  4. echo %e%
  5. echo echo off > \1avi\%j%\%e%.bat
  6. echo this is a test >> \1avi\%j%\%e%.bat
  7. start \1avi\%j%\%e%.bat
  8. )
复制代码
我想请教下  在这个情况下为什么set /a e=%e%+1这条指令没有执行。变量e一直是0?
作者: chouxia    时间: 2014-10-8 10:49

还有个问题,我如何将批处理的输出重定向到文本的一行中?
也就是说将 echo this is a test >> \1avi\%j%\%e%.bat      把这行内容输入到文件文件的第一行末尾。
作者: Batcher    时间: 2014-10-8 11:25

开启变量延迟扩展
  1. setlocal enabledelayedexpansion
  2. set e=0
  3. for /l %%i in (1,1,%i%) do (
  4.     set /a e+=1
  5.     echo !e!
  6. )
复制代码

作者: Batcher    时间: 2014-10-8 11:28

回复 2# chouxia


set /p =this is a test <nul >>a.bat
作者: chouxia    时间: 2014-10-8 11:46

谢谢 领导  辛苦了   精神上请你吃大餐  肉体上你自己搞定吧  
作者: chouxia    时间: 2014-10-8 12:09

setlocal enabledelayedexpansion
set e=0
for /l %%i in (1,1,%i%) do (
        set /a e+=1
        echo !e!
        echo echo off > \1avi\%j%\!e!.bat
        echo this is a test >> \1avi\%j%\!e!.bat
        start \1avi\%j%\!e!.bat
)
不管是set /a e+=1 还是set /a e=!e!+1都不生效
还有 set /p =this is a test <nul >>a.bat这行为什么这么写呢?
另外,批量合并文件 我想到用sed来完成 用echo this is a test >a.bat
然后用sed做同行编辑。最后用start启动批处理。
不知道这个方法可行不?
有什么需要注意的地方?
作者: terse    时间: 2014-10-8 12:47

回复 6# chouxia
这样不一样吗
  1. setlocal enabledelayedexpansion
  2. for /l %%i in (1,1,%i%) do (
  3.         echo echo off > \1avi\%j%\%%i.bat
  4.         echo this is a test >> \1avi\%j%\%%i.bat
  5.         start \1avi\%j%\%%i.bat
  6. )
  7.    
复制代码

作者: chouxia    时间: 2014-10-8 12:51

对啊 楼上的  我这犯二了
作者: Batcher    时间: 2014-10-8 13:32

回复 6# chouxia


echo 命令会在行尾添加回车换行
set /p 命令可以避免这个问题
作者: chouxia    时间: 2014-10-8 14:17

回复 9# Batcher


    那是不是说我用循环可以在这一行一直输入内容?
这样我就可以使用合并功能了吧!
作者: Batcher    时间: 2014-10-8 18:10

回复 10# chouxia


    亲自试试就知道啦




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