0

I am sending various parameters to batch file. In that i need the parameter next to the "-l" ..

For example : when calling the .bat

Test.bat sampl.exe -s ssss -m mmmm -l path -k kkkk -d dddd

In this i need to extact "path" based on the presence of -l. In general, I need to extract the next parameter to "-l". Is there a way i could do it ? Please help

Ganesh
  • 3
  • 1

2 Answers2

1

Below there is a standard code for Batch files like this one. The code is even simpler if variable names are the same than the switches, i.e. set s=ssss set l=path etc.

@echo off
if "%1" neq "" goto getParams
rem A simple description of how to use this Batch file goes here, ie:
echo Test.bat progname [-s ssss] [-m mmmm] [-l path] [-k kkkk] [-d dddd]
goto :EOF

:getParams
rem Set here default parameter values, ie:
for %%a in (s m l k d) do set %%a=
set progName=%1
:shift
shift
for %%a in (s m l k d) do if /I "%1" equ "-%%a" goto getParam
goto main

:getParam
set %1=%2
shift
goto shift

:main
rem Run here the program, ie:
cd %l%
%progName% %s% %m% %k% %d%

I hope it helps...

Aacini
  • 65,180
  • 12
  • 72
  • 108
0

The idea is to loop through the list of parameters and if -l is found then call another section that then extracts the next parameter.

The SHIFT, removes the first parameter from the list of available parameters. eg:

If you ran: sampl.exe -s ssss -m mmmm -l path -k kkkk -d dddd

The available parameters would be = -s ssss -m mmmm -l path -k kkkk -d dddd\

If in the script you executed SHIFT, then the available parameters would be = ssss -m mmmm -l path -k kkkk -d dddd

See the code example below:

@ECHO OFF

SET path=
SET mmm=
SET sss=
SET ddd=

REM Loop through passed parameters
:LOOP
IF [%1]==[] GOTO LOOP_END
    IF [%1]==[-s] @CALL :PROCESS_S %2
    IF [%1]==[-m] @CALL :PROCESS_M %2
    IF [%1]==[-l] @CALL :PROCESS_L %2
    IF [%1]==[-d] @CALL :PROCESS_D %2
    SHIFT
GOTO LOOP
:LOOP_END


REM call your actual end result here.. Once the batch file gets here, the variable path would have been set if there was a -l <my path> passed, otherwise it would be empty
cd %path%
runmyprogram.exe %sss% %mmm% %ddd%  

GOTO:EOF


REM Define your methods down here.
:PROCESS_S
    IF [%1]==[] GOTO:EOF
    SET sss=%1
    SHIFT
    GOTO:EOF

:PROCESS_M
    IF [%1]==[] GOTO:EOF
    SET mmm=%1
    SHIFT
    GOTO:EOF

:PROCESS_L
    IF [%1]==[] GOTO:EOF
    SET path=%1
    SHIFT
    GOTO:EOF

:PROCESS_D
    IF [%1]==[] GOTO:EOF
    SET ddd=%1
    SHIFT
    GOTO:EOF
AlexS
  • 650
  • 6
  • 12