How to pass multiple parameters in CMD to batch file - batch-file

i dont get it. I would like to pass multiple parameters to a batch file, that has the following description:
When passing parameters instead the first parameter (%1) should be PARAM and the
other parameters are shown in the list.
%epin% or %1 contains the file with full path and no extensions for input files
%epout% or %2 contains the file with full path and no extensions for output files
%epinext% or %3 contains the extension of the file selected from the EP-Launch
program. Could be imf or idf -- having this parameter ensures that the correct user
selected file will be used in the run.
%epwthr% or %4 contains the file with full path and extension for the weather file
%eptype% or %5 contains either "EP" or "NONE" to indicate if a weather file is used
%pausing% or %6 contains Y if pause should occur between major portions of
batch file
%maxcol% or %7 contains "250" if limited to 250 columns otherwise contains
"nolimit" if unlimited (used when calling readVarsESO)
%convESO% or %8 contains Y if convertESOMTR program should be called
%procCSV% or %9 contains Y if csvProc program should be called
%cntActv% or %10 contains the count of other simulations active or about to be
active
%multithrd% or %11 contains N if multithreading should be disabled
All i want is to pass parameters %1, %2, %3, %4 and %5... the rest should not be set...
Can somebody please tell me how this works? I searched the web and tried for hours but i won`t get this.
Thanks and greets!

It looks to me like the author of this batch file allows you to use it in 2 different ways,
Option 1
Just pass the parameters on the command line,
file.bat Param1 Param2 Param3 .....
Option 2
set the variables listed and then call the batch file with the single parameter that is exactly PARAM.
SET epin=Param1
SET epout=Param2
...
file.bat PARAM

what is the problem with ?:
set "epin=%~dpfn1"
set "epout=%~dpfn2"
set "epinext=%~3"
if /I not .%epinext% equ .imf (
if /I not .%epinext% equ .idf (
echo wrong extension
exit /b 1
)
)
set "epwthr=%%~dpfnx4"
set "eptype=%%~5"

use a short batchfile (in fact it is very short: one line) (you could name it "DoFile.bat")
#file.bat %1 %2 %3 %4 %5 Y "250" Y Y 3 N
It takes 5 parameters and starts the wanted batch with the full amount of 11 parameters.
It might be neccessary to adapt it a little bit to the needs of file.bat
(no errorhandling included)

Related

Checking parameters for information

I have a Script that reads other .bat Files and counts the Commentlines. I pass the .bat file that i want to test as a parameter.
For example, this is how i start my script.
C:\User\User\Desktop>CommentReader.bat test.bat
Right now test.bat needs to be in the same folder with the Skript.
I want the option that I can pass multiple .bat files that need testing and the option that I can pass the path too. for example:
C:\User\User\Desktop>CommentReader.bat D:\testfolder\test1.bat E:\test2.bat test3.bat
I also want to pass commands for example /l so the script also reads empty lines.
C:\User\User\Desktop>CommentReader.bat /l D:\testfolder\test1.bat E:\test2.bat test3.bat
I know how I can code that it should also read empty lines but
what is the best way to go through my parameters and check the information?
My Idea was something like this:
FOR /f tokens=1,2,3,4,5,6,7,8,9, delims= " %%a in (%*) DO (
)
But maybe there is a better way?
As Stephan stated in the comments section, SHIFT is what you're looking for. This script will loop through all parameters and echo them out. Just replace the ECHO command with your actual code.
#ECHO OFF
:LOOP
IF NOT "%1"=="" (
ECHO %1
SHIFT
GOTO LOOP
)
Your first parameter is always %1, the second one is %2 and so on. The SHIFT command will "forget" the original %1 and decrease all indices by 1, so %2 will become %1, %3 will become %2 and so on. As soon as you reach the last argument SHIFT will turn %1 into an empty string so that you beak out of the loop.

pass an array or list to .bat file to concatenate

I am trying to pass an array to a conca.bat file so I can concatenate all the strings that list or array has.
I have been able to concatenate, buty just if I put the Array by hand, but I am not able to do it when I pass ii from command line...
It works:
FOR %%i IN (12346,49874,48954) DO call :concat %%i
set var=%var%%1;
Doesn´t work
FOR %%i IN %1 DO call :concat %%i
set var=%var%%1;
What is the structure I must follow from the command prompt?
conca.bat "12346,49874"
or
conca.bat {12346,49874}
There are no real lists or arrays in CMD. However, if I've understood your question correctly, you are trying to do something like this:
concat.bat 123 456 789
and want the output to be 123456789. If this is the case, SHIFT is the magic command you are looking for. This should do it:
#ECHO OFF
SET concatString=
:LOOP
IF [%1]==[] GOTO ENDLOOP
SET concatString=%concatString%%1
SHIFT
GOTO LOOP
:ENDLOOP
ECHO %concatString%
PAUSE
When you pass parameters to a bat file via command line, they are accessible via %1, %2, %3 and so in. This means for concat.bat a b c that %1 is a, %2 is b and %3 is c. The only problem is that we might not know how many parameters there will be and you want your script to work with only one parameter as well as with 100 of them. At this point SHIFT is saving the day.
SHIFT does one simple thing. It moves the index of the parameters to the right, so the "old" %1 disappears, %2 becomes %1, %3 becomes %2 and so on. We just keep on looping until the last passed parameter becomes %1. After another shift there is no value left which can be assigned to %1 so [%1]==[] becomes true and we jump out of the loop.

Is there a way to re-set the first batchfile argument?

I'd like to do this:
set %1="html"
But it doesnt work, I get back:
'"html"' is not recognized as an internal or external command,
operable program or batch file.
%1 is not a variable that you assign with SET. It represents the first argument to a called batch script (test.bat arg1 or call test.bat arg1) or subroutine (call :subroutine arg1).
If you want to change the value of %1, then you do so where you make the CALL, not within the CALLed script/routine.
There are situations where a line set %1=value is used. You might have a routine that computes a value and then stores the result in a variable whose name is retrieved from the first argument to the routine.
But if the routine is called without an argument, then the line expands to set =value, which generates the error that you are getting.
If you are working with the %1 argument, and conditionally want to "change the value" within the script, then you must transfer the value to an environment variable, and work with the variable from that point on.
A trivial example would be to provide a default value if the argument is not passed:
#echo off
setlocal
set "arg1=%~1"
if not defined arg1 set "arg1=default"
REM From now on you never refer to `%1` but use `%arg1%` instead.
Take a look at this:
#ECHO OFF
Setlocal EnableDelayedExpansion
SET 1="html"
echo %1%
echo !1!
PAUSE
Executing this code with parameter abc will return
abc
html
Obviously you need Setlocal EnableDelayedExpansion to be able to "overwrite" %1. However, this won't actually overwrite %1 but make it's updated value accessable. AFAIK there is no way to really overwrite it. There is only one workaround I can think of:
Call your file with "html" as second parameter and use SHIFT:
ECHO %1
SHIFT
ECHO %1
Executing this code with params X and html (yourfile.bat X html) will print:
X
html

batch file - pop up with optional arguments

First time poster.
I use a program called PDW. It scans pager frequencies and displays pager messages on the screen.
One of the functions is that it can run a batch or COM file when it pics up a message to a specific pager.
It also has the function to be able to pass through "optional arguments" to the batch or COM file.
.
This is what it says in the help document of the program;
Command file
The command file lets you run an external program file (with optional arguments) when
you receive a filter match and the filter has the “Enable filter command file” enabled.
This program file can be any executable file or a “.BAT” or “.COM” file.
“Browse” lets you browse for a filename, or you can just type in a filename directly.
In the “arguments” control you can enter the arguments to be passed to the command
file.
These can be anything you like or any of these special character sequences :
%1 Address
%2 Time
%3 Date
%4 Mode
%5 Type
%6 Bitrate
%7 Message
%8 Label
For example : “%1” “%2” “%3” “%7” will expand to something like :
1234567 15:21:44 01-02-10 This is a TEST message
Currently I have the following batch file set up (found on stackoverflow), which creates a popup but does not display the optional arguments...
>#echo off
echo code=Msgbox(" ALERT!", vbYesNo, "TRIGGER ALERT") > "%temp%\popupBox.vbs"
echo WScript.Quit code >> "%temp%\popupBox.vbs"
cscript /nologo "%temp%\popupBox.vbs"
if %errorlevel%==6 call :ok_tag
if %errorlevel%==7 call :cancel_tag
echo Done!
exit /b 1
:ok_tag
echo You pressed Yes!
exit /b
:cancel_tag
echo You pressed No!
exit /b
What I want to do, is have a file that not only creates a pop up on my screen, but also can receive these optional arguments from the program (Specifically %1 Address, %2 Time, %3 Date, and %7 Message).
So can anyone help me with a code that will do what I want?
This change should give you extra info in the popup
echo code=Msgbox(" %~1 - %~2 - %~3 - %~7 ", vbYesNo, "TRIGGER ALERT") > "%temp%\popupBox.vbs"

call ant script in windows cmd with parameters and redirect output

I'm trying to call a windows cmd script that runs ant with a variable number of parameters and redirect the output to a logfile. The reason why I call the script that runs ant is because then I ant returns the control the my calling script in which I can proceed with other stuff. My call fails on the redirect at the end, it looks like the redirect is seen as a parameter.
The call look like this:
call ant_call ant -buildfile %BUILDDIR%\cadis\cm\build.xml -Dcvs.tag=%1 deploy_cmdscripts >> %LOGFILE% 2>&1
Since I have an unknown number of parameters the ant script looks like this
%1 %2 %3 %4 %5 %6 %7 %8 %9
Is there an easy way to solve this?
Use %* instead of %1 %2 %3 %4 %5 %6 %7 %8 %9.
%* contains all parameters from the command line, even more than nine.

Resources