Solved How to store values of arguments in a batch variable ?

July 3, 2019 at 13:18:13
Specs: Windows 10
Hi Team,

I am calling a batch file using c sharp code and also passing 2 arguments to the batch file using c sharp code.

I wanted to know how I should get the values of these 2 parameter/arguments, inside the batch file ? How I can store the values of these arguments using batch variable inside batch file ?

Please let me know.

Thank you.
Sushil Sahu


See More: How to store values of arguments in a batch variable ?

Reply ↓  Report •

#1
July 3, 2019 at 13:36:09
✔ Best Answer
%1 = First argument
%2 = Second argument
.
.
.
%9 = Ninth argument

How To Ask Questions The Smart Way

message edited by Razor2.3


Reply ↓  Report •

#2
July 4, 2019 at 00:46:39
Hi,

Thank you for your reply. However, below is my code -

proc = System.Diagnostics.Process.Start(@"C:\Users\SushilSahu\Desktop\ISM_MergeModules.bat");
proc.StartInfo.Arguments = string.Format(value, tmFolderName);
proc.Start();

I have created an object of process and I am passing 2 arguments to my batch file using string.Format. I have to save the values of these 2 arguments inside batch file in some variables.
So, I wanted to know how I could save these argument's values inside my batch file in some variable. So that I can further use those variables in some operations in my batch file ?

Please let me know.

Thank you.
Sushil Sahua


Reply ↓  Report •

#3
July 4, 2019 at 02:29:23
you'll have to start the .bat like so(or in your c sharp method):

start test.bat variable1 variable2

then from inside the test.bat(example):

@echo off
::catching incomming vars:
if "%1%2"=="" echo no variables caught...&pause>nul&exit/b
set "receivedvar1=%1"
set "receivedvar2=%2"
::below lines can be removed, it's just for showing the reseult
echo %receivedvar1%
echo %receivedvar2%
pause
::place your code here

i5-6600K[delid]@4.9GHz/4.4GHz cache@1.504v | 2x4GB Crucial-DDR4-2400CL17@CL12-15-15-28 1T 3000MHz@1.45v | Sapphire Nitro+ SE RX 590 8GB@1450Mhz core@0.925v/2250MHz@1500MHz timings BiosModded


Reply ↓  Report •

Related Solutions

#4
July 4, 2019 at 03:30:54
Hi Hidde663,

Thank you for your reply. Let me try these things in my code, will let you know how it goes.

Regards,
Sushil Sahu


Reply ↓  Report •

#5
July 4, 2019 at 06:50:54
Hard to tell what's happening without knowing the value of value in your format, but I will remind you String.Format() is not String.Join().

How To Ask Questions The Smart Way

message edited by Razor2.3


Reply ↓  Report •

#6
July 9, 2019 at 00:34:49
Hi Hidde663,

Thank you so much for your last reply, it helped me a lot.
Thank you Razor2.3, my issue issue is resolved for now.

Now I am trying 1 thing under the batch file. It is related to IF statements. As below -

IF %merge%=="DatacapTM_App_APT.ism"
"C:\Program Files (x86)\InstallShield\2013\System\ISCmdBld.exe" -p "C:\InstallShield 2013 Projects\%tm%\DatacapTM_App_APT\DatacapTM_App_APT.ism"

like above execution of .ism file, I have multiple .ism file to be executed based on the value in IF condition. There are total 28 .ism files I have to execute, but , that will depend on the value of IF condition.

How I have written my scripts in the batch is -

IF Condition 1
Execute script 1

IF Condition 2
Execute script 2

IF Condition 3
Execute script 3

But I guess, something is wrong in IF conditions, because, none of the scripts getting executed.

Am I doing anything wrong in IF. Do I need to use IF Then or IF Else ?

Please let me know.

Thank you.

Regards,
Sushil Sahu


Reply ↓  Report •

#7
July 9, 2019 at 18:24:18
IF %merge%=="DatacapTM_App_APT.ism"
"C:\Program Files (x86)\InstallShield\2013\System\ISCmdBld.exe" -p "C:\InstallShield 2013 Projects\%tm%\DatacapTM_App_APT\DatacapTM_App_APT.ism"

If everything is consistent in your format, to eliminate IF, maybe:
:: this just gets the path by removing .ism then appending the executable .ism
for %%a in (%merge%) do set pth=%%~na\%%a
echo path to ex: %%~na
echo ism executed: %%a
echo executed: "C:\Program Files (x86)\InstallShield\2013\System\ISCmdBld.exe" -p "C:\InstallShield 2013 Projects\%tm%\%pth%

"C:\Program Files (x86)\InstallShield\2013\System\ISCmdBld.exe" -p "C:\InstallShield 2013 Projects\%tm%\%pth%

::--- end batch snip - not tested.


Reply ↓  Report •

#8
July 10, 2019 at 05:15:06
Hi nbrane,

Thank you for your comment, however, the .ism file is getting executed properly when I execute it as a single statement, without the IF statement.

But when I put IF conditions then it doesn't executes. I feel there might be something wrong in my IF syntax but I am not sure.

IF %merge%=="DatacapTM_App_APT.ism"

The value of %merge% would be string, do I need to put "DatacapTM_App_APT.ism" under double quotes in IF condition?

Or something else is wrong there?

Please let me know.

Thank you.

Regards,
Sushil Sahu


Reply ↓  Report •

#9
July 10, 2019 at 07:46:16
Few things:
1) The entire thing has to be on one line: IF Condition 1 Execute script 1
As written the IF fails syntax check, and ISCmdBld is called unconditionally.

2) Double quotes are included in the evaluation, so the following does not evaluate to true:

IF FooBar=="FooBar" ECHO.You will never see this output

How To Ask Questions The Smart Way


Reply ↓  Report •

#10
July 10, 2019 at 19:46:26
I was trying to get away from the "IF"s since that seemed overly complicated and redundant. Since a variable can be executed directly, I thought maybe this could serve instead of the "IF" ladder. for a simple example:
set a=dir /s /b
echo executing %a%:
%a%
::----- end sample bat
So if you have a set of .ism files, this would be the aim I had in mind for post #7:
for /f "tokens=*" %%a in ('dir /b /s *.ism') do (
echo executing:"C:\Program Files (x86)\InstallShield\2013\System\ISCmdBld.exe" -p "C:\InstallShield 2013 Projects\%tm%\%%a"
pause
"C:\Program Files (x86)\InstallShield\2013\System\ISCmdBld.exe" -p "C:\InstallShield 2013 Projects\%tm%\%%a"
)
probably to be run from the %tm% directory, but that was never clarified or referred to, so I don't know how to set this up exactly. I believe the concept should work, it's just a matter of tuning the paths.

Reply ↓  Report •

#11
July 12, 2019 at 05:50:37
Hi @Razor 2.3,

Thank you so much for your reply, now my IF Conditions are working fine.

@nbrane - Thank you for your reply too, I guess, its a good suggestions, I would definitely apply it, somewhere in the script ahead.

Thank you again.

Regards,
Sushil Sahu


Reply ↓  Report •

Ask Question