• 0

Help w/ batch file


Question

Hello, I'm quite rusty in making batch files as I haven't made one in a few years so I figured I'd ask you guys for help

 

I'm trying to create a simple batch file for Windows so that when a certain program closes the file automatically opens another program

 

what would be a good way to set this up? would this even be possible with a simple batch file?

Link to comment
Share on other sites

11 answers to this question

Recommended Posts

  • 0
:NOTEPAD
start /wait notepad.exe || goto QUIT

:NEXTITEM1
start /wait mplayer2.exe || goto QUIT

:NEXTITEM2
REM You get the idea...

:QUIT
exit  

or

:NOTEPAD
start /wait notepad.exe
IF %ERRORLEVEL% == 0 goto NEXTITEM1
else goto QUIT

:NEXTITEM1
start /wait mplayer.exe
IF %ERRORLEVEL% == 0 goto NEXTITEM2
else goto QUIT

:NEXTITEM2
start /wait explorer.exe
IF %ERRORLEVEL% == 0 goto NEXTITEM3
else goto QUIT

:NEXTITEM3
REM You get the idea...

:QUIT
exit
Link to comment
Share on other sites

  • 0

at my work we have a new software phone and when you logout this new one automatically closes instead of going back to a login screen like the old one. i'd like to set up a batch file that re-opens the login screen when it closes

 

(and before you ask. yes my work is ok with us using simple batch scripts. they have a few pre-made for us that launch all our main apps for us. just none that relaunch specific apps if they close)

Link to comment
Share on other sites

  • 0

What OS? does it have Powershell?

Windows 7

 

and no, it doesn't look like powershell is installed on the workstations

Link to comment
Share on other sites

  • 0

If all you're doing is restarting the same app when it closes, then all you need is a simple loop. Have the batch file start at login with the task scheduler, or put it into the startup folder.

 

persistent.bat
 

@echo off
:TOP
start /WAIT app.exe
GOTO TOP

Now, if all you want to do is have one app and only one app only running, there are other solutions.

Link to comment
Share on other sites

  • 0

xendrome has it right. 

 

I had something similar to Joe Users when I wanted the only thing to happen on a computer was launch remote desktop.  I changed the shell from explorer.exe to the bat file. Worked great.

Link to comment
Share on other sites

  • 0

Note that these examples are predicated on using a batch file to actually open your "software phone" for the first time. If you're looking for some kind of exit event handler, I don't think there is any support for process watching. In powershell, perhaps, but not in the normal batch processor.

 

If, however, you're using a batch file to launch your app, the start /wait examples will work fine.

 

-Forjo

Link to comment
Share on other sites

  • 0

thanks guys. xendrome's example worked perfectly :)

 

got it set up so it only reopens as many times as needed for my shift

Link to comment
Share on other sites

  • 0

If you want it a bit more silent use a vb script

 

 The following will loop notepad.  The only way to kill the notepad process from looping is to kill the wscript.exe process in task manager....there is no dos screen to minimize or try to hide.  Copy the quoted text into a test.vbs file then double click test.vbs after you close and save.

 

sProcessName = "notepad.exe"
sComputer = "."    
Set oWmi = GetObject("winmgmts:" _
   & "{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
 Set oShell = CreateObject("WScript.Shell")

Do
  Set colProcessList = oWmi.ExecQuery _
      ("Select * from Win32_Process Where Name = '" & sProcessName & "'")
   If colProcessList.Count = 0 Then 'Notepad is not running...Open Notepad
   oShell.Run "C:\windows\notepad.exe", 1, False
     
End If
 
WScript.Sleep 10
Loop
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.