B4J Question Executing a .bat file doesn't do anything

jmon

Well-Known Member
Licensed User
Longtime User
Hello, I need your help!

Not sure if I didn't sleep well or what, but my script here doesn't do anything and I cannot see the error!
I need to do a restart function in my application. So I create a .bat script that delays for 5 seconds before launching the application.

B4X:
Dim Script As String = $"@echo off
echo Restarting Notepad
echo Waiting for 5 seconds...
timeout /t 5 /nobreak > NUL
echo Starting the application...
start "C:\WINDOWS\system32\notepad.exe"
exit
"$

Dim BatchFile As String = File.Combine("C:\", "NotepadRestarter.bat")
File.WriteString(File.GetFileParent(BatchFile), File.GetName(BatchFile), Script)

If File.Exists(File.GetFileParent(BatchFile), File.GetName(BatchFile)) Then
    Dim sh As Shell
    sh.Initialize("Restart", BatchFile, Null)
    sh.Run(1000)

Else
    LogError("Restart failed")
End If

What I end up with is this:
Capture.PNG


So it looks like it's only opening cmd, but not executing the bat file?

Thanks for your help!
 

PaulMeuris

Active Member
Licensed User
Do you really need a batch file to do a restart?
In this code snippet a new version of the notepad.exe program is started.
You might want to use a timer to set the number of seconds to wait.
B4X:
Private Sub Button1_Click
    For i = 0 To 2
        start_exe("notepad.exe")
        Sleep(5000)  
    Next
End Sub
Private Sub start_exe(fname As String)
    Dim sh As Shell
    sh.Initialize("sh", "cmd", Array As String("/c", "C:\WINDOWS\system32\" & fname))
    sh.WorkingDirectory = "E:\_temp"
    sh.Run(-1)
    Wait For sh_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
    If Success Then
        Log("Notepad started.")
    End If
End Sub
Is this what you wanted to do?
 
Upvote 0

jmon

Well-Known Member
Licensed User
Longtime User
Thanks for your help!! I was doing something similar before, but I need the 5 second timeout before starting the new instance.
This is because my application first verifies if another instance is running.

Sorry, in my example please read "myApp.exe" instead of "notepad", as the goal is to restart the application

The expected result is:
  • Application is running
    • User clicks the restart button
    • Execute the "Restart.bat"
    • Close the application, i.e. close MySQL connection etc...
  • The .bat waits 5 seconds for the previous process to close
  • The .bat opens a new instance of the application
    • The new instance verifies if no other instance is running
  • Application is restarted successfully
The following doesn't really work for me, as sometimes it takes a few seconds to close the previous instance:
  • Application is running
    • User clicks the restart button
    • Execute the APPLICATION
    • Close the application, i.e. close MySQL connection etc...
  • The new instance opens itself
    • The new instance verifies if no other instance is running
  • Application is restarted successfully
 
Upvote 0

jmon

Well-Known Member
Licensed User
Longtime User
It seems that I just had to remove the "start" from the command, not sure why, but that works:

B4X:
Dim Script As String = $"@echo off
echo Restarting Notepad
echo Waiting for 5 seconds...
timeout /t 5 /nobreak > NUL
echo Starting the application...
"C:\WINDOWS\system32\notepad.exe"
exit
"$
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
It seems that I just had to remove the "start" from the command, not sure why, but that works:

B4X:
Dim Script As String = $"@echo off
echo Restarting Notepad
echo Waiting for 5 seconds...
timeout /t 5 /nobreak > NUL
echo Starting the application...
"C:\WINDOWS\system32\notepad.exe"
exit
"$
I tried this code but the notepad opens immediately. I am running on Windows 11.
 
Upvote 0

jmon

Well-Known Member
Licensed User
Longtime User
I tried this code but the notepad opens immediately. I am running on Windows 11.
same for me, still trying.

At the moment, as a workaround, I changed the behavior of my application. At startup, if another process is detected, it will wait up to 10 seconds until the process is closed to start itself.
 
Upvote 0

PaulMeuris

Active Member
Licensed User
Did you try it with an endless loop?
B4X:
@echo off
:start
"C:\WINDOWS\system32\notepad.exe"
timeout /t 5 /nobreak > NUL
goto :start
 
Upvote 0
Top