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
ibg
  • 3
  • 4
  • 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? – Mark C. Apr 20 '15 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. – ibg Apr 20 '15 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. – Tony Hinkle Apr 20 '15 at 18:01

1 Answers1

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.

Community
  • 1
  • 1
Tony Hinkle
  • 4,706
  • 7
  • 23
  • 35
  • 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` – ibg Apr 21 '15 at 13:22
  • 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)). – Tony Hinkle Apr 21 '15 at 13:35
  • 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)). – Tony Hinkle Apr 21 '15 at 13:37
  • 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~! – ibg Apr 21 '15 at 15:03
  • Glad I could help! Please mark as answer so that I get some gratification for the effort. :) – Tony Hinkle Apr 21 '15 at 15:07