从输入文件中读取下面显示的值(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
some processing on VAR1,2,3,4 and then
echo PrmOne=%VAR1%>Output_%VAR1%.txt
echo PrmTWO=%VAR2%>>Output_%VAR1%.txt
echo PrmTHREE=%VAR3%>>Output_%VAR1%.txt
echo PrmFOUR=%VAR4%>>Output_%VAR1%.txt
----- OutPut(每次迭代都是新的)文件将为每次迭代包含这样的值----------------
PrmOne=A
PrmTWO=Select * from TablenameA where time=>yesterdaytime_UPDATED and time<endtime_UPDATED
PrmTHREE=XXX
PrmFOUR=YYY
为此提供了解决方案但需要以上述方式输出请帮忙 感谢@Aacini
@echo off
SET vFILENAME=FILE.txt
for /F "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/
答案 0 :(得分:2)
您可以使用延迟扩展来避免可变内容输出问题
>"Output_%VAR1%.txt" (
setlocal enabledelayedexpansion
echo PrmOne=!VAR1!
echo PrmTWO=!VAR2!
echo PrmTHREE=!VAR3!
echo PrmFOUR=!VAR4!
endlocal
)
答案 1 :(得分:1)
Aacini原始代码略有不同。没必要调用函数。如果您需要调用该功能,请相应调整。
@echo off
SET vFILENAME=FILE.txt
for /F "usebackq tokens=1,2,3,4 delims=;" %%i in ("%vFILENAME%") do (
>>%%~i.txt (
set /P "=PrmOne=%%~i" & echo/
set /P "=PrmTwo=%%~j" & echo/
set /P "=PrmThree=%%~k" & echo/
set /P "=PrmFour=%%~l" & echo/
)<nul
)