Shoutbox

Help writing a batch file - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: Help writing a batch file (/showthread.php?tid=90287)

Help writing a batch file by MeEtc on 04-21-2009 at 03:09 AM

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
}

RE: Help writing a batch file by Jarrod on 04-21-2009 at 05:43 AM

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')


RE: Help writing a batch file by MeEtc on 04-21-2009 at 05:55 AM

that would require that python is installed on my pc, which its not...


RE: Help writing a batch file by Jarrod on 04-21-2009 at 06:47 AM

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]


RE: Help writing a batch file by Dempsey on 04-21-2009 at 07:44 AM

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
)

RE: Help writing a batch file by CookieRevised on 04-21-2009 at 04:09 PM

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


;)
RE: Help writing a batch file by Dempsey on 04-21-2009 at 05:15 PM

Ah yea, I had the speech marks escaped because I copy and pasted it from a FileMaker script. 

I was close! :P


RE: Help writing a batch file by Mike on 04-21-2009 at 06:00 PM

Don't forget that taskkill.exe is only available in the Professional edition of Windows XP! Not sure about Vista though.


RE: Help writing a batch file by MeEtc on 04-21-2009 at 06:24 PM

I have xp pro, so all is well. Thanks cookie and dempsey, I'll try it out tonight


RE: Help writing a batch file by MeEtc on 04-22-2009 at 02:27 AM

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?


RE: Help writing a batch file by Jarrod on 04-22-2009 at 03:35 AM

quote:
Originally posted by CookieRevised

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
)



use the start command instead.
so
start "c:\program files\synergy\synergys.exe" -a pieceofcrap:24800 -d FATAL
start  "c:\program files\ultramon\ultramon.exe" /d


RE: Help writing a batch file by MeEtc on 04-22-2009 at 04:26 AM

had to modify that slightly to get it to work properly to:

start c:\progra~1\synergy\synergys.exe -a pieceofcrap:24800 -d FATAL

double quotes for start as the first param indicate the title of a new prompt window, and it went downhill from there. It runs perfectly now. All I need now is a DVI cable that doesn't cost an arm and a leg...


RE: Help writing a batch file by CookieRevised on 04-23-2009 at 06:54 AM

quote:
Originally posted by MeEtc
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?
What second half? What you mean by "synergys is still running"? You mean taskkill doesnt kill the synergys process ?

I ask this because it makes little sense that by adding START to everything it all of a sudden does work. Because you should not explicitly need the START command to start a program!

You only explicitly need START if you want to alter the console window like the title, minimized state, change the priority, wait for the app to quit, etc...

It should work perfectly without it, provided taskkill does kill the process in time. For that reason, and only that reason, it might be a good idea to build in a wait period before running "c:\program files\ultramon\ultramon.exe" /e so that taskkill has some time to kill the synergys process.

As that is the problem from what I understand from your post.

So, simply adding START like Jarod said doesn't make any difference and wont fix anything and you will most likely experience the same problem another time (when taskkill needs some more time than instant for whatever reason to kill synergys).

So instead of adding START everywhere, try to change:
   taskkill /f /t /im synergys.exe
to
   start /wait taskkill /f /t /im synergys.exe
this should pause the batch until taskkill has done its job instead of immediatly executing the next line (starting ultramon).
RE: Help writing a batch file by Vilkku on 04-23-2009 at 07:27 AM

Was about to comment on that part you just edited out, Cookie... the reason I (and I bet most other) use Ultramon is to extend the taskbar to the second monitor. If you know another (freeware :o) application that does it, then I'd be very interested in it (I know there is one but it doesn't have the proper Vista skin in the free version and it also for some reason wants to add stuff like the clock to the second taskbar).

When it comes to Synergy, it's an application to share one keyboard and mouse between many computers (essentially making the second computer act like another monitor for the first one, without being able to move applications between computers).


RE: Help writing a batch file by CookieRevised on 04-23-2009 at 07:37 AM

