Passing parameters through CMD in batch files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
yakirllc
Posts: 4
Joined: 02 Dec 2013 03:18

Passing parameters through CMD in batch files

#1 Post by yakirllc » 02 Dec 2013 03:31

Hi,

It's hard to explain my problem so I'll just show it to you:
I want to run the following command: run.bat "1 \"3 2\"" and get the following output:
param1: 1 param2: 3 2
Instead, I get nothing.

Here is my script:
@echo off
set arg=%~1
FOR /F "tokens=1*" %%I in ("%arg%") do @echo param1: %%I param2: %%~J

I dont want to use small ticks:
run.bat "1 '3 2'"

It's important for me to use \" because I need to have the possibility of running the following command: run.bat "1 \"2 '3 4'\"" and get the output:
param1: 1 param2: 2 '3 4'

Thanks for the help.

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Passing parameters through CMD in batch files

#2 Post by ShadowThief » 02 Dec 2013 07:06

Any particular reason you have to have all the parameters passed in as one giant parameter?

yakirllc
Posts: 4
Joined: 02 Dec 2013 03:18

Re: Passing parameters through CMD in batch files

#3 Post by yakirllc » 02 Dec 2013 08:31

Hi,

Yes, because this is how my system works.

Thanks.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Passing parameters through CMD in batch files

#4 Post by foxidrive » 02 Dec 2013 08:32

Double quotes are special characters. It also looks like you are escaping something with slashes.

If you want an answer that works in your situation then think about showing us exactly what you are doing, rather than contrived examples that don't explain the task.

yakirllc
Posts: 4
Joined: 02 Dec 2013 03:18

Re: Passing parameters through CMD in batch files

#5 Post by yakirllc » 02 Dec 2013 08:49

OK then, My goal is as the following, I need to have a batch script that executes a perl script while this perl script might need to get parameters and those parameters can be complicated like:
tasklist | find /i "some process"
and so on.
Why do I must use the batch script? because my system has to execute it using C:\path\to\perl.exe script.pl "arg1" "arg 2"
At the end, the batch call should look like:
run.bat "script.pl \"arg1\" \"arg 2\""
Hope it helps you.

Thanks.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Passing parameters through CMD in batch files

#6 Post by foxidrive » 03 Dec 2013 06:34

Many scripting languages can use a hex or ascii number representation of double quotes. \x22 for example.

Without knowing the exact task and parameters it's hard to suggest anything, that isn't going to break in some way or fashion.

yakirllc
Posts: 4
Joined: 02 Dec 2013 03:18

Re: Passing parameters through CMD in batch files

#7 Post by yakirllc » 03 Dec 2013 06:43

Hi,

I understand.

Thanks of the help.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Passing parameters through CMD in batch files

#8 Post by penpen » 03 Dec 2013 08:14

If the multiple encapsulated parameters (in one parameter) don't contain any further doublequotes, then you may do this to extract it:

Code: Select all

@echo off
setlocal enableDelayedExpansion
set params=%*
if """" == "%params:~0,1%%params:~-1%" set "params=%params:~1,-1%"
set "N=0"
for %%a in (%params%) do (
   set /A "N+=1"
   set "param[!N!]=%%a"
)
for /L %%n in (1,1,!N!) do echo param[%%n]=!param[%%n]!
endlocal
goto :eof

penpen

Post Reply