0

A batch file is running my vb.net console application. This console application connects to an SQL database and inputs things such as the date and what the process (batch file) is doing. Instead of making the using write out the name of this batch file so that it is recorded and sent to the database, I would like to make it more automatic.

  • So here is my question: Can a console application figure out the Filename/Filepath of the batch file that is starting it, without specifying it's directory within the application as this application will be run from numerous batch files that will come from different directories and the source code may not be available (just the exe).
ibg
  • 3
  • 4
  • 1
    Your entire question is very confusing. Can you pretend you're us and look at this as if we have no idea what you're talking about, and then re-word your question? – rory.ap Apr 17 '15 at 17:55
  • A batch-script file is nothing, the process is CMD.exe, that is what you should start looking at. Note that the CMD first argument is the full filepath of the batch-script. – ElektroStudios Apr 17 '15 at 18:09
  • You want to know, in your application, from which .bat file it was launched? Say, `c:\a.bat` for `c:\anotherdir\b.bat`? – hometoast Apr 17 '15 at 18:10
  • 2
    You can always pass information in by using Command Line args. https://social.msdn.microsoft.com/Forums/en-US/b8ce54b3-42e2-4dee-a449-72b3093225c7/vbnet-environmentgetcommandlineargs-capturing-each-argument-in-a-textbox?forum=vbgeneral – Mark Hall Apr 17 '15 at 18:10
  • Yes, I want to know the filepath of the .bat file that launched the application, as this will give me the Script's name, which I can then push to a database. – ibg Apr 17 '15 at 18:12
  • It sounds like you want similar function of using `$0` in a bash script. You're not going to get that same information from a batch file. – hometoast Apr 17 '15 at 18:19
  • possible duplicate of [How to get the path of the batch script in Windows?](http://stackoverflow.com/questions/3827567/how-to-get-the-path-of-the-batch-script-in-windows) – Peter M Apr 17 '15 at 18:20
  • How is this a duplicate? I'm trying to do this within a VB.net Console Application, not within the batch file.. – ibg Apr 17 '15 at 18:22
  • 1
    Your .NET app cannot magically deduce the name or location of the batch file that invoked it, but your batch file *can* use the technique(s) in the article that @PeterM cited and then pass that information to the .NET app as a command-line parameter. – Gord Thompson Apr 17 '15 at 18:30
  • I get how Command Line args would work (like if I put the file name of the application into cmd followed by "hello", I could pass hello to the application. However, the batch file is running the application (more than once) and if i try to put that batch file's full filepath first followed by an argument, I get System.IndexOutOfRangeException.. – ibg Apr 17 '15 at 18:38
  • We can't help you without seeing your code. – Peter M Apr 17 '15 at 19:27

0 Answers0