我需要使用关闭回显的批处理文件将十行回显到文本文档。文件已保存,但每一行都显示 ECHO 已关闭。我的批处理文件是-

I need to echo ten lines to a text document using a batch file with echo off. The file gets saved, but each line says ECHO is off. my batch file is-

@echo off  
color a    
:ini  
set /p  1= %random%    
set /p  2= %random%   
set /p  3=  %random%  
set /p  4=  %random%  
set /p  5=  %random%  
set /p  6=  %random%  
set /p  7=  %random%  
set /p  8=  %random%  
set /p  9=  %random%  
set /p  10=  %random%

设置随机数

(  

echo %1%  
echo %2%  
echo %3%  
echo %4%  
echo %5%  
echo %6%  
echo %7%  
echo %8%  
echo %9%  
echo %10%  
)>file.txt

试图将它们回显到文件中 我的并发症是回声已关闭。 我需要它关闭。

变量不应仅以数字命名。根据 dbenham 的以下评论:

%1% is interpreted as %1 (the first script argument) which happens to be undefined in this case, followed by a lone % which is simply consumed. Generally, you should not define a variable with a name beginning with a number. But you can access such variables if you use delayed expansion - %1% does not work, but !1! does work.

因此,在您的情况下,解析器遇到无效变量(例如 %1%),因此它仅处理 ECHO 命令,如果 ECHO 打开或关闭,该命令只输出。

除此之外,您的脚本似乎有点不正常,因为您试图使用随机数作为提示文本来提示输入 10 个值。

如果你只需要输出 10 个随机数,你可以 "gracefully" 使用 FOR 循环:

@ECHO OFF
SETLOCAL EnableDelayedExpansion

SET FileName=file.txt
SET NumberOfValues=10

REM Remove existing file if it exists.
IF EXIST "%FileName%" DEL "%FileName%"

FOR /L %%A IN (1,1,%NumberOfValues%) DO ECHO !RANDOM!>>file.txt

ENDLOCAL

或者,一种更简洁的方法是您可以 运行 直接在 Windows 命令提示符下(不需要批处理文件):

CMD /V:ON /C FOR /L %A IN (1,1,10) DO @ECHO !RANDOM!>file.txt