What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » Help writing a batch file

Pages: (2): « First [ 1 ] 2 » Last »
Help writing a batch file
Author: Message:
MeEtc
Patchou's look-alike
*****

Avatar
In the Shadow Gallery once again

Posts: 2200
Reputation: 60
38 / Male / Flag
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
}
[Image: signature/]     [Image: sharing.png]
I cannot hear you. There is a banana in my ear.
04-21-2009 03:09 AM
Profile PM Web Find Quote Report
Jarrod
Veteran Member
*****

Avatar
woot simpson

Posts: 1304
Reputation: 20
– / Male / Flag
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.

[Image: 5344.png]
[Image: sig.png]

A.k.a. The Glad Falconer














04-21-2009 05:43 AM
Profile E-Mail PM Find Quote Report
MeEtc
Patchou's look-alike
*****

Avatar
In the Shadow Gallery once again

Posts: 2200
Reputation: 60
38 / Male / Flag
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...
[Image: signature/]     [Image: sharing.png]
I cannot hear you. There is a banana in my ear.
04-21-2009 05:55 AM
Profile PM Web Find Quote Report
Jarrod
Veteran Member
*****

Avatar
woot simpson

Posts: 1304
Reputation: 20
– / Male / Flag
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.

[Image: 5344.png]
[Image: sig.png]

A.k.a. The Glad Falconer














04-21-2009 06:47 AM
Profile E-Mail PM Find Quote Report
Dempsey
Scripting Contest Winner
*****

Avatar
http://AdamDempsey.net

Posts: 2395
Reputation: 53
37 / Male / Flag
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
)
SoundPacks   -   Scripts   -   Skins

that's not a bug, thats an unexpected feature
04-21-2009 07:44 AM
Profile E-Mail PM Web Find Quote Report
CookieRevised
Elite Member
*****

Avatar

Posts: 15519
Reputation: 173
– / Male / Flag
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
Profile PM Find Quote Report
Dempsey
Scripting Contest Winner
*****

Avatar
http://AdamDempsey.net

Posts: 2395
Reputation: 53
37 / Male / Flag
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! :P
SoundPacks   -   Scripts   -   Skins

that's not a bug, thats an unexpected feature
04-21-2009 05:15 PM
Profile E-Mail PM Web Find Quote Report
Mike
Elite Member
*****

Avatar
Meet the Spam Family!

Posts: 2795
Reputation: 48
31 / Male / Flag
Joined: Mar 2003
Status: Online
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.
YouTube closed-captions ripper (also allows you to download videos!)
04-21-2009 06:00 PM
Profile E-Mail PM Web Find Quote Report
MeEtc
Patchou's look-alike
*****

Avatar
In the Shadow Gallery once again

Posts: 2200
Reputation: 60
38 / Male / Flag
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
[Image: signature/]     [Image: sharing.png]
I cannot hear you. There is a banana in my ear.
04-21-2009 06:24 PM
Profile PM Web Find Quote Report
MeEtc
Patchou's look-alike
*****

Avatar
In the Shadow Gallery once again

Posts: 2200
Reputation: 60
38 / Male / Flag
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?
[Image: signature/]     [Image: sharing.png]
I cannot hear you. There is a banana in my ear.
04-22-2009 02:27 AM
Profile PM Web Find Quote Report
Pages: (2): « First [ 1 ] 2 » Last »
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On