Solved

How to pass parameters from batch file to vb.net program?

Posted on 2008-10-04
7
1,295 Views
Last Modified: 2012-06-27
The program is vb.net 2005, and I run it from a dos batch file.  Now I need to optionally send a parameter that the vb program can recognize and work with.  For example, in my batch file I might call the program with a parameter like this:

call ecotpc.exe ABC

And, in my ecotpc vb program, I'd like to be able to handle the optional parameter something like this:

If %1 = "" then null routine...
else
psWork1 = %1

The problem is that I don't know the syntax to "capture" that variable, and I don't know what extra code I might need to add to the top section of my code (such as "imports", etc.)

Any clear, detailed example would be greatly appreciated!  TIA
0
Comment
Question by:sasllc
  • 5
  • 2
7 Comments
 
LVL 2

Expert Comment

by:madivad2
ID: 22641837
Ahhh, it's been ages since I tried something like this. VB6 was something like Command$, in .Net look at the values contained in Environment.GetCommandLineArgs()...

looking, here is a link to two methods of achieving what you are after. I believe the second way is easiest, but overriding main is just as good. If you are using a form, you will have to rely on method 2

http://www.vbdotnetheaven.com/UploadFile/mahesh/CmdLineArgs02052007003318AM/CmdLineArgs.aspx


'Method 1 - override Main'
 

Shared Sub Main(ByVal args As String())

    For Each arg As String In args

        Console.WriteLine(arg)

    Next arg 

    Console.ReadLine()

End Sub
 

'Method 2'

For Each arg As String In Environment.GetCommandLineArgs()

    Console.WriteLine(arg)

Next arg

Open in new window

0
 
LVL 3

Author Comment

by:sasllc
ID: 22642343
OK, I almost have it working, but I need additional specifics.  Using method 2 above, I changed the "console.writeline" to messagebox.show(arg).  And then, if the batch file command line says "call ecotpc.exe ABC", I get two message boxes...the first one says "ecotpc" (the program name), and the second one says "ABC" (the parameter I keyed in)--and the "ABC" is the parameter I'm interested in.  

So how can I specifically access only the "ABC" in this case and place it in a variable such as psWork1?  I tried "psWork1 = arg(2)" (hoping I would get the 2nd string), which does not work, but I don't know what else to try.

I'm trying to get to something simple like this concept:

If arg = "" then null routine...
else
psWork1 = arg

I know I will only have one parameter, at most, and I need to know how to "get to" that one parameter--such as "ABC" in this test I ran.
0
 
LVL 2

Expert Comment

by:madivad2
ID: 22643178
in which case arg(0) is the command line itself (minus arguments), arg(1) is the 1st argument, arg(2) is the 2nd and so on...


' for example '

MsgBox(Environment.GetCommandLineArgs.Count) 

' will always be 1 more than the number of parameters - in your case, it will be 2 '
 

' Option 1 - will always return the last paramater '

MsgBox(Environment.GetCommandLineArgs(Environment.GetCommandLineArgs.Count - 1))
 

' Option 2 - will always return the 1st parameter '

MsgBox(Environment.GetCommandLineArgs(1))
 

' since you only have 1 parameter, they will return the same value '

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:madivad2
ID: 22643189

If Environment.GetCommandLineArgs.Count = 1 then 

 ' null routine... 1 = command line only, 2 means we have parameter '

else

 psWork1 = Environment.GetCommandLineArgs(1)

end if

Open in new window

0
 
LVL 3

Author Comment

by:sasllc
ID: 22643994
When I try to use this syntax:

If Environment.GetCommandLineArgs.count = 1 Then

I get this error: 'count' is not a member of 'System.Array'.  It gives me a long list of other options after the '.' -- which I don't know how to use -- but 'count' is not one of them.  Any ideas?

And if not, I have been able to do a workaround based on your information.  But it would be nice to able to use "clean code" like you've shown instead--if I could get .count to work.
0
 
LVL 2

Accepted Solution

by:
madivad2 earned 500 total points
ID: 22644286
I'm wondering if that is a 2005 limitation, I am using 2008 and it's available.

You can use Length as an alternative, I think it translates the same.
If Environment.GetCommandLineArgs.Length = 1 then 

 ' null routine... 1 = command line only, 2 means we have parameter '

else

 psWork1 = Environment.GetCommandLineArgs(1)

end if

Open in new window

0
 
LVL 2

Expert Comment

by:madivad2
ID: 22644298
I have verified the above works, and it seems that 2008 (for some reason) is more forgiving. After doing a little research I have found that Environment.GetCommandLineArgs is an Array and not a collection, hence that is why you are getting the error. As a rule (as I have found out) Length is used for Arrays, and Count is used for Collections. Still not sure why it works for me though
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now