I have a loop which is going line by line through a text file and is extracting the value which i need for 2 purposes.
use it as a query parameter for calling an api
use it as name for a text file i create to store the api response. As i cant put '/' in the name of the file, i change it to '_'
The text file could look like this:
1/2/3
4/5/6
...
My whole script currently looks like this:
@echo off
setlocal enabledelayedexpansion
for /F %%a in (text.csv) do (
echo %%a
SET varA=%%a
SET varB=%%a
:deaster
FOR /f "tokens=1* delims=/" %%i IN ("!varA!") DO (
SET varA=%%j
IF DEFINED varA (
SET varA=%%i_%%j
GOTO deaster
) ELSE (
SET varA=%%i
)
)
curl -o ./myFolder/!varA!.txt -v -X GET https://myWebRoute/?q=!varB! -H "Authorization: Bearer ..."
echo %%a
timeout /t 1
)
My current problem is, that the outer loop stops after the first iteration and the last echo just replies '%a'
When i test this wiht providing a varA with a fixed value and not feeding it %%a.
I tested around with a lot of different variations, but as soon as i use %%a for assigning it to varA, it fails.
Maybe someone can help, i would appreciate it.
Uwe
I have a loop which is going line by line through a text file and is extracting the value which i need for 2 purposes.
use it as a query parameter for calling an api
use it as name for a text file i create to store the api response. As i cant put '/' in the name of the file, i change it to '_'
The text file could look like this:
1/2/3
4/5/6
...
My whole script currently looks like this:
@echo off
setlocal enabledelayedexpansion
for /F %%a in (text.csv) do (
echo %%a
SET varA=%%a
SET varB=%%a
:deaster
FOR /f "tokens=1* delims=/" %%i IN ("!varA!") DO (
SET varA=%%j
IF DEFINED varA (
SET varA=%%i_%%j
GOTO deaster
) ELSE (
SET varA=%%i
)
)
curl -o ./myFolder/!varA!.txt -v -X GET https://myWebRoute/?q=!varB! -H "Authorization: Bearer ..."
echo %%a
timeout /t 1
)
My current problem is, that the outer loop stops after the first iteration and the last echo just replies '%a'
When i test this wiht providing a varA with a fixed value and not feeding it %%a.
I tested around with a lot of different variations, but as soon as i use %%a for assigning it to varA, it fails.
Maybe someone can help, i would appreciate it.
Uwe
batch-file
Share
Improve this question
edited Mar 31 at 15:29
Compo
38.8k55 gold badges3131 silver badges4545 bronze badges
asked Mar 31 at 14:14
Uwe KönigUwe König4666 bronze badges4
2you can't jump within a loop or code block. a) every goto breaks the loop-context, b) labels behave odd within loops and code blocks ("undefined behaviour" - it may skip lines or show other unintended "functionality"). Iron Rule: no labels in code blocks, goto only to intentionally leave (and break) the loop or code block (jump out of the block, not within (and of course not into))
– Stephan
CommentedMar 31 at 14:21
Tip: try for /f "delims=" %%a in (text.csv) do (, set "var=%%a", echo !var:/=_!, )
– Stephan
CommentedMar 31 at 14:25
i had tested !var:/=_! already but failed. Must have messed something up. Using it now and it works like a charm. Thx alot @Stephan
– Uwe König
CommentedMar 31 at 14:47
I have removed your solution from the question area. Solutions belong squarely in the answer area. As a courtesy, I have replaced the content from your own answer with that information.
– Compo
CommentedMar 31 at 15:30
Add a comment
|
1 Answer
1
Reset to default
0
Solution:
Thx to @Stephan the now working code looks like this:
@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in (text.csv) do (
SET "varA=%%a"
SET "varA=!varA:/=_!"
SET "varB=%%a"
curl -o ./myFolder/!varA!.txt -v -X GET https://smyWebRoute?q=!varB! -H "Authorization: Bearer ..."
echo %%a
timeout /t 1
)
pause >nul
goto
breaks the loop-context, b) labels behave odd within loops and code blocks ("undefined behaviour" - it may skip lines or show other unintended "functionality"). Iron Rule: no labels in code blocks,goto
only to intentionally leave (and break) the loop or code block (jump out of the block, not within (and of course not into)) – Stephan Commented Mar 31 at 14:21for /f "delims=" %%a in (text.csv) do (
,set "var=%%a"
,echo !var:/=_!
,)
– Stephan Commented Mar 31 at 14:25