Home / Microsoft / Microsoft DOS / Batch file to detect close program and rename.
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] 2  All - (Bottom) Print
Author Topic: Batch file to detect close program and rename.  (Read 571 times)
Mortifer
Topic Starter
Starter



Posts: 4

Experience: Beginner
OS: Unknown

« on: February 01, 2012, 12:36:47 PM »

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, .

regard
IP logged
Geek-9pm
Sage



Thanked: 373
Posts: 8,928

Computer: Specs
Experience: Expert
OS: Windows XP


Geek After Dark

Geek 9pm blog
« Reply #1 on: February 01, 2012, 01:00:35 PM »

Turn on delayed execution. Otherwise things get out of order.
IP logged

Salmon Trout
Sage



Thanked: 546
Posts: 7,950

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #2 on: February 01, 2012, 01:00:46 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
IP logged


Proud to be European
Squashman
Hopeful



Thanked: 25
Posts: 342

Experience: Experienced
OS: Other



« Reply #3 on: February 01, 2012, 01:28:42 PM »

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.
IP logged
Salmon Trout
Sage



Thanked: 546
Posts: 7,950

Computer: Specs
Experience: Beginner
OS: Unknown

1
« Reply #4 on: February 01, 2012, 01:33:50 PM »

squashman, you have a very good point.

IP logged


Proud to be European
Mortifer
Topic Starter
Starter



Posts: 4

Experience: Beginner
OS: Unknown

« Reply #5 on: February 01, 2012, 02:43:21 PM »

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 again
« Last Edit: February 01, 2012, 02:56:54 PM by Mortifer » IP logged
Squashman
Hopeful



Thanked: 25
Posts: 342

Experience: Experienced
OS: Other



« Reply #6 on: February 01, 2012, 03:31:54 PM »

Well 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.
IP logged
Squashman
Hopeful



Thanked: 25
Posts: 342

Experience: Experienced
OS: Other



« Reply #7 on: February 01, 2012, 06:20:10 PM »

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 waits
IP logged
Squashman
Hopeful



Thanked: 25
Posts: 342

Experience: Experienced
OS: Other



« Reply #8 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.
IP logged
Mortifer
Topic Starter
Starter



Posts: 4

Experience: Beginner
OS: Unknown

« Reply #9 on: February 02, 2012, 07:07:28 PM »

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  ;D
IP logged
Squashman
Hopeful



Thanked: 25
Posts: 342

Experience: Experienced
OS: Other



« Reply #10 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
IP logged
Geek-9pm
Sage



Thanked: 373
Posts: 8,928

Computer: Specs
Experience: Expert
OS: Windows XP


Geek After Dark

Geek 9pm blog
« Reply #11 on: February 02, 2012, 08:18:31 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
IP logged

Squashman
Hopeful



Thanked: 25
Posts: 342

Experience: Experienced
OS: Other



« Reply #12 on: February 02, 2012, 09:38:25 PM »

OK. But where is WaitForexe.vbs
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.
IP logged
Geek-9pm
Sage



Thanked: 373
Posts: 8,928

Computer: Specs
Experience: Expert
OS: Windows XP


Geek After Dark

Geek 9pm blog
« Reply #13 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 If
No quite so simple. The thing giving by Salmon Trout is more simple.
IP logged

Squashman
Hopeful



Thanked: 25
Posts: 342

Experience: Experienced
OS: Other



« Reply #14 on: February 03, 2012, 05:21:31 AM »

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 If
No 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.
 ::)
IP logged
Pages: [1] 2  All - (Top) Print 
Home / Microsoft / Microsoft DOS / Batch file to detect close program and rename. « previous next »
 


Login with username, password and session length

Old Forum Search | Forum Rules
Copyright © 2010 Computer Hope ® All rights reserved.
Powered by SMF 2.0 RC3 | SMF © 2006–2010, Simple Machines LLC
Page created in 0.132 seconds with 20 queries.