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:
  • 711 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I pass arguments to a batch file?

#11
Simple solution(even though question is old)

Test1.bat

echo off
echo "Batch started"
set arg1=%1
echo "arg1 is %arg1%"
echo on
pause

CallTest1.bat

call "C:\Temp\Test1.bat" pass123

output

YourLocalPath>call "C:\Temp\test.bat" pass123

YourLocalPath>echo off
"Batch started"
"arg1 is pass123"

YourLocalPath>pause
Press any key to continue . . .

Where YourLocalPath is current directory path.

To keep things simple store the command param in variable and use variable for comparison.

Its not just simple to write but its simple to maintain as well so if later some other person or you read your script after long period of time, it will be easy to understand and maintain.

To write code inline : see other answers.
Reply

#12
Accessing batch parameters can be simple with %1, %2, ... %9 or also %*,
but only if the content is simple.

There is no simple way for complex contents like `"&"^&`, as it's not possible to access %1 without producing an error.

set var=%1
set "var=%1"
set var=%~1
set "var=%~1"

The lines expand to

set var="&"&
set "var="&"&"
set var="&"&
set "var="&"&"

And each line fails, as one of the `&` is outside of the quotes.

It can be solved with reading from a temporary file a **remarked** version of the parameter.

@echo off
SETLOCAL DisableDelayedExpansion

SETLOCAL
for %%a in (1) do (
set "prompt="
echo on
for %%b in (1) do rem * #%1#
@echo off
) > param.txt
ENDLOCAL

for /F "delims=" %%L in (param.txt) do (
set "param1=%%L"
)
SETLOCAL EnableDelayedExpansion
set "param1=!param1:*#=!"
set "param1=!param1:~0,-2!"
echo %%1 is '!param1!'

The trick is to enable `echo on` and expand the %1 after a `rem` statement (works also with `%2 .. %*`).
So even `"&"&` could be echoed without producing an error, as it is remarked.

But to be able to redirect the output of the `echo on`, you need the two for-loops.

The extra characters `* #` are used to be safe against contents like `/?` (would show the help for `REM`).
Or a caret ^ at the line end could work as a multiline character, even in after a `rem`.

Then reading the **rem parameter** output from the file, but carefully.
The FOR /F should work with delayed expansion off, else contents with "!" would be destroyed.
After removing the extra characters in `param1`, you got it.

And to use `param1` in a safe way, enable the delayed expansion.
Reply

#13
Here's how I did it:

@fake-command /u %1 /p %2

Here's what the command looks like:

test.cmd admin P@55w0rd > test-log.txt

The `%1` applies to the first parameter the `%2` (and here's the tricky part) applies to the second. You can have up to 9 parameters passed in this way.
Reply

#14
To refer to a set variable in command line you would need to use `%a%` so for example:

set a=100
echo %a%
rem output = 100

Note: This works for Windows 7 pro.
Reply

#15
In batch file

set argument1=%1
set argument2=%2
echo %argument1%
echo %argument2%

`%1` and `%2` return the first and second argument values respectively.

And in command line, pass the argument

Directory> batchFileName admin P@55w0rd

Output will be

admin
P@55w0rd
Reply

#16
Paired arguments
----------------

If you prefer passing the arguments in a key-value pair you can use something like this:

@echo off

setlocal enableDelayedExpansion

::::: asigning arguments as a key-value pairs:::::::::::::
set counter=0
for %%# in (%*) do (
set /a counter=counter+1
set /a even=counter%%2

if !even! == 0 (
echo setting !prev! to %%#
set "!prev!=%%~#"
)
set "prev=%%~#"
)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: showing the assignments
echo %one% %two% %three% %four% %five%

endlocal


And an example :

c:>argumentsDemo.bat one 1 "two" 2 three 3 four 4 "five" 5
1 2 3 4 5


Predefined variables
--------------------

You can also set some environment variables in advance. It can be done by setting them in the console or [setting them from my computer][1]:

@echo off

if defined variable1 (
echo %variable1%
)

if defined variable2 (
echo %variable2%
)

and calling it like:

c:\>set variable1=1

c:\>set variable2=2

c:\>argumentsTest.bat
1
2

File with listed values
-----------------------

You can also point to a file where the needed values are preset.
If this is the script:

@echo off

setlocal
::::::::::
set "VALUES_FILE=E:\scripts\values.txt"
:::::::::::


for /f "usebackq eol=: tokens=* delims=" %%# in ("%VALUES_FILE%") do set "%%#"

echo %key1% %key2% %some_other_key%

endlocal

and values file is this:

:::: use EOL=: in the FOR loop to use it as a comment

key1=value1

key2=value2

:::: do not left spaces arround the =
:::: or at the begining of the line

some_other_key=something else

and_one_more=more

the output of calling it will be:

>value1 value2 something else

Of course you can combine all approaches. Check also [arguments syntax][2] , [shift][3]


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

Reply

#17
Everyone has answered with really complex responses, however it is actually really simple. ```%1 %2 %3``` and so on are the arguements parsed to the file. ```%1``` is arguement 1, ```%2``` is arguement 2 and so on.

So, if I have a bat script containing this:
```shell
@echo off
echo %1
```
and when I run the batch script, I type in this:
```
C:> script.bat Hello
```
The script will simply output this:
```
Hello
```
This can be very useful for certain variables in a script, such as a name and age. So, if I have a script like this:
```shell
@echo off
echo Your name is: %1
echo Your age is: %2
```
When I type in this:
```
C:> script.bat Oliver 1000
```
I get the output of this:
```
Your name is: Oliver
Your age is: 1000
```
Reply

#18
If you're worried about security/password theft (that led you to design this solution that takes login credentials at execution instead of static hard coding without the need for a database), then you could store the api or half the code of password decryption or decryption key in the program file, so at run time, user would type username/password in console to be hashed/decrypted before passed to program code for execution via `set /p`, if you're looking at user entering credentials at run time.

If you're running a script to run your program with various user/password, then command line args will suit you.

If you're making a test file to see the output/effects of different logins, then you could store all the logins in an encrypted file, to be passed as arg to test.cmd, unless you wanna sit at command line & type all the logins until finished.

The number of args that can be supplied is [limited to total characters on command line](

[To see links please register here]

). To overcome this limitation, the previous paragraph trick is a workaround without risking exposure of user passwords.
Reply

#19
Another useful tip is to use `%*` to mean "all". For example:

echo off
set arg1=%1
set arg2=%2
shift
shift
fake-command /u %arg1% /p %arg2% %*

When you run:

test-command admin password foo bar

The above batch file will run:

fake-command /u admin /p password admin password foo bar

I may have the syntax slightly wrong, but this is the general idea.
Reply

#20
<!-- language-all: lang-bat -->

[![enter image description here][1]][1]

> *For to use looping get all arguments and in pure batch:*

**Obs:** For using without: **`?*&|<>`**
_ _ _

@echo off && setlocal EnableDelayedExpansion

for %%Z in (%*)do set "_arg_=%%Z" && set/a "_cnt+=1+0" && (
call set "_arg_[!_cnt!]=!_arg_!" && for /l %%l in (!_cnt! 1 !_cnt!
)do echo/ The argument n:%%l is: !_arg_[%%l]!
)

endlocal

> *Your code is ready to do something with the argument number where it needs, like...*
<!-- language-all: lang-bat -->

@echo off && setlocal EnableDelayedExpansion

for %%Z in (%*)do set "_arg_=%%Z" && set/a "_cnt+=1+0" && call set "_arg_[!_cnt!]=!_arg_!"

fake-command /u !_arg_[1]! /p !_arg_[2]! > test-log.txt


[1]:

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

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