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
3
  • 2
    so you want to run your batch as somebatchfile.bat param1 param2 etc? Commented Aug 27, 2018 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 Commented Aug 27, 2018 at 12:04
  • Possible duplicate of Get list of passed arguments in Windows batch script (.bat) Commented Aug 27, 2018 at 13:15

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

IMPORTANT NOTE - It worked in the batch file only with a tilde ( %~1 and %~2) as @compo suggested in my original post comments
@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
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)

2 Comments

IMPORTANT NOTE - It worked in the batch file only with a tilde ( %~1 and %~2) as @compo suggested in my original post comments
The ~ is to remove surrounding quotes. You didn't mention quotes in your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.