Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 693 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to pass additional extra arguments to a batch file?

#1
I would like to use first 2 arguments passed to a batch file and then append everything else to the command executed in the batch file.

Is there an easy way to do that?

// batch file
command -Duser=%1 -Dpwd=%2 {? how to pass all remaining arguments on the command line}

The batch will be executed like this

batch.bat USR PWD -Dargument1=1 -Dargument2=2

Effectively, I would like the following to be executed

command -Duser=USR -Dpwd=PWD -Dargument1=1 -Dargument2=2
Reply

#2
This might be what you want

C:>help shift
Changes the position of replaceable parameters in a batch file.

SHIFT [/n]

If Command Extensions are enabled the SHIFT command supports
the /n switch which tells the command to start shifting at the
nth argument, where n may be between zero and eight. For example:

SHIFT /2

would shift %3 to %2, %4 to %3, etc. and leave %0 and %1 unaffected.

Reply

#3
The `for` command should give you what you want:

for /f "tokens=1,2*" %%a in ("%*") do echo command -Duser=%%a -Dpwd=%%b %%c

From the help for `for` (`for /?`):


----------


`FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do @echo %i %j %k`
would parse each line in myfile.txt, ignoring lines that begin with
a semicolon, passing the 2nd and 3rd token from each line to the for
body, with tokens delimited by commas and/or spaces. **Notice the for
body statements reference %i to get the 2nd token, %j to get the
3rd token, and %k to get all remaining tokens after the 3rd.** For
file names that contain spaces, you need to quote the filenames with
double quotes. In order to use double quotes in this manner, you also
need to use the usebackq option, otherwise the double quotes will be
interpreted as defining a literal string to parse.


----------


So in my solution, `%%a` has the value of the first argument passed to the script, `%%b` the value of the second argument, and `%%c` the remaining arguments.

When I run `batch.bat USR PWD -Dargument1=1 -Dargument2=2` from a command prompt I get:

command -Duser=USR -Dpwd=PWD -Dargument1=1 -Dargument2=2

Remove the `echo` after the `do` to call your command with the arguments.
Reply

#4
Patrick Cuff's solution usually works, but it will fail if either of the first 2 arguments contain quoted spaces or special characters. It will also fail if the arguments are delimited with something other than spaces. The FOR /F delimiters could be expanded to include `,` `;` and `=`, but then it will fail if the argument contains any of those quoted delimiters.

The following improvement supports quoted special characters like `&` and `|`, but the argument delimiters are still a problem.

@echo off
setlocal enableDelayedExpansion
set args=%*
for /f "tokens=1,2*" %%a in ("!args!") do (
endlocal
echo arg1=%%a arg2=%%b remainder=%%c
)

***EDIT*** *Here is a simpler modification using USEBACKQ that achieves the same thing*

for /f "usebackq tokens=1,2*" %%a in ('%*') do echo arg1=%%a arg2=%%b remainder=%%c

*End edit*

Unfortunately a truly general solution requires a GOTO loop that parses every parameter from 3 on. The solution below should work as long as all quotes are balanced and all special characters are quoted.

@echo off
setlocal
set "args="
:parseArgs
set args=%args% %3
shift /3
if "%~3" neq "" goto :parseArgs
if (%3) equ ("") goto :parseArgs
echo arg1=%1 arg2=%2 remainder=%args%

To eliminate all limitations would require drastic measures - see

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through