05-19-2004, 01:11 PM | #1 (permalink) |
Insane
|
is this possible with a batch file?
this may be a dumb question because i'm not all that familiar with the limits or possibilities of batch files, but here is what i would like to do.
I have one program that basically starts another program....but sometimes they interfere with each other and the second program doesn't start up right. Could i make a batch file that the first program calls (instead of calling the second program itself), and then the batch file would close the first program and open the second? Then I would need it to open the first program back up when I close the second program. Is it possible with batch files? Thanks for any help.
__________________
Mechanical Engineers build weapons. Civil Engineers build targets. |
05-19-2004, 01:51 PM | #2 (permalink) |
Rookie
Location: Oxford, UK
|
Making the program quit is going to be your problem. Windows XP has the 'TASKKILL' command:
Code:
TASKKILL [/S system [/U username [/P [password]]]] { [/FI filter] [/PID processid ¦ /IM imagename] } [/F] [/T] Description: This command line tool can be used to end one or more processes. Processes can be killed by the process id or image name. Parameter List: /S system Specifies the remote system to connect to. /U (domain\)user Specifies the user context under which the command should execute. /P (password) Specifies the password for the given user context. Prompts for input if omitted. /F Specifies to forcefully terminate process(es). /FI filter Displays a set of tasks that match a given criteria specified by the filter. /PID process id Specifies the PID of the process that has to be terminated. /IM image name Specifies the image name of the process that has to be terminated. Wildcard '*' can be used to specify all image names. /T Tree kill: terminates the specified process and any child processes which were started by it. /? Displays this help/usage.
__________________
I can't understand why people are frightened of new ideas. I'm frightened of the old ones. -- John Cage (1912 - 1992) |
05-19-2004, 01:53 PM | #3 (permalink) |
paranoid
Location: The Netherlands
|
Probably...
but start of simple: what OS are you running.... In the end you'll probably have 2 batch files: A> run program A, when it closes, run batchfile B B> run program B, when it closes, run batchfile A though some measure needs to be there to prevent the infamous infinite loop.
__________________
"Do not kill. Do not rape. Do not steal. These are principles which every man of every faith can embrace. " - Murphy MacManus (Boondock Saints) |
05-19-2004, 08:19 PM | #5 (permalink) |
Insane
|
i'm using xp pro, so taskkill is the one i need...but now i can't figure out exactly the right syntax. i tried process id, but apparently i don't know what a process id is because it always said i gave an invalid one (i just figured it was the name of the program under processes in the task manager).
I did this TASKKILL /pid progam.exe and (but in a separate occasion) TASKKILL /pid program Then i tried image name, but i guess i don't know what exactly they want for that either. any help with that? thanks
__________________
Mechanical Engineers build weapons. Civil Engineers build targets. |
05-19-2004, 09:23 PM | #6 (permalink) |
I am Winter Born
Location: Alexandria, VA
|
The PID = process ID, a numerical value.
If you open up task manager and switch to process view, go to "select columns", and add "PID (Process Identifier)" you'll then be able to figure out what to kill with taskkill. However, each process has a unique PID - and when you run a program twice, it will have different PIDs. In fact, let's say you run winamp and then AIM, then quit both, and relaunch both. It's possible (though not likely) for AIM to have winamp's old PID and vice versa. Anything done with PIDs will have to be done through API calls to determine the PID of the application with such-and-such name and then kill it
__________________
Eat antimatter, Posleen-boy! |
05-19-2004, 09:36 PM | #7 (permalink) |
Insane
|
okay, i got it to work with /im (that's where it wants what i though process id wanted)...but it doesn't kill the program unless i use /f. So i tried using /f and then simply rerunning the first program on the line after running the second program and it seemed to work at first. But after doing it a couple times, it wouldn't even run the second program anymore. So, i think i just need to rewrite it. here's what i've been using:
TASKKILL /im 1stprogram.exe cd d:\2ndprogramdirectory 2ndprogam.exe parameter cd d:\1stprogramdirectory 1stprogram.exe it's simple because i'm a newb . If there's anything anybody thinks would help with it, please feel free to change it. on a side note....just using the batch file seems to have helped a lot with how the second program gets called. Without using /f (even though the first program is still running) I've run into my problem a lot less, but it still occasionally happens. thanks for all the help so far, and for anymore to come.
__________________
Mechanical Engineers build weapons. Civil Engineers build targets. |
05-20-2004, 06:35 AM | #8 (permalink) |
"Officer, I was in fear for my life"
Location: Oklahoma City
|
Why don't you have program A close as soon as it calls the batch file?
Your batch file could look something like this: Code:
pause cd d:\2ndprogramdirectory call 2ndprogram.exe parameter cd\1stprogramdirectory 1stprogram.exe |
05-20-2004, 09:13 AM | #9 (permalink) |
Insane
|
hrdwareguy, although that's a good idea, I don't know how to make the program close itself when it calls the batch file other than taskkill (I didn't write program A or anything and although it is open source, I don't have the skills to try and change it). Also, a pause does make it work, but I really don't want to have to hit a button each time I do it.
Another problem though, everytime program A calls the batch file or program B, it becomes "not responding", until program B is closed. So, the only way to close it is to force it (/f) and I think that causes a memory leak. So i don't know if I even want to go that route anymore. I think all it really needs is a bit of a delay. I think program B get's loaded up too fast and doesn't give program A time to minimize or something like that (I don't really know if that is even a plausible cause). So how could i get it to delay? I think I'm going to just have it change directories a bunch before it calls program B and see if that works. edit: okay. so the delay didn't work, it just took longer to load. The same problem still exists in that the two programs don't like each other. I've somewhat narrowed down when this happens though. Program A is just a frontend for emulators that I have startup when the computer starts up. The first time it starts up, it almost always screws up loading the certain emulator. Even though the emulator isn't showing on the screen, I can close it with alt-f4 and go straight back to the frontend. If I try it again, it usually does the same thing. If I close the frontend and re-open it, it works fine and continues to work fine. I really have no idea why it does this. If at anytime the emulator does load correctly, it will continue to load correctly until I close the frontend. my batch file is only this for right now: cd d:\programBdirectory programB.exe parameters adding other stuff in didn't seem to change the outcome and using TASKKILL just screwed it up more. So basically, I'm kind of lost as to what the problem is and how to fix it. If anybody has any more suggestions, please help me. thanks
__________________
Mechanical Engineers build weapons. Civil Engineers build targets. Last edited by yatzr; 05-20-2004 at 01:34 PM.. |
05-20-2004, 01:57 PM | #10 (permalink) | |
"Officer, I was in fear for my life"
Location: Oklahoma City
|
Quote:
|
|
05-20-2004, 02:42 PM | #11 (permalink) |
Rookie
Location: Oxford, UK
|
I think the problem might be in the way you're starting the batchfile - you want it to return control to prog A once it's called prog B. I think using "start" might do the trick (and avoid having to use /f on taskkill). Perhaps someone can remember better. But essentially you need to find a way to start everything and return control to program A.
*EDIT:* Code:
START Start a specified program or command in a separate window. syntax START "title" [/Dpath] [options] [command] [parameters] key WHAT to run path : Starting directory command : The NT Command, Batch file or executable program to run parameters : The parameters passed to the command HOW to run it /MIN : Minimized /MAX : Maximized /WAIT : Start application and wait for it to terminate /LOW : Use IDLE priority class /NORMAL : Use NORMAL priority class /HIGH : Use HIGH priority class /REALTIME : Use REALTIME priority class "title" : Text for the CMD window title. /B : Start application without creating a new window. In this case ^C will be ignored - leaving ^Break as the only way to interrupt the application /I : Ignore any changes to the current environment. Options for 16-bit WINDOWS programs only /SEPARATE Start in separate memory space (more robust) /SHARED Start in shared memory space (default) You need the batchfile to be running completely separately from prog A, so when the batchfile kills prog A it doesn't kill itself! I think using 'start', or maybe even 'cmd.exe <batchfile>' will be the best way to ensure it's separate.
__________________
I can't understand why people are frightened of new ideas. I'm frightened of the old ones. -- John Cage (1912 - 1992) Last edited by cliche; 05-20-2004 at 11:36 PM.. |
05-22-2004, 11:28 AM | #12 (permalink) |
Insane
|
well, i mess around with it for hours and i still couldn't get it to work. I think I'm just going to try to find a different emulator (since this is the only one that seems to have a problem). Thanks for all the help though. I actually learned quite a bit about some commands from this.
edit: the search for a different emulator didn't go too well. there just aren't any others that do what i want. so i messed around with the batch files some more. I tried going back to the TASKKILL and discovered something interesting. Using /f didn't screw it up because of any memory leak....for some reason, closing the program with the batch file, and then later opening it with the batch file screws something up. I used the taskkill, start /wait programB, open programA approach. The first time the batch file is run, it works perfectly. After that though, programA doesn't even run the batch file. I tested this by putting a pause in the first line of the batch file. ProgramA just does a screen flicker and goes back to itself without running a thing. BUT, if I don't open programA back up with the batch file (nothing is left running after closing programB) and simply double click on programA to open it back up, it works fine. SO, I think there must be some way to open programA back up so that it's just like double clicking the icon, and that is what I need. I already tried the start command with several options, so that's not it. Any suggestions? also, cliche, you suggested to try doing cmd.exe batchfile to call the batch file...but I need to pass the batch file a parameter. Is there a way to do this with putting cmd.exe in front? edit again: I discovered that programA screws up if it's opened by a batch file or while a batch file is running (ie. double clicking the icon while a batch file is on pause). So now I'm starting to think this isn't possible. Unless someone has a good idea on how to open the program without the batch file running. I've pretty much lost all hope at this point.
__________________
Mechanical Engineers build weapons. Civil Engineers build targets. Last edited by yatzr; 05-23-2004 at 11:21 AM.. |
05-23-2004, 01:06 PM | #13 (permalink) |
Insane
|
SUCCESS! After thinking about just how i could open the frontend back up without a batch file it hit me....macro. As luck would have it, I was even using a macro program (I used it to map alt-f4 to a button on my mouse/remote) and all I had to do was tell it to launch the frontend back up after hitting alt-f4. It worked perfectly.
So in conclusion, i don't think it could've been done with just batch files...but that's alright, because it works now and that's all that matters.
__________________
Mechanical Engineers build weapons. Civil Engineers build targets. |
Tags |
batch, file |
|
|