locked
How do you run external bat files serially without exe changing the path? RRS feed

  • Question

  • My VB exe is in a folder with a subfolder containing a bat file. That bat file changes directory and calls another bat file which runs and then changes to another directory to run another bat file. This happens several times. When I run my exe, it runs the first bat file but the path stays the path of the exe so when the first bat file runs it can't change directories to the next bat file because it's starting from the parent folder of the exe and not the bat file (starts from the wrong directory). How do I stop it from doing this/get it to use the path of the bat files?

    I'm using 

    Process.Start(".\subfolder\batchfile.bat")



    • Edited by fellowtradr Wednesday, March 27, 2019 8:19 AM
    Wednesday, March 27, 2019 8:17 AM

Answers

  • Hi,

    Because the process has a working directory concept. For example, the bat directly writes the file name, which is relative to the working directory of the process, not the file directory where bat is located; if you use Process.Start directly, the working directory is your own exe directory, which causes the startup. The process failed to find a relative path.

    You can try the following code:

    Imports System.IO
    
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Dim batPath As String = Environment.CurrentDirectory & "\subfolder\batchfile.bat"
            Dim pro As Process = New Process()
            Dim file As FileInfo = New FileInfo(batPath)
            pro.StartInfo.WorkingDirectory = file.Directory.FullName
            pro.StartInfo.FileName = batPath
            pro.StartInfo.CreateNoWindow = False
            pro.Start()
        End Sub
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by fellowtradr Wednesday, March 27, 2019 6:59 PM
    Wednesday, March 27, 2019 8:50 AM

All replies

  • Hi,

    Because the process has a working directory concept. For example, the bat directly writes the file name, which is relative to the working directory of the process, not the file directory where bat is located; if you use Process.Start directly, the working directory is your own exe directory, which causes the startup. The process failed to find a relative path.

    You can try the following code:

    Imports System.IO
    
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Dim batPath As String = Environment.CurrentDirectory & "\subfolder\batchfile.bat"
            Dim pro As Process = New Process()
            Dim file As FileInfo = New FileInfo(batPath)
            pro.StartInfo.WorkingDirectory = file.Directory.FullName
            pro.StartInfo.FileName = batPath
            pro.StartInfo.CreateNoWindow = False
            pro.Start()
        End Sub
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by fellowtradr Wednesday, March 27, 2019 6:59 PM
    Wednesday, March 27, 2019 8:50 AM
  • Maybe begin batchfile.bat with this line:

       cd /D %~dp0

     

    Wednesday, March 27, 2019 9:37 AM
  • This appears to work and it runs the second batch file, but doesn't look like it makes it to the next one because of a problem with the bat. There's a set path line that adds %PATH% at the end that breaks it and produces a "\Windows was not expected at this time" error which I'm guessing has something to do with the way windows has stored the path environment variable, but I am able to remove that and use a local path no problem.

    This has been very helpful, thank you.

    As for changing the bat files as Violrel_ suggested I think that would work (haven't tested it) but I was trying to make no changes to the existing bat files so I only have to distribute an exe that gets dropped in a parent folder. It looks like I may have no choice because of the path error from the bat described above, but I can always overwrite the bat dynamically with a corrected version. 

    Thanks again, I spent hours searching for answers and testing but didn't find anything as clear as what has been provided here so far.


    • Edited by fellowtradr Wednesday, March 27, 2019 4:51 PM updated with final results
    Wednesday, March 27, 2019 3:59 PM