June 16, 2024, 08:28:02 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


How to wait until a batch file has run

Started by Andy, December 22, 2008, 01:18:27 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Andy

Hi Everyone,

Can anyone help please..

My program creates a batch file with several lines to run, I the use the "System" command to execute the batch file and the "Deletefile" command to delete it afterwards.

The porblem is timing, the "Deletefile" command to delete the batch file sometimes runs before the batch file has finished, is there any way to test if the batch has finished or is still running ?

Thank you very much.
Andy.
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

Ionic Wind Support Team

See the included example program "waitforprocess.eba"

I haven't tried it on a batch file, so don't know if it will work or if Windows assigns a process handle.  But it is worth a try.

Paul.
Ionic Wind Support Team

Andy

Thanks Paul,

I will give it a try !!

Many Thanks,
Andy.
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

sapero

Andy, you can also instrct the command processor to delete the file later:
string bat = "\"" + GetStartPath() + "script.bat\""

' create the bat file
system "cmd", "/c echo pause>"+ bat

' execute it and delete: cmd /c call file.bat & del file.bat
system "cmd", "/c call "+ bat+" & del "+ bat

Andy

sapero,

Thanks very much, I will give that a try - so good to know there are people like yourself out there to give help!

Many thanks,
Andy.
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

Peter

Excuse me if I'm bumping this a little, but another method that people often are not aware of, is that you can simply add a "del <path>\batchfile.bat" to the end of your batchfile to make it delete itself. Just wanted to tell you if that would help you in any way. Depending on your code and what the batchfile does, this could maybe remove the need to wait for the batchfile at all. Less code, more speed. :)

Peter

I'm feeling a little dumb here, but I can't find that file anywhere. Am I just worthless or is this example code something I need to download separately?

Quote from: Paul Turley on December 22, 2008, 10:00:19 AM
See the included example program "waitforprocess.eba"

I haven't tried it on a batch file, so don't know if it will work or if Windows assigns a process handle.  But it is worth a try.

Paul.

Ionic Wind Support Team

Peter,
Sorry,  for some reason it wasn't included with the current installations.  Here it is.

Paul.


'how to launch a new process and wait for it to close

REM define a user type with 4 byte packing
type SHELLEXECUTEINFO,4
def cbSize:int
def fMask:int
def hwnd:int
def lpVerb:pointer
def lpFile:pointer
def lpParameters:pointer
def lpDirectory:pointer
def nShow:int
def hInstApp:int
def lpIDList:int
def lpClass:pointer
def hkeyClass:INT
def dwHotKey:int
def hIcon:int
def hProcess:int
endtype

CONST WAITTIMEOUT = 258
CONST SEE_MASK_NOCLOSEPROCESS = 0x40
def info:SHELLEXECUTEINFO
def ret:int

DECLARE IMPORT,ShellExecuteExA(info:SHELLEXECUTEINFO),int
DECLARE IMPORT,WaitForSingleObject(handle:int,ms:int),int

info.cbSize = 15 * 4
info.fMask = SEE_MASK_NOCLOSEPROCESS
info.hwnd = 0
info.lpVerb = "Open"
info.lpFile = "notepad.exe"
info.lpParameters = "\"c:\\autoexec.bat\""
info.lpDirectory = "c:\\"
info.nShow = @SWRESTORE
info.hInstApp = 0

ShellExecuteExA(info)

do
  ret = WaitForSingleObject(info.hprocess,0)
until ret <> WAITTIMEOUT

MessageBox 0,"Launched process has ended","Info"

END

Ionic Wind Support Team

Raid

Thanks for the sample code. I was looking for something along those lines.
Proudly licensed EBASIC owner since Feb 2009. Network library present!