我有一个文本文件(list.txt),它是一个列表文件名,可以简化如下:
一个
b
ç
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
在循环中处理多行数据时,批处理文件似乎有困难 有人可以对此有所了解吗?提前谢谢。
答案 0 :(得分:1)
@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
更改文件的路径。
答案 1 :(得分:1)
@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
也可以与delayedexpansion
一起使用。请注意使用set /a total
- 运行时,而不是解析时值应用于计算。