我不太习惯制作批处理文件,所以可能做错了什么,但我真的在echo off/on方面遇到了很大的问题。(话虽如此,如果您发现我漏掉了其他错误,请随时指出)
这是我代码的简化版本:
@echo off
setlocal
set args=
set dir="."
:getargs
IF "%2"=="" (
set dir="%1"
goto callbatch
)
set args=%args% %1
shift
goto getargs
:callbatch
for %%f in (%dir%\*.txt) do (
echo processing %%f
"%BATCHHOME%\batch.bat %args% %%f
echo
)
基本上,我有一个在BATCHHOME中的批处理文件,它对单个txt文件执行某些操作,我想生成一个批处理文件,以处理给定目录中的所有txt文件。循环中的孤独回声是为了更好地说明问题。
现在,这就是我的问题所在。其输出结果看起来像:
processing foo\text1.txt
some output from batch.bat
ECHO is on.
C:\somedir>(
echo processing foo\text2.txt
"C:\the\path\of\batch.bat" the arguments here foo\text2.txt
)
ECHO is on.
C:\somedir>(
echo processing foo\text3.txt
"C:\the\path\of\batch.bat" the arguments here foo\text3.txt
)
ECHO is on.
(etc)
好的...我不知道为什么我调用的批处理文件会打开echo,但我并没有制作它,所以我每次只需将其关闭!我将最后一部分修改为:
:callbatch
for %%f in (%dir%\*.txt) do (
echo processing %%f
"%BATCHHOME%\batch.bat %args% %%f
echo off
)
echo
echo on
echo
echo Finished!
(再次强调,这两个“echo”是为了调试而存在的)所以现在输出看起来像这样:
processing foo\text1.txt
some output from batch.bat
processing foo\text2.txt
some output from batch.bat
processing foo\text3.txt
some output from batch.bat
(etc.)
ECHO is off.
ECHO is on.
Finished!
这几乎是完美的,但有些奇怪。如果echo打开了,为什么会显示“Finished!”而不是“echo Finished!”?然而更大(但相关)的问题是,当批处理文件完成时,我不再显示路径。换句话说,我不再拥有:
C:\Somedir>_
我只是得到了
_
我需要手动输入“echo on”才能再次显示路径。我做错了什么?
编辑:为了澄清,我知道单独使用“echo”会打印echo的当前状态。那正是我的意图。在第一段代码中,它们是用来显示在调用其他批处理文件后echo神秘地开启的。在第二个代码中,它是用来显示在批处理文件结束时echo处于开启状态,但行为却好像没有这样。这就是我的问题所在。
echo.
单独的echo
命令会显示当前的回显设置。 - wmz