批处理文件中的循环和变量

时间:2012-10-11 06:14:50

标签: for-loop dos batch-file

我想只读取批处理文件中的前两行,并将它们存储在两个不同的变量中。我用了这段代码:

@echo off 

for /f "delims=''" %%f in (red.txt) do (

echo file is: %%f

set /A Counter+=1
call :a %counter%
echo Counter After Increment: %Counter%
echo.
)
:a
if %Counter% equ 1 (set "file1=%f"
echo first file is: %file1%)

if %Counter% equ 2 (set "file2=%f"
echo second file is: %file2%)

if %Counter% equ 4 exit
echo Counter after for loop: %Counter%

pause

但变量'file1'和'file2'显示为空白。 有关如何去做的任何想法?

1 个答案:

答案 0 :(得分:1)

你的尝试有点矫枉过正,语法错误。

试试这个

<red.txt (
  set /p line1=
  set /p line2=
)
echo line1=%line1% 
echo line2=%line2%