0

I have a batch script which uses command line arguments to perform a task; however, "%2" is also included in a URL, thus the url also gets the second command line argument pasted where %2 should be. How could I avoid this scenario while maintaining the command line arguments? One solution would be to store the command line arguments as a variable then destroy the original %2, but I am unsure of how to achieve this.

Would this be a case of setlocal and endlocal? If so, how could I destroy the command line arguments once I save them as variable names? I could also turn-off the command line arguments once I store them. Any ideas on how to do this?

I have been directed to this Stack Overflow question How can I URL-encode spaces in an NT batch file?; however, I am unsure if this is what I am looking for. It seems in this question, the OP was wanting to include the URL in the batch parameter call. I am wanting my batch parameter calls to remain %1 = username, %2 = password, %3 = fullname and then reference them in the script without them being pasted in the URL within the script. I am accessing the URL's via wget and "%20" is being used as a space. Thus, where ever %2 is, it is pasting my the password parameter rather than keeping it "%2". I know this Stack Overflow question is the right direction, but I cannot formulate a solution from it. Any tips or help?

Code Example:

foo.bat foobar passwordforfoobar foobarfullname

Within batch script

call wget.exe --keep-session-cookies --save-cookies="cookies.txt" --post-data="username=%1&password=%2" "www.foo.com"

call wget.exe --load-cookies=cookies.txt "www.foo.com/search=2017%20Foo%20test"
branch.lizard
  • 595
  • 1
  • 5
  • 15
  • Possible duplicate of [How can I URL-encode spaces in an NT batch file?](https://stackoverflow.com/questions/10218451/how-can-i-url-encode-spaces-in-an-nt-batch-file) – phuclv Oct 30 '17 at 06:05
  • See above edits. This is very close, but I am unsure how to proceed. I am much more comfortable in bash than batch; my apologies for the additional guidance needed. – branch.lizard Oct 30 '17 at 18:56
  • 1
    Consider changing `%20` to `%%20` and using `Start ""` instead of `Call` Are you aware that you've passed three parameters, despite only using two? Enter `Start/?` at the prompt for information on that command and whilst you're there take a look at `Shift/?`, you may find it interesting. – Compo Oct 30 '17 at 19:58
  • 1
    Yes, the third parameter is used a different section of the code. Using the Start "" command paired with %%20 did exactly what I was looking for. I will formulate an answer and credit you for the proper direction. Thank you. – branch.lizard Oct 30 '17 at 20:01
  • I've chosen the wrong duplcate. These are more correct: [Batch Opening a Website with %'s](https://stackoverflow.com/q/46721905/995714), [Ignore percent sign in batch file](https://stackoverflow.com/q/1907057/995714), [echo % (print an percent sign)](https://stackoverflow.com/q/32150412/995714) – phuclv Oct 31 '17 at 01:45

1 Answers1

0

User Compo suggested using Start "" (rather than call) paired with %%20 in the url and the code executed flawlessly.

call wget.exe --keep-session-cookies --save-cookies="cookies.txt" --post-data="username=%1&password=%2" "www.foo.com"

Start "" wget.exe --load-cookies=cookies.txt "www.foo.com/search=2017%%20Foo%%20test"
branch.lizard
  • 595
  • 1
  • 5
  • 15