Help writing a batch file |
Author: |
Message: |
MeEtc
Patchou's look-alike
In the Shadow Gallery once again
Posts: 2200 Reputation: 60
38 / /
Joined: Nov 2004
Status: Away
|
O.P. Help writing a batch file
Who's good with command line programming?
I'm trying to write a batch file that will automate switching a virtual KVM method with UltraMon and Synergy. basically 2 commands need to be run when either when starting or stopping (along with switching input on the screen itself).
What I don't know how to do, is how to detect if a certain program is running.
basically, this is how it needs to run:
code: if synergys.exe is running {
exit program synergys.exe
run "c:\program files\ultramon\ultramon.exe" /e
} else {
run "c:\program files\synergy\synergys.exe" -a pieceofcrap:24800 -d FATAL
run "c:\program files\ultramon\ultramon.exe" /d
}
I cannot hear you. There is a banana in my ear.
|
|
04-21-2009 03:09 AM |
|
|
Jarrod
Veteran Member
woot simpson
Posts: 1304 Reputation: 20
– / /
Joined: Sep 2006
|
RE: Help writing a batch file
IMO python would be a much better solution to this
python code: ###define function for the processes
import win32pdh,os
def get_processes():
win32pdh.EnumObjects(None, None, win32pdh.PERF_DETAIL_WIZARD)
junk, instances = win32pdh.EnumObjectItems(None,None,'Process', win32pdh.PERF_DETAIL_WIZARD)
proc_dict = {}
for instance in instances:
if proc_dict.has_key(instance):
proc_dict[instance] = proc_dict[instance] + 1
else:
proc_dict[instance]=0
proc_ids = []
for instance, max_instances in proc_dict.items():
for inum in xrange(max_instances+1):
hq = win32pdh.OpenQuery() # initializes the query handle
try:
path = win32pdh.MakeCounterPath( (None, 'Process', instance, None, inum, 'ID Process') )
counter_handle=win32pdh.AddCounter(hq, path) #convert counter path to counter handle
try:
win32pdh.CollectQueryData(hq) #collects data for the counter
type, val = win32pdh.GetFormattedCounterValue(counter_handle, win32pdh.PDH_FMT_LONG)
proc_ids.append((instance, val))
except win32pdh.error, e:
#print e
pass
win32pdh.RemoveCounter(counter_handle)
except win32pdh.error, e:
#print e
pass
win32pdh.CloseQuery(hq)
return proc_ids
x=get_processes()
if synergys.exe in x:
os.system("exit program synergys.exe")#this could be subed with a pid and killtask if you did a dictionary look up
else:
os.system('"c:\program files\synergy\synergys.exe" -a pieceofcrap:24800 -d FATAL')
os.system(' "c:\program files\ultramon\ultramon.exe" /d')
This post was edited on 04-21-2009 at 06:47 AM by Jarrod.
|
|
04-21-2009 05:43 AM |
|
|
MeEtc
Patchou's look-alike
In the Shadow Gallery once again
Posts: 2200 Reputation: 60
38 / /
Joined: Nov 2004
Status: Away
|
O.P. RE: Help writing a batch file
that would require that python is installed on my pc, which its not...
I cannot hear you. There is a banana in my ear.
|
|
04-21-2009 05:55 AM |
|
|
Jarrod
Veteran Member
woot simpson
Posts: 1304 Reputation: 20
– / /
Joined: Sep 2006
|
RE: Help writing a batch file
you could just compile the python into an exe but that would require python and py to exe, i would but i don't use exe anymore
[edit: or just install python]
This post was edited on 04-21-2009 at 07:26 AM by Jarrod.
|
|
04-21-2009 06:47 AM |
|
|
Dempsey
Scripting Contest Winner
http://AdamDempsey.net
Posts: 2395 Reputation: 53
38 / /
Joined: Jul 2003
|
RE: Help writing a batch file
Something like this should do it:
code: @echo off
tasklist | find /i \"" & synergys.exe & "\" >nul
if %errorlevel% EQU 0 (
taskkill /f /im synergys.exe
cd "c:\program files\ultramon"
ultramon.exe /e
) ELSE (
cd "c:\program files\synergy"
synergys.exe -a pieceofcrap:24800 -d FATAL
cd "c:\program files\ultramon"
ultramon.exe /d
)
|
|
04-21-2009 07:44 AM |
|
|
CookieRevised
Elite Member
Posts: 15517 Reputation: 173
– / /
Joined: Jul 2003
Status: Away
|
RE: Help writing a batch file
Dempsey got it... Although he made a woopsy with the find parameters: code: @ECHO OFF
tasklist | find /i "synergys.exe" 1>NUL 2>&1
IF %ERRORLEVEL% EQU 0 (
taskkill /f /t /im synergys.exe
"c:\program files\ultramon\ultramon.exe" /e
)
IF %ERRORLEVEL% EQU 1 (
"c:\program files\synergy\synergys.exe" -a pieceofcrap:24800 -d FATAL
"c:\program files\ultramon\ultramon.exe" /d
)
PS: other changes:
- 1>NUL 2>&1 will also surpress error messages on screen.
- Reason for 2 seperate IFs instead of the IF tHEN ELSE like before is so that the second one is not executed when an error (=errorlevel 2) occurs in the FIND command.
- Added parameter /T to taskkill command which will kill the process tree instead of only the process.
- No reason to first change the current directory to the respective program folders
This post was edited on 04-21-2009 at 04:15 PM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
|
|
04-21-2009 04:09 PM |
|
|
Dempsey
Scripting Contest Winner
http://AdamDempsey.net
Posts: 2395 Reputation: 53
38 / /
Joined: Jul 2003
|
RE: Help writing a batch file
Ah yea, I had the speech marks escaped because I copy and pasted it from a FileMaker script.
I was close!
|
|
04-21-2009 05:15 PM |
|
|
Mike
Elite Member
Meet the Spam Family!
Posts: 2795 Reputation: 48
32 / /
Joined: Mar 2003
|
RE: Help writing a batch file
Don't forget that taskkill.exe is only available in the Professional edition of Windows XP! Not sure about Vista though.
|
|
04-21-2009 06:00 PM |
|
|
MeEtc
Patchou's look-alike
In the Shadow Gallery once again
Posts: 2200 Reputation: 60
38 / /
Joined: Nov 2004
Status: Away
|
O.P. RE: Help writing a batch file
I have xp pro, so all is well. Thanks cookie and dempsey, I'll try it out tonight
I cannot hear you. There is a banana in my ear.
|
|
04-21-2009 06:24 PM |
|
|
MeEtc
Patchou's look-alike
In the Shadow Gallery once again
Posts: 2200 Reputation: 60
38 / /
Joined: Nov 2004
Status: Away
|
O.P. RE: Help writing a batch file
Having a problem with the second half of the script with synergys.exe. The batch file won't continue running because synergys.exe is still running. ultramon.exe won't start until synergys.exe stops. Any way around that?
I cannot hear you. There is a banana in my ear.
|
|
04-22-2009 02:27 AM |
|
|
Pages: (2):
« First
[ 1 ]
2
»
Last »
|
|