0

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?

K. C
  • 735
  • 2
  • 8
  • 25
  • 2
    It's a bad habit to ***not*** separate the subroutine from program flow with a prior `Goto :Eof`. After having called the label `:myWget` program flow reaches the wget command again this time using eventually present command line arguments - not call arguments. –  Jun 10 '18 at 13:27

2 Answers2

3

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.

Compo
  • 36,585
  • 5
  • 27
  • 39
  • quoting matters :-). Thank you, @Compo, `WGet %~1 %~2` could be better in my case consider I may need a `%~3` as well. – K. C Jun 10 '18 at 13:47
  • @K.C, In that particular scenario, and based on the additional information under the other answer, you'd probably want to use, `If /I %3=="STOP" Exit /B 1`, or `If /I "%~3"=="STOP" Exit /B 1` because the comparison includes the encompassing doublequotes too. – Compo Jun 10 '18 at 15:13
  • thanks, @Compo updated question, since `--header="Connection: close"` will cause similar trouble again in `%~1`, not sure what need to be escaped and how... – K. C Jun 10 '18 at 15:29
  • @Mofi, there's absolutely nothing wrong with it in use with the URL provided in the OP's question and my answer. What you should have said, is that you do not recommend the use of `%~2` should the passed URL contain ampersands, and the OP could have reverted to `%2`. I have made that modification in my answer, because I doubt that the doublequotes around it should need removing in either case, _and especially with their inclusion now of a `%3` argument_. – Compo Jun 10 '18 at 16:53
1

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:

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • Thank you @Mofi, I learned a lot in your posts. It is shame that I never notice those details even I use windows every day :-( the reason I want `%DownParameters%` as `%1` in function `myWGET` is I want do something like `if "%3"=="STOP" ( exit /b 1 )` in function `myWGET`, since the call is `wget.exe %*`, any chance I can still use that? I tested the `%1` is `--continue` – K. C Jun 10 '18 at 13:28
  • Thank you, I will try `FOR` code for long term solution. now I still confusing why `--header="Connection: close"` the `" close"` part will missing in `%~1`, it is surely the `blank` and `"` related. no matter how I using escape char `^`, the `close` always missing in `%~1`. The very ugly solution I use now is `set UglyDownParameters1=%DownParameters: =?%` because `?` will never be used in my case, pass it to `:myWGET`, and in `:myWGET` replace `?` backing to `blank` again ugly but works, and the reason I stick in `%~1` is to make sure it aligned with corresponding scripts in `bash` :-( – K. C Jun 10 '18 at 16:35
  • ,thanks for the explanation, the strange part is the `"Connection: close"` itself, 1,you can replace that blank with `double quotes or comma`, the arguments list will pass to `%1` success , but ` tab/space` not working in that `double quotes, spaces, tabs and comma` set, so I guess `tab/space` might be special when command processor splits arguments first hits double quotes then space, 2, and I tried `:Connection: close"`,`[space]Connection: close"`,`[tab]Connection: close"`,`""Connection: close"` all works, the only not working format is `"Connection: close"`, that is strange anyway. – K. C Jun 10 '18 at 17:25