0

I use a cURL command stored in .bat file.

I want to have variables implemented in the command, that will receive the value when I call the command to execute the .bat file.

Can anybody help with understading the syntax of:

  • Passing variables to batch file
  • Defining certain parts of the cURL command as variables (The parts in bold below)

Here is my cURL command (Parts in bold should be variables):

curl -k --user "usercode:" -d source="https://secure.4log.com/**431032**" https://api.pdfshift.io/v2/convert/ -d sandbox=true --output C:\Users\administrator\Desktop\**testpdf11**.pdf
Yaniv Ben-Malka
  • 47
  • 3
  • 10
  • 2
    so you want to run your batch as `somebatchfile.bat param1 param2` etc? – Gerhard Aug 27 '18 at 12:02
  • 2
    Call the batch file with two arguments/parameters,`BatchScript.cmd "431032" "testpdf11"` where the script would receive `431032` as `%~1` and `testpdf11` as `%~2`! `curl -k --user "usercode:" -d source="https://secure.4log.com/%~1" https://api.pdfshift.io/v2/convert/ -d sandbox=true --output C:\Users\administrator\Desktop\%~2.pdf` – Compo Aug 27 '18 at 12:04
  • Possible duplicate of [Get list of passed arguments in Windows batch script (.bat)](https://stackoverflow.com/questions/357315/get-list-of-passed-arguments-in-windows-batch-script-bat) – aschipfl Aug 27 '18 at 13:15

2 Answers2

1

If you want to send parameters you simply use:

curl %1 %2 %3

Each numeric value defines the position of the word after your batch.

so as a simple example, if you run only need to run something like:

mybatch.bat 1234 5678

That 1234 will be seen as %1 and 5678 will be %2 and you will get:

source="https://secure.4log.com/%1" C:\Users\administrator\Desktop\%2.pdf

resulting to:

source="https://secure.4log.com/1234" C:\Users\administrator\Desktop\5678.pdf

If you plan on using paramters with space, or any quoted text for that matter, you should rather use %~1 and %~2 as it will remove the quotes for you.

Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • IMPORTANT NOTE - It worked in the batch file only with a tilde ( %~1 and %~2) as @compo suggested in my original post comments – Yaniv Ben-Malka Aug 27 '18 at 12:53
  • 1
    @YanivBen-Malka yes. I am aware. `~` simply removes double quotes. So if you use quoted input i.e paramters with a space, the you require the `~` simply test it by creating a test batch file and add code. `echo %1 %~1` then call it as `batchfile.bat "this line"` and you will see it will echo `"this line" this line` – Gerhard Aug 27 '18 at 13:06
1

Parameters to the batchfile are referenced by %1 for the first one, %2 for the second one and so on until %9 This makes your command:

curl -k --user "usercode:" -d source="https://secure.4log.com/%~1" https://api.pdfshift.io/v2/convert/ -d sandbox=true --output C:\Users\administrator\Desktop\%~2.pdf

Note: use %~1 to remove surrounding quotes (no effect, if there aren't such)

You might want to check if there are at least two parameters before with:

if "%~2"=="" (echo you need two parameters & goto :eof )

(Maybe even add some plausibility tests)

Stephan
  • 53,940
  • 10
  • 58
  • 91