批处理文件-将两行读取到一个for循环中的两个独立变量中



我有一个文本文件(list.txt),它是一个列表文件名,可以简化如下:
a
b
c
d

我需要一个"for"循环,它的作用如下:

set variable1=a  
set variable2=b
do something with variable1 & 2, lets say variable1 + variable2>>output.txt
then restart the loop from the second line:
set variable1=b
set variable2=c
perform the addition>>output.txt
then from the third line:
set variable1=c
set variable2=d etc..
and keep going until the end of my list.txt

当处理循环中的多行数据时,批处理文件似乎有困难
有人能解释一下吗?提前谢谢。

@ECHO OFF
SETLOCAL
SET "var1="
SET "var2="
FOR /f %%a IN (q26028954.txt) DO (
 CALL SET "var1=%%var2%%"
 SET "var2=%%a"
 IF DEFINED var1 (
  SET /a total=var1+var2
  CALL ECHO %%var1%% + %%var2%% = %%total%%
  )
)
GOTO :EOF

其中q26028954.txt包含

1
4
7
11

产生

1 + 4 = 5
4 + 7 = 11
7 + 11 = 18

也可以与CCD_ 2一起使用。请注意set /a total的使用-运行时,而不是解析时间值应用于计算。

@echo off
setlocal disableDelayedExpansion
set "file_to_process=c:some.txt"
set "line="
for /f "usebackq tokens=* delims=" %%# in ("%file_to_process%") do (
    setlocal enableDelayedExpansion
    if "!line!" NEQ "" (
        echo do something with !line! and %%#
    )
    endlocal & set "line=%%#"
)
endlocal

更改文件的路径。

最新更新