在批处理中以迭代方式从文件读取输入时出现问题
Issue while reading Input from a file in iteration in Batch Processing
我在从输入文件读取下面显示的值 (FILE.txt) 时遇到问题。
< > 符号会导致不正确的读取。我厌倦了使用 ^(转义字符,但它没有用,我也尝试了双引号 (""),结果同样是混乱的输出。
我是否可以使用 < > = 字符迭代读取文件内容。
请帮助
FILE.txt contains
A;Select * from TablenameA where time=>yesterdaytime and time<endtime;XXX;YYY
B;Select * from TablenameB where time=>yesterdaytime and time<endtime;AAA;YYY
C;Select * from TablenameC where time=>yesterdaytime and time<endtime;BBB;YYY
--------------SCRIPT----------------------------------
SET vFILENAME=FILE.txt
for /F "tokens=1,2,3,4 delims=;" %%i in (%vFILENAME%) do call :process "%%i" "%%j" "%%k" "%%l"
goto END
:process
set VAR1=%~1
set VAR2=%~2
set VAR3=%~3
set VAR4=%~4
echo %VAR1%
echo %VAR2%
echo %VAR3%
echo %VAR4%
----------------------SCRIPT-END----------------------
更新:从 for 循环“%vFILENAME%”中删除引号。
问题更新:如果我想将输出重定向到另一个文件怎么办
示例:
echo PrmOne=%VAR1%>Output.txt
echo PrmTWO=%VAR2%>>Output.txt
echo PrmTHREE=%VAR3%>>Output.txt
echo PrmFOUR=%VAR4%>>Output.txt
问题是由于参数中的双引号引起的:
for /?
FOR /F .. ("string")
该行应该是
for /F "tokens=1,2,3,4 delims=;" %%i in (%vFILENAME%) do call :process "%%i" "%%j" "%%k" "%%l"
否则字符串"FILE.txt"被用作输入
更新,在注释后 < > 符号用于间接回显需要双引号
您的代码有几个错误(除了引号问题):for /F
命令需要 usebackq
选项,因为您将文件名括在引号中:
@echo off
SET vFILENAME=FILE.txt
for /F "usebackq tokens=1,2,3,4 delims=;" %%i in ("%vFILENAME%") do call :process "%%i" "%%j" "%%k" "%%l" < NUL
goto :EOF
:process
set "VAR1=%~1"
set "VAR2=%~2"
set "VAR3=%~3"
set "VAR4=%~4"
set /P "=%VAR1%" & echo/
set /P "=%VAR2%" & echo/
set /P "=%VAR3%" & echo/
set /P "=%VAR4%" & echo/
输出:
A
Select * from TablenameA where time=>yesterdaytime and time<endtime
XXX
YYY
B
Select * from TablenameB where time=>yesterdaytime and time<endtime
AAA
YYY
C
Select * from TablenameC where time=>yesterdaytime and time<endtime
BBB
YYY
我在从输入文件读取下面显示的值 (FILE.txt) 时遇到问题。 < > 符号会导致不正确的读取。我厌倦了使用 ^(转义字符,但它没有用,我也尝试了双引号 (""),结果同样是混乱的输出。
我是否可以使用 < > = 字符迭代读取文件内容。 请帮助
FILE.txt contains
A;Select * from TablenameA where time=>yesterdaytime and time<endtime;XXX;YYY
B;Select * from TablenameB where time=>yesterdaytime and time<endtime;AAA;YYY
C;Select * from TablenameC where time=>yesterdaytime and time<endtime;BBB;YYY
--------------SCRIPT----------------------------------
SET vFILENAME=FILE.txt
for /F "tokens=1,2,3,4 delims=;" %%i in (%vFILENAME%) do call :process "%%i" "%%j" "%%k" "%%l"
goto END
:process
set VAR1=%~1
set VAR2=%~2
set VAR3=%~3
set VAR4=%~4
echo %VAR1%
echo %VAR2%
echo %VAR3%
echo %VAR4%
----------------------SCRIPT-END----------------------
更新:从 for 循环“%vFILENAME%”中删除引号。
问题更新:如果我想将输出重定向到另一个文件怎么办
示例:
echo PrmOne=%VAR1%>Output.txt
echo PrmTWO=%VAR2%>>Output.txt
echo PrmTHREE=%VAR3%>>Output.txt
echo PrmFOUR=%VAR4%>>Output.txt
问题是由于参数中的双引号引起的:
for /?
FOR /F .. ("string")
该行应该是
for /F "tokens=1,2,3,4 delims=;" %%i in (%vFILENAME%) do call :process "%%i" "%%j" "%%k" "%%l"
否则字符串"FILE.txt"被用作输入
更新,在注释后 < > 符号用于间接回显需要双引号
您的代码有几个错误(除了引号问题):for /F
命令需要 usebackq
选项,因为您将文件名括在引号中:
@echo off
SET vFILENAME=FILE.txt
for /F "usebackq tokens=1,2,3,4 delims=;" %%i in ("%vFILENAME%") do call :process "%%i" "%%j" "%%k" "%%l" < NUL
goto :EOF
:process
set "VAR1=%~1"
set "VAR2=%~2"
set "VAR3=%~3"
set "VAR4=%~4"
set /P "=%VAR1%" & echo/
set /P "=%VAR2%" & echo/
set /P "=%VAR3%" & echo/
set /P "=%VAR4%" & echo/
输出:
A
Select * from TablenameA where time=>yesterdaytime and time<endtime
XXX
YYY
B
Select * from TablenameB where time=>yesterdaytime and time<endtime
AAA
YYY
C
Select * from TablenameC where time=>yesterdaytime and time<endtime
BBB
YYY