Prerequisites
I have a file called urls.txt
where I store my URLs, e.g.
www.google.de/test1.yaml?at=refs/heads/master
www.google.de/test2.yaml?at=refs/heads/master
www.google.de/test3.yaml?at=refs/heads/master
My goal
Now I want to loop through these URLs and pass them to another function to download them.
:downloader
FOR /F "delims=" %%i IN (urls.txt) DO (call :sub_function %%i)
goto :eof
:sub_function
echo here url is: %~1
Output
The output is that it cuts off the query strings from the URLs and does not pass them completely to the next function.
For example, the output is: www.google.de/test1.yaml?at
What do I miss?
CodePudding user response:
To protect special characters (like the =
-sign in your situation, which constitutes a standard token separator just like SPACE, TAB, ,
and ;
), use quotation for the argument, so it is really treated as one.
Then the call
command initiates a second %
-expansion phase, which is the reason why the %
-signs in your argument cause issues (actually the sequence %2
represents the second argument of your script). To circumvent that problem, store the argument string in a variable and ensure that is is going to be expanded during said second %
-expansion phase.
Since URLs may also contain the &
-symbol, the argument in the sub-function should not become expanded unquoted in order not to misinterpret it as the command concatenation operator.
Here is the corrected code:
:downloader
FOR /F "delims=" %%i IN (urls.txt) DO (
set "ARG=%%i" & call :sub_function "%%ARG%%"
)
goto :eof
:sub_function
echo here url is: "%~1"