How to retrieve script parameters? - windows

I have the batch file MyBatch.bat and I call it like:
MyBatch.bat -param1=abc -param2="def jkl"
how from MyBatch.bat can I retrieve the value of param1 (abc) and param2 (def jkl)

setlocal
:parmloop
if "%~1" neq "" set "%~1=%~2"&shift&shift&goto parmloop
set -

Related

Batch Script or PowerShell script to read the multiple values from text file and insert into variables

I am looking for a batch script or PowerShell script that read values from a text file and place them in a script local variables.
For example the input file looks like:
input.txt
PARAM1=VALUE1
PARAM2=VALUE2
PARAM3=VALUE3
In the script I would like to place the values into three different variables like:
echo %variable1%
echo %variable2%
echo %variable3%
The output will be:
value1
value2
value3
I started the script like:
#echo off
setlocal
for /f "delims== tokens=1,2" %%G in ("input.txt") do set %%G=%%H echo %G%
Please provide me some insights or ideas
The code is almost okay except for:
usebackq option is needed if you specify the input file in doublequotes
echo %G% should be either echo !%%G! and of course you'll need setlocal enableDelayedExpansion at the start of the script or just before the loop.
Or in your case you can simply print the value in %%H.
use () for multi-line for-loops, otherwise separate the commands with & on the same line
use quotes in set as shown in the code below in case the values have special characters.
use echo. to print a line that could be empty (/,:,( may be used instead of dot)
for /f "delims== tokens=1,2 usebackq" %%G in ("input.txt") do (
set "%%G=%%H"
echo.%%H
)
I want to read values from a file and assign them to script local variables.
#echo off
setlocal
for /f "delims== tokens=1,2" %%G in ("input.txt") do set %%G=%%H echo %G%
Your batch file is close, but need a couple of tweaks:
In order to read the contents of input.txt in the for loop you need to type the file.
You have a mistake in your echo command, it should be %%G not %%H.
To display the variable names
test.cmd:
#echo off
setlocal
for /f "delims== tokens=1,2" %%G in ('type input.txt') do (set %%G=%%H && echo %%G)
endlocal
Example usage:
F:\test>type input.txt
PARAM1=VALUE1
PARAM2=VALUE2
PARAM3=VALUE3
F:\test>test
PARAM1
PARAM2
PARAM3
To display the values of the variables, replace echo %%G with echo %%H
test.cmd:
#echo off
setlocal
for /f "delims== tokens=1,2" %%G in ('type input.txt') do (set %%G=%%H && echo %%H)
endlocal
Example usage:
F:\test>type input.txt
PARAM1=VALUE1
PARAM2=VALUE2
PARAM3=VALUE3
F:\test>test
VALUE1
VALUE2
VALUE3
Further Reading
An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
for /f - Loop command against the results of another command.
type - Display the contents of one or more text files.
If you want to use the PowerShell syntax can be:
$InputText = Get-Content "C:\Temp\input.txt"
$i=0 #add inkrement
ForEach ($line in $InputText)
{
$i++ #increment for variable numbers
($line -split "=")[1] | Set-Variable -Name ("variable$i")
}
#Show variables
$variable1
$variable2
#and so
More elegant than batch files :)

Windows Batch File Expansion Variables

I'm having an issue with a Batch File. Basically, I would like to seek ahead by 1 in a for loop and use that value to reference the parameter at that position and then assign it to a variable which I can then use later, see code sample below.
Example: set myVar=%1+1
Where %1 is param name, and %2 would be param value.
::Batch file sample
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set count=0
set HasParam=0
set ParamValue="null"
set paramValPos=0
for %%i in (%*) do (
set /a count=!count!+1
if /i "%%i"=="MyParam" (
set /a HasParam=1
set /a paramValPos=!count!+1
::The next line is where I Need Help, it's currently incorrect!
set ParamValue=%!paramValPos!
)
)
if %HasParam%==1 (
echo "Parameter Value: %ParamValue%"
)
pause
Example call from command line: prog.bat MyParam=5
Now I know that I can access it at pos 2, like:
::Sample batch file
#echo off
echo "Param Name: %1"
echo "Param Value: %2"
pause
However, this is not what I want, as there are more parameters and some are optional and they can also be passed in, in any order.
Thanks for your assistance.
call set "ParamValue=%%!paramValPos!"
If I understood your request correctly, you want the next parameter after a given one. The Batch code below do that:
#echo off
set "HasParam="
set "ParamValue="
for %%i in (%*) do (
if defined HasParam (
if not defined ParamValue (
set ParamValue=%%i
)
) else if /i "%%i"=="MyParam" (
set HasParam=yes
)
)
if defined HasParam (
echo "Parameter Value: %ParamValue%"
)
pause

Updating a command line parameter in a batch file

Is it possible to update or replace a command line parameter (like %1) inside of a batch file?
Sample code:
rem test.cmd
#echo off
echo Before %1
IF "%1" == "123" (
set %%1 = "12345678"
)
echo After %1
Desired Result:
C:/>Test 123
Before 123
After 12345678
Actual Result:
C:/>Test 123
Before 123
After 123
No. What you are trying is not possible.
Can be simulated passing original batch parameters to subrutine, or call the same cmd recursively with modified parameters, which again get %1, %2, ... the parameters provided in the call. But this is not what you ask.
rem test.cmd
#echo off
echo Before %1
if "%~1"=="123" (
call :test %1234
) else (
call :test %1
)
goto :EOF
:test
echo After %1
Argument variables are reserved, protected variables, you can't modify the content of one of those variables by yourself.
I suggest you to store the argument in a local variable then you can do all operations you want:
#echo off
Set "FirstArg=%~1"
Echo: Before %FirstArg%
IF "%FirstArg%" EQU "123" (
Set "FirstArg=12345678"
)
Echo: After %FirstArg%
Pause&Exit

Echo length of a variable from the windows command line

So I'm looking for a quick command to evaluate the length of environment variables on the windows command line.
Conceptually, something to the effect of:
echo %PATH%.length
Thanks!
#echo off
setlocal enabledelayedexpansion
call :COUNT "%path%"
echo Your variable is %length% characters long
pause >nul
:COUNT
set temp=%1
set length=0
:loop
if defined temp (
set temp=!temp:~1!
set /a length+=1
goto :loop
)
Usage: call :COUNT %yourvar% and the length will be stored in %length%.

How do I return a value from a function in a batch file?

I have the following batch file
#echo off
setlocal EnableDelayedExpansion
for /f "delims==" %%J in (File_List.txt) do (
call :setDate %%J MYD
echo/Date is: %MYD%
)
endlocal &goto :eof
:setDate
SETLOCAL ENABLEEXTENSIONS
echo %1
echo %~2
set NAME=%1
set NAME=%NAME:~-11%
echo %NAME%
echo %~2
endlocal&set %2=%NAME%&goto :eof
but with File_List.txt containing
file2012-05.csv
I get
file2012-05.csv
MYD
2012-05.csv
MYD
Date is:
How do I actually get the function setDate to return the value I want?
As I don't understand from your script what you want to achieve, I reply (for completeness) to the original subject: returning a value from a function.
Here is how I do it:
#echo off
set myvar=
echo %myvar%
call :myfunction myvar
echo %myvar%
goto :eof
:myfunction
set %1=filled
goto :eof
Result is:
empty
filled
The batch interpreter evaluates %MYD% at parse time, and at that time it's empty. That's why you have Delayed Expansion. Change this line:
echo/Date is: %MYD%
to this:
echo/Date is: !MYD!
and it will work like you want, because then it tells the interpreter to evaluate MYD at run-time.

Resources