0

I am starting my vb.net console application .exe from within a batch file, such as this: (timeout used for test purposes)

@Echo off echo Starting Script start C:\Users\user\Desktop\ConsoleApplication2 timeout /t 8 /nobreak start C:\Users\user\Desktop\ConsoleApplication2 

When the vb.net console application is run, a process name, description, date/time and computer name are passed in and inserted into the sql database. At the moment however, only the date/time (done in sql) and computer name are correct as they are done automatically, whereas the name and description would be user input. I have tried to use command line arguments for at least the name, but seeing as the vb application is running from the batch file, it didn't work. So now I am wondering if there is any other way I could get the Name and Description inputs (without altering the time/waiting for input as the timestamps are crucial) or if anyone had any suggestions?

VB.net code:

Imports System Imports System.Data Imports System.Data.SqlClient Imports Console = System.Console Module Module1 Sub Main() 'Connection to server Dim conn As New SqlClient.SqlConnection conn.ConnectionString = ("Server Connection Info...;") conn.Open() 'Calling Stored Procedure Dim objCommand As New SqlClient.SqlCommand objCommand.Connection = conn objCommand.CommandText = "ProcessLog_Insert" objCommand.CommandType = CommandType.StoredProcedure 'Pass in each of the required parameters objCommand.Parameters.Add("ProcessName", SqlDbType.NVarChar).Value = "Test3" 'Pass in a description of the process being run objCommand.Parameters.Add("ProcessDescription", SqlDbType.NVarChar).Value = "Desc" 'Pass in the Computer name used by current machine objCommand.Parameters.Add("ComputerName", SqlDbType.NVarChar).Value = Environment.MachineName 'Execute the Query objCommand.ExecuteNonQuery() 'Closes the Connection conn.Close() conn = Nothing objCommand = Nothing End Sub End Module 
3
  • So, you're saying the Users are going to be entering the information needed, but since the batch file executed your app, you can't save this stuff to the database and use it? Commented Apr 20, 2015 at 14:45
  • Well I want to collect this information for wherever the app is placed within a batch file, as it will be used in numerous scripts. The computer name and date/time are good to go, but I have no way to get the name or description at the moment, as you can't have vb magically know which bat file is running it (that would give me the name). So yeah I can't get those 2 fields at the moment. Commented Apr 20, 2015 at 14:53
  • From the information given, we can't tell what data you are using for the description, so we're not sure how to help. Where is the description coming from? If the user can enter this information when they run the batch file, then it can be passed as a parameter to the batch file, and then the batch file can pass it as a parameter to the vb application. Commented Apr 20, 2015 at 18:01

1 Answer 1

0

Within a batch script, %~f0 will return the path and filename of the batch file. When you launch the application, use %~f0 to pass the batch filename to the .exe. In your VB application, you just have to get the argument that was passed in the command line.

Finding out the file name of the running batch file

So if I'm understanding you correctly, the description can be passed into the batch file as a parameter, and the batch file can pass the description and the script name to the vb app.

c:\temp\test.bat "My Lovely Description"

And in your batch file:

c:\vbapp.exe "%1" "%~f0"

Handling command line arguments in VB is pretty straightforward, but if you need more help just comment accordingly.

Sign up to request clarification or add additional context in comments.

5 Comments

When I input without brackets (batfile.bat "hello") in cmd, I'm given the file's path and hello on the same line, how would I go about incorporating the "hello" or description as a second parameter? I'm unsure of how to use %2 - %9 as other parameters and i've read up on them.. Echo off C:\Users\user\Desktop\ConsoleApplication2 "%1" "%~f0" echo message timeout /t 1 /nobreak timeout /t 1 /nobreak echo message timeout /t 1 /nobreak C:\Users\user\Desktop\ConsoleApplication2 "%1" "%~f0" pause
When the batch file runs "C:\Users\user\Desktop\ConsoleApplication2 "%1" "%~f0", it will pass %1 to the vb app as an argument, which can be accessed with My.Application.CommandLineArgs(0). The batch filename, which is passed with "%~f0" can be accessed with My.Application.CommandLineArgs(1). I don't really understand what you are saying when you say they are "on the same line." To see what's happening, make the very first code in your vb app show the two parameters that were passed in. MsgBox(My.Application.CommandLineArgs(0)) MsgBox(My.Application.CommandLineArgs(1)).
Then run the batch file c:\whatever\whatever.bat "MyDescription". The batch file will then run C:\Users\user\Desktop\ConsoleApplication2 "%1" "%~f0" , which will pass "MyDescription" and the batch file name to the vb app, and they can be access as MsgBox(My.Application.CommandLineArgs(0)) and MsgBox(My.Application.CommandLineArgs(1)).
Aha! My.Application.CommandLineArgs(0) is just what I needed! As for the description, I may just write it all in 1 line using _'s as spaces, and then change that later. Thank you so much for helping me~!
Glad I could help! Please mark as answer so that I get some gratification for the effort. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.