user967706
user967706

Reputation: 21

Running a .bat in the background

So I have this in my coding:

vb Code:

file = My.Computer.FileSystem.OpenTextFileWriter("c:\command.bat", False)
        file.WriteLine("@echo off")
        file.WriteLine("cd " & TextBox2.Text)
        file.WriteLine("adb shell dumpsys meminfo " & TextBox1.Text & " > C:\Sample.txt")
        file.Close()
        Shell("C:\command.bat") 

what I want it to do is to run the batch file without it opening if that makes sense. Right now this runs on a loop for 10 minutes and on every 2 second tick it opens and then closes the .bat. Which is really annoying to see a .bat open and close every two seconds. Is there anyway to get this process to run silently in the background so the user doesnt even know that it is running?

Upvotes: 2

Views: 8728

Answers (2)

Steven Padrick
Steven Padrick

Reputation: 43

Here is an example on how to use Process.Start with a hidden window

    Dim params As String = "C:\command.bat"
    Dim myProcess As New ProcessStartInfo
    myProcess.FileName = "cmd.exe"
    myProcess.Arguments = params
    myProcess.WindowStyle = ProcessWindowStyle.Hidden
    Process.Start(myProcess)

if you run into the issue of file not found errors with the path you can try to add the following Windows API call and run your file path through this function as well.

'This would be declared at the top of your Form Code/Class Code
Private Declare Auto Function GetShortPathName Lib "kernel32" ( _
ByVal lpszLongPath As String, _
ByVal lpszShortPath As StringBuilder, _
ByVal cchBuffer As Integer) As Integer

And here is the function to return back a ShortPath (win98 style path (ie. c:/progra~1/myfolder/myfile.bat)

Public Function GetShortPath(ByVal longPath As String) As String
    Dim requiredSize As Integer = GetShortPathName(longPath, Nothing, 0)
    Dim buffer As New StringBuilder(requiredSize)

    GetShortPathName(longPath, buffer, buffer.Capacity)
    Return buffer.ToString()
End Function

then simply call your path like this in your process.start function

    Dim params As String = GetShortPathName("C:\command.bat")

Upvotes: 3

Grekoz
Grekoz

Reputation: 275

Shell("C:\command.bat", AppWinStyle.Hide)

That will run the batch file but the window is hidden.

or use Process.Start as suggested by David. with WindowStyle = ProcessWindowStyle.Hidden

Upvotes: 3

Related Questions