Reputation: 745
I want to pass %DownParameters%
and %URL%
to function :myWGET
in below code stored in a batch file with name abc.bat
.
set DownParameters="--continue --wait=5 --no-check-certificate --retry-connrefused --tries=30 -blah -blah"
set URL=https://someip/a.zip
call :myWGET %DownParameters% %URL%
:myWGET
wget.exe %1 %2
REM what i expected is wget.exe %DownParameters% %URL%
goto:eof
However, this is not working because of the spaces in %DownParameters%
.
The workaround is using %DownParameters%
directly in :myWGET
, but that is not wanted.
How to pass the arguments in %DownParameters%
and %URL%
to function :myWGET
for usage on wget.exe
command line?
[update 1]
by using
set "DownParameters=--no-check-certificate --wait=30 --tries=1 --retry-connrefused --header="Connection: close""
--header="Connection: close"
will still cause trouble when
Call :myWGET "%DownParameters%" "%URL%"
echo %~1
in :myWGET
show
--no-check-certificate --wait=30 --tries=1 --retry-connrefused --header="Connection:
, the "` close" is missing, what need to be escaped?
Upvotes: 0
Views: 5424
Reputation: 38718
Similar to that already answered, it's all a matter of correct quoting.
Set "DownParameters=--continue --wait=5 --no-check-certificate --retry-connrefused --tries=30 -blah -blah"
Set "URL=https://someip/a.zip"
Call :myWGET "%DownParameters%" "%URL%"
Rem Any other commands here
GoTo :EOF
:myWGET
WGet %~1 %2
GoTo :EOF
In the Call
command, the doublequote pairs encompass each parameter/argument. In the WGet
command the ~
expands each metavariable removing those encompassing doublequotes.
Upvotes: 3
Reputation: 49187
First, take a look on answers on Why is no string output with 'echo %var%' after using 'set var = text' on command line? and How to set environment variables with spaces? Those answers explain very detailed the difference between set "variable=value"
and set variable="value"
.
The working code:
set "DownParameters=--continue --wait=5 --no-check-certificate --retry-connrefused --tries=30 -blah -blah"
set "URL=https://someip/a.zip"
call :myWGET %DownParameters% "%URL%"
goto :EOF
:myWGET
for %%I in (%*) do if /I "%%~I" == "STOP" exit /B 1
echo wget.exe %*
wget.exe %*
goto :EOF
A FOR loop or FINDSTR can be used to exit the subroutine if any argument passed to the subroutine is case-insensitive the word STOP
.
for %%I in (%*) do if /I "%%~I" == "STOP" exit /B 1
The FOR loop processes each argument in the argument list and runs IF for a simple case-insensitive string comparison whereby double quotes are removed from argument string before comparing with word STOP
.
echo %* | %SystemRoot%\System32\findstr.exe /I /R "\<STOP\>" >nul 2>&1 && exit /B 1
ECHO redirects the list of arguments as one line to FINDSTR which searches case-insensitive for the word STOP
. FINDSTR terminates with exit code 0 if the word is really found in which case Windows command processor executes the command EXIT. Otherwise FINDSTR terminates with 1 indicating that the searched string is not found and so Windows command processor continues with next command line in the batch file. The output of FINDSTR to handle STDOUT and STDERR are suppressed by redirecting it to device NUL as not needed.
The FOR loop is better in my opinion as really comparing entire argument strings without or with enclosing double quotes separated by one or more spaces.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
... explains also %*
echo /?
findstr /?
for /?
goto /?
if /?
set /?
See also:
Upvotes: 2