我正在尝试使用for-loop来迭代值,但是没有得到预期的结果。基于实际的输出,我认为这里不需要嵌套循环,但是我不确定如何在没有循环的情况下实现预期的输出。
@Echo Off
SETLOCAL ENABLEDELAYEDEXPANSION
Set "Ids=1,2"
Set "basePath=C:\Users\Documents\ReusableQueries\"
Set "fileName=test1,test2"
Set "extension=.csv"
echo ****Configuation Details****
Echo basePath - %basePath%
Echo fileName - %fileName%
Echo extension - %extension%
echo.
For %%i in (%Ids%) do (
For %%f in (%fileName%) do (
echo Id - %%i and file name - %%f
Set "completepath=%basePath%%%f%extension%"
echo completepath - !completepath!
FOR /F "tokens=*" %%a IN (!completepath!) do (
echo Result of api command1 call for Id : %%i and fileName: %%f
echo.
)
)
)
Pause
当前输出:
****Configuation Details****
basePath - C:\Users\Documents\ReusableQueries\
fileName - test1,test2
extension - .csv
Id - 1 and file name - test1
completepath - C:\Users\Documents\ReusableQueries\test1.csv
Result of api command1 call for Id : 1 and fileName: test1
Id - 1 and file name - test2
completepath - C:\Users\Documents\ReusableQueries\test2.csv
Result of api command1 call for Id : 1 and fileName: test2
Id - 2 and file name - test1
completepath - C:\Users\Documents\ReusableQueries\test1.csv
Result of api command1 call for Id : 2 and fileName: test1
Id - 2 and file name - test2
completepath - C:\Users\Documents\ReusableQueries\test2.csv
Result of api command1 call for Id : 2 and fileName: test2
预期输出:
****Configuation Details****
basePath - C:\Users\Documents\ReusableQueries\
fileName - test1,test2
extension - .csv
Id - 1 and file name - test1
completepath - C:\Users\Documents\ReusableQueries\test1.csv
Result of api command1 call for Id : 1 and fileName: test1
Id - 2 and file name - test2
completepath - C:\Users\Documents\ReusableQueries\test2.csv
Result of api command1 call for Id : 2 and fileName: test2
答案 0 :(得分:2)
您似乎误解了嵌套(for
)循环的概念:它们不是并行循环,而是内部循环在外部循环的每次迭代中运行。
鉴于Id
始终是数字且连续的,因此要获得所需的内容,请删除for %%i
循环,在set /A "Id=0"
循环之前执行for %%f
,然后执行{{ 1}}作为循环主体中的第一个命令,并使用set /A "Id+=1"
代替!Id!
,如下所示:
%%i
答案 1 :(得分:1)
由于您使用的是连续的数字Id
,因此aschipfl's answer最适合您的需求。
这里是一种更通用的方法,可让您了解Id
不是数字或随机的情况。
set NameIdPairs="test1,1", "test2,2", "Filename with spaces,0xfca"
set "basePath=C:\Users\Documents\ReusableQueries\"
set "extension=.csv"
for %%P in (%NameIdPairs%) do (
echo Current Pair: %%P
for /F "tokens=1,2 delims=," %%F in (%%P) do (
echo Id - %%G and file name - %%F
set "completepath=%basePath%%%F%extension%"
echo completepath - !completepath!
)
)
每对filename
和id
的格式为"filename,id"
逗号(,
)周围不能有任何空格将它们分开,否则它们将成为filename
和或id
这对货币对本身之间用逗号(,
)和/或空格隔开。
外部循环在每次迭代中选择一对,然后内部循环标记该对以提取filename
和id