Home > Software engineering >  wget with Variables in URL in Windows Batch
wget with Variables in URL in Windows Batch

Time:04-01

I'm trying to download files with wget in a windows batch file using variables in url.

Here's my code example

set gpsweek=2202
set weekday=1
wget http://ftp.aiub.unibe.ch/CODE/COD%gpsweek%%weekday%.EPH_M

This doesn't works. But

wget http://ftp.aiub.unibe.ch/CODE/COD22021.EPH_M

works. So i guess it's something with the string. I tried to use some " " around the string, but it also doesn't work.

Before wget i used curl and i had no problems with the arguments.

Any ideas what i'm doing wrong?

Thank you in advance. Pat

CodePudding user response:

a Good way to see why something "doesn't work" in batch-files is to debug the code. One way is to use echo. You can simply echo the line to see if the result is what you expect it to be. If it is not, then you can rectify it.

Variables are sensitive to whitespace. We need to ensure we do not include any of them, even accidently. To do that, simply enclose the variables during set in double quotes from before the variable name until the end of the value set "var=value"

So your code should work as expected by simply running it with the double quotes:

@echo off
set "gpsweek=2202"
set "weekday=1"
wget "http://ftp.aiub.unibe.ch/CODE/COD%gpsweek%%weekday%.EPH_M"
:: for debugging purposes, we echo the URL, this can be removed in production
echo "http://ftp.aiub.unibe.ch/CODE/COD%gpsweek%%weekday%.EPH_M"
  • Related