quote:
Originally posted by Vilkku
Was about to comment on that part you just edited out, Cookie...
Yeah sorry, wasn't realy relevant to the thread...

quote:
Originally posted by Vilkku
the reason I (and I bet most other) use Ultramon is to extend the taskbar to the second monitor. If you know another (freeware :o) application that does it, then I'd be very interested in it (I know there is one but it doesn't have the proper Vista skin in the free version and it also for some reason wants to add stuff like the clock to the second taskbar).
On my other computer the video drivers has that build in. Nevertheless, I don't like having an 'extended' taskbar. For that matter, I know Ultramon has some other nifty features, but all features I personaly would never use. So its kind of useless to me.... hence my (removed) comments.

quote:
Originally posted by Vilkku
When it comes to Synergy, it's an application to share one keyboard and mouse between many computers (essentially making the second computer act like another monitor for the first one, without being able to move applications between computers).
Yeah, looked it up. Sound like a cool thing and very innovative/original I think to use TCP/IP. Although by the way it works, it sure must have problems with certain programs and stuff. But again no use for me as I have a 2 'Blackboxes' for that (second hand of course, way too expensive otherwise :p) of which one is a matrix switch, so I can have any monitor for any PC. Should have known about senergys before I bought them, damn...

EDIT: although, now that I think about it, don't you need all the connected PCs to be running if you want to use a particular monitor in that case? Which is a no-go in my case... blah... those two 'blackboxes' makes my whole desktop a bit cooler (more lights and switches and stuff you know :D)
RE: Help writing a batch file by foaly on 04-23-2009 at 12:01 PM

quote:
Originally posted by CookieRevised
What second half? What you mean by "synergys is still running"? You mean taskkill doesnt kill the synergys process ?

I ask this because it makes little sense that by adding START to everything it all of a sudden does work. Because you should not explicitly need the START command to start a program!

Ehm as far as I know you do...

try this as a batch file:

code:
mspaint.exe
notepad.exe
pause
start mspaint.exe
start notepad.exe


it will only start notepad after paint is closed...
but the second part will start them both...
RE: Help writing a batch file by MeEtc on 04-23-2009 at 03:12 PM

Yes foaly, that's exactly whgats happening. The batch file was waiting for synergys.exe to finish running before running the coomand to shut off the secondary monitor. As I said earlier, I have it working now, and that's all that really matters.


RE: RE: Help writing a batch file by CookieRevised on 04-23-2009 at 10:22 PM

quote:
Originally posted by foaly
quote:
Originally posted by CookieRevised
What second half? What you mean by "synergys is still running"? You mean taskkill doesnt kill the synergys process ?

I ask this because it makes little sense that by adding START to everything it all of a sudden does work. Because you should not explicitly need the START command to start a program!
Ehm as far as I know you do...
try this as a batch file:
code:
mspaint.exe
notepad.exe
pause
start mspaint.exe
start notepad.exe
it will only start notepad after paint is closed...
but the second part will start them both...
Ah, programs opening a window and keep on running... forgot about that. I never use a batch file to start a windows based program or program which keeps running and I didn't knew that ultramon or synergys with those command line parameters would still be running after doing their thing, stupid me.

For all other normal batch file stuff (as I like to call it now :p) you should not use START though as it opens the program in another console thread and thus you can't control, check or redirect the output or even know if program A starts before program B.

You learn something new every day. Thanks foaly
RE: Help writing a batch file by Jarrod on 04-23-2009 at 10:39 PM

quote:
Originally posted by foaly
quote:
Originally posted by CookieRevised
What second half? What you mean by "synergys is still running"? You mean taskkill doesnt kill the synergys process ?

I ask this because it makes little sense that by adding START to everything it all of a sudden does work. Because you should not explicitly need the START command to start a program!

Ehm as far as I know you do...

try this as a batch file:

code:
mspaint.exe
notepad.exe
pause
start mspaint.exe
start notepad.exe


it will only start notepad after paint is closed...
but the second part will start them both...
that's why I said use start:p, I just wasn't fast enough