1.

Solve : Batch file to detect close program and rename.?

Answer»

Hi gents

I'm making a batch file to change a file extension "jogger" to "jogger.pack" Then run an third-party program "runner.exe" that using "jogger.pack" file.
What I got so far:
Code: [Select]ren Jogger Jogger.pack
runner.exe
Pause

While Runner.exe is being use, jogger.pack remain the same. After the Runner.exe closed, I need to change the "Jogger.pack" filename back to just "Jogger" again. The cycle repeat of adding extension, run program, and removing extension whenever the user running the bat file.

I don't know how to detect when "runner.exe" closes to do the remove naming.

If possible, perhaps to write code that remove the extension when user close the bat file command window, .

regardTurn on delayed execution. Otherwise things get out of order. Quote from: Mortifer on February 01, 2012, 12:36:47 PM

I don't know how to detect when "runner.exe" closes to do the remove naming.

Code: [Select]:loop
REM Wait 60,000 milliseconds (1 second)
PING 1.1.1.1 -n 1 -w 60000 >NUL
REM check tasklist to see if runner.exe is running
REM if it is go to :loop
tasklist | findstr /I "runner.exe" > nul && goto loop
REM you get here after runner.exe stops
REM do your rename stuff NEXT
I guess I am confused on this.   Batch files are sequential processing.
It will not go on to the next command until the PREVIOUS one completes.
At least I have never run into an instance of this not happening.
Not sure why you couldn't use the START command with the wait switch either.
So I guess I am not sure why we would need to use tasklist to see if it is still running.
squashman, you have a very good point.

Hi gents

I'm new at this so thank you very much for the speedy response 
The code Salmon provided works superbly.

I tried messing around with Squash Start /wait switch too. However, I later found out that it doesn't work on 32-bit GUI app which "runner.exe" unfortunately is.


Thank you all againWell I am pretty sure Notepad is a 32bit gui application and I have posted examples on this very forum executing notepad with and without notepad and the batch file paused each time until I closed notepad.Well this certainly is INTERESTING.  It is KIND of a mixed BAG of  what applications will wait and which ones will not.
Seems like all the Microsoft apps I throw at it will wait.
Chrome will not.
Firefox waits.
itunes waits
Skype waitsFound a few more things.
http://www.ericphelps.com/scripting/samples/index.htm#Run
You can use the Wait For Title or Wait for Exe VBscripts to pause your script.
Although I guess it really doesn't hurt to do keep polling for the process with Tasklist.  Especially if you want to keep it pure batch.
I tried it on Runner.exe and it won't wait . The wait switch would have been lovely as users might run the program for a large amount of time and i'm not sure what 700+ checks might do to performance.

Ye, VBscript will likely give me a brain aneurysm. Perhaps if i'm 20 years younger I might try to learn it 
The Vbscript is a no brainer to use.  All you do is download it and use.  Nothing to edit inside the code at all.

You would use it just like this.
Code: [Select]ren Jogger Jogger.pack
runner.exe
WaitForexe.vbs runner.exe
ren Jogger.pack Jogger Quote from: Squashman on February 02, 2012, 07:56:19 PM
The Vbscript is a no brainer to use.  All you do is download it and use.  Nothing to edit inside the code at all.

You would use it just like this.
Code: [Select]ren Jogger Jogger.pack
runner.exe
WaitForexe.vbs runner.exe
ren Jogger.pack Jogger
OK. But where is WaitForexe.vbs Quote from: Geek-9pm on February 02, 2012, 08:18:31 PM
OK. But where is WaitForexe.vbs
Quote from: Squashman on February 01, 2012, 09:35:42 PM
Found a few more things.
http://www.ericphelps.com/scripting/samples/index.htm#Run
You can use the Wait For Title or Wait for Exe VBscripts to pause your script.
Although I guess it really doesn't hurt to do keep polling for the process with Tasklist.  Especially if you want to keep it pure batch.
From the link bySquashman
Code: [Select]On Error Resume Next
If WScript.Arguments.Count <> 1 Then
    WScript.Echo "Waits for an application to shut down. Usage:" & vbCrLf & Ucase(WScript.ScriptName) & " ""Program.exe""" & vbCrLf & "Where ""Program.exe"" is the executable name of the application you are waiting for."
Else
    blnRunning = True
    Do While blnRunning = True
        Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
        Set colItems = objWMIService.ExecQuery("Select Name from Win32_Process where Name='" & Wscript.Arguments(0) & "'",,48)
        blnRunning = False
        For Each objItem in colItems
            blnRunning = True
        Next
        WScript.Sleep 500
    Loop
End IfNo quite so simple. The thing giving by Salmon Trout is more simple. Quote from: Geek-9pm on February 02, 2012, 10:03:25 PM
From the link bySquashman
Code: [Select]On Error Resume Next
If WScript.Arguments.Count <> 1 Then
    WScript.Echo "Waits for an application to shut down. Usage:" & vbCrLf & Ucase(WScript.ScriptName) & " ""Program.exe""" & vbCrLf & "Where ""Program.exe"" is the executable name of the application you are waiting for."
Else
    blnRunning = True
    Do While blnRunning = True
        Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
        Set colItems = objWMIService.ExecQuery("Select Name from Win32_Process where Name='" & Wscript.Arguments(0) & "'",,48)
        blnRunning = False
        For Each objItem in colItems
            blnRunning = True
        Next
        WScript.Sleep 500
    Loop
End IfNo quite so simple. The thing giving by Salmon Trout is more simple.
And this is coming from a guy who sits there and recommends PowerShell to people who ask questions about batch files and never provides them a PowerShell script.  Then you see myself, Raven or Salmon come along and write the Batch file that can do it.

I am sure the batch script code from Salmon Trout was not so simple for the O/P to understand either.  I basically said the VBscript was easy to use!  I showed the BATCH code on how to use the VBscript.  How hard can that really be!  You don't have to know VBscript to use the code at all. Just download it and use it in your batch file.


Discussion

No Comment Found