• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 676
  • Last Modified:

batch scripting with checking to see if program is already running

I am trying to get this scrip to check if a program is already running. If it is, then exit script. If the program isn't running, then run script.

Everything in the script works but the IF Tasklist statements at the top:

@echo off

:CheckLights
tasklist /fi "Imagename eq Tucson Lights.exe" > NUL | find /I /N "Tucson Lights.exe"
IF ERRORLEVEL 0 GOTO Launch
tasklist /fi "Imagename eq Tucson Lights.exe" > NUL | find /I /N "Tucson Lights.exe"
IF ERRORLEVEL 2 GOTO Exit

:Exit
exit

:Launch
setlocal
set BaseFolder=C:\Users.Lights
set UserFolder=%BaseFolder%\%UserName%
set LightsFolder=%UserFolder%\Lights
set SetupFolder=C:\RDS.Setup\Lights
set ProdFolder=C:\RDS.Setup\PROD

if not exist "%UserFolder%" (
	md "%UserFolder%"
)
if not exist "%LightsFolder%" (
	robocopy.exe "%SetupFolder%" "%LightsFolder%" *.* /Z /r:0 /w:0
)
robocopy.exe "%ProdFolder%" "%LightsFolder%" *.exe /H /O /Z /r:0 /w:0
robocopy.exe "%ProdFolder%" "%LightsFolder%" system.dbf /H /O /Z /r:0 /w:0

net.exe use G: /delete >NUL 2>&1
net.exe use G: "\\dctucson\group$" /persistent:no
net use lpt3 "\\RDS01\Office BoxTag Printer"
rem CD\
rem CD "%LightsFolder%" 
rem Lights.exe usermax2
start "" /D "%LightsFolder%" "%LightsFolder%\Tucson Lights.exe" /Seperate /Wait /MAX

Exit

Open in new window




Help?!?
0
Glenn M
Asked:
Glenn M
  • 5
  • 3
  • 2
2 Solutions
 
becraigCommented:
ok so you said you want to exit if tasklist finds it, but your if statement says launch if found and exit if not :

also why are you piping to NUL the trying to get output from NUL ?




@echo off

:CheckLights
tasklist /fi "Imagename eq Tucson Lights.exe" | find /I /N "Tucson Lights.exe" > NUL
if ERRORLEVEL == 0 (
GOTO Exit
) else (
GOTO Launch
)

:Exit
exit

:Launch
setlocal
set BaseFolder=C:\Users.Lights
set UserFolder=%BaseFolder%\%UserName%
set LightsFolder=%UserFolder%\Lights
set SetupFolder=C:\RDS.Setup\Lights
set ProdFolder=C:\RDS.Setup\PROD

if not exist "%UserFolder%" (
	md "%UserFolder%"
)
if not exist "%LightsFolder%" (
	robocopy.exe "%SetupFolder%" "%LightsFolder%" *.* /Z /r:0 /w:0
)
robocopy.exe "%ProdFolder%" "%LightsFolder%" *.exe /H /O /Z /r:0 /w:0
robocopy.exe "%ProdFolder%" "%LightsFolder%" system.dbf /H /O /Z /r:0 /w:0

net.exe use G: /delete >NUL 2>&1
net.exe use G: "\\dctucson\group$" /persistent:no
net use lpt3 "\\RDS01\Office BoxTag Printer"
rem CD\
rem CD "%LightsFolder%" 
rem Lights.exe usermax2
start "" /D "%LightsFolder%" "%LightsFolder%\Tucson Lights.exe" /Seperate /Wait /MAX

Exit
              

Open in new window

0
 
oBdACommented:
There are several things to note:
1. "if errorlevel 0" is always true; "if errorlevel x" checks if the errorlevel is the same or bigger than x,
2. "find.exe" will only return errorlevel 0 or 1, but never 2.
3. As becraig said, if you pipe a command's output to NUL, it's pointless to redirect the output to another program; to suppress find.exe's output, you need to pipe find.exe's output.
4. There's usually no need to use "exit" in a batch script; if a batch is invoked from another script or via double-click in Explorer, the script will end automatically anyway, and if it's called via command line from an open prompt (for testing), it's usually counterproductive to have your testing shell closed each time you run the script. If you want to leave your script in an "if" condition, you can use "goto :eof" (":eof" is an implicitly defined label that points to the very end of the script, you don't need to add it yourself), or you can use "exit /b Errorlevel" if you want to return an errorlevel.
5. robocopy.exe supports several file masks after the source and target folder are specified, so you don't need two robocopy commands.
6. If you want to use start's "wait", "max", and "separate":
6a. These options need to be placed before the command you're running; currently, they're passed to lights.exe (which probably ignores them, if you didn't notice anything strange).
6b. Why did you add the "/wait" to the "start" command, if the script is done afterwards anyway? All this would do is keep the command prompt open until lights.exe is done, in all likelihood confusing the user.
6c. The "seperate" option you added is called "separate" when used for "start", and it's probably not required, unless you're running a 32bit OS and lights.exe is indeed a 16bit program.
Note that I didn't correct any of the "6" issues in the script below, because I might be wrong and they belong to lights.exe after all.
@echo off
setlocal

set Process=Tucson Lights.exe
set BaseFolder=C:\Users.Lights
set UserFolder=%BaseFolder%\%UserName%
set LightsFolder=%UserFolder%\Lights
set SetupFolder=C:\RDS.Setup\Lights
set ProdFolder=C:\RDS.Setup\PROD

tasklist /fi "IMAGENAME eq %Process%" | find /I "%Process%" >NUL
if not errorlevel 1 goto :eof

if not exist "%UserFolder%" (
	md "%UserFolder%"
)
if not exist "%LightsFolder%" (
	robocopy.exe "%SetupFolder%" "%LightsFolder%" *.* /Z /r:0 /w:0
)
robocopy.exe "%ProdFolder%" "%LightsFolder%" *.exe system.dbf /H /O /Z /r:0 /w:0

net.exe use G: /delete >NUL 2>&1
net.exe use G: "\\dctucson\group$" /persistent:no
net.exe use lpt3 "\\RDS01\Office BoxTag Printer"
rem CD\
rem CD "%LightsFolder%" 
rem Lights.exe usermax2
start "" /D "%LightsFolder%" "%LightsFolder%\%Process%" /Seperate /Wait /MAX

Open in new window

0
 
Glenn MSystems Engineer / ManagerAuthor Commented:
I copied the commands from others, so I don't know why I need/ user NULL.

My goal was for the script to check to see if "Tucson Lights.exe" was running. If it was, then end script.
If it wasn't running, then run Script.

Knowing this, what would you change in your scripting suggestions?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Glenn MSystems Engineer / ManagerAuthor Commented:
...Revision to my note Above:

I actually need the script to check and see if "Tucson Lighs.exe" and allow TWO sessions of "Tucson Lights.exe".
I am trying to prevent a "Tucson Lights.exe" session of lights of being executed and ran.

I was trying to start with one, and then expand it to two.... so if we can adjust the script according, I would appreciate it.
0
 
becraigCommented:
ok so you want to run one more session if one is already running but exit if two are running  ?

Just trying to be sure.
0
 
oBdACommented:
Try it with this then:
@echo off
setlocal

set Process=Tucson Lights.exe
set MaxProcesses=2
set BaseFolder=C:\Users.Lights
set UserFolder=%BaseFolder%\%UserName%
set LightsFolder=%UserFolder%\Lights
set SetupFolder=C:\RDS.Setup\Lights
set ProdFolder=C:\RDS.Setup\PROD

for /f %%a in ('tasklist.exe /fi "IMAGENAME eq %Process%" ^| find.exe /C /I "%Process%"') do (set ProcessCount=%%a)
if %ProcessCount% geq %MaxProcesses% (
	msg.exe %UserName% "Sorry, there are already %MaxProcesses% instances of '%Process%' running."
	goto :eof
)

if not exist "%UserFolder%" (
	md "%UserFolder%"
)
if not exist "%LightsFolder%" (
	robocopy.exe "%SetupFolder%" "%LightsFolder%" *.* /Z /r:0 /w:0
)
robocopy.exe "%ProdFolder%" "%LightsFolder%" *.exe system.dbf /H /O /Z /r:0 /w:0

net.exe use G: /delete >NUL 2>&1
net.exe use G: "\\dctucson\group$" /persistent:no
net.exe use lpt3 "\\RDS01\Office BoxTag Printer"
rem CD\
rem CD "%LightsFolder%" 
rem Lights.exe usermax2
start "" /MAX /D "%LightsFolder%" "%LightsFolder%\%Process%"

Open in new window

0
 
Glenn MSystems Engineer / ManagerAuthor Commented:
Correct, Testing Script Above...
0
 
Glenn MSystems Engineer / ManagerAuthor Commented:
...Well, I can't tell if the script is working correctly, as it is detecting processes from other users ( this is a terminal server ) and the script ends.  :)

Can we change how the FIND looks, and to only look for the current user?
0
 
oBdACommented:
No problem; I suspected something like this, but thought you wanted to limit to a total of 2 sessions, not two sessions per user. Just requires another filter in line 12:
@echo off
setlocal

set Process=Tucson Lights.exe
set MaxProcesses=2
set BaseFolder=C:\Users.Lights
set UserFolder=%BaseFolder%\%UserName%
set LightsFolder=%UserFolder%\Lights
set SetupFolder=C:\RDS.Setup\Lights
set ProdFolder=C:\RDS.Setup\PROD

for /f %%a in ('tasklist.exe /fi "IMAGENAME eq %Process%" /fi "USERNAME eq %UserName%" ^| find.exe /C /I "%Process%"') do (set ProcessCount=%%a)
if %ProcessCount% geq %MaxProcesses% (
	msg.exe %UserName% "Sorry, there are already %MaxProcesses% instances of '%Process%' running."
	goto :eof
)

if not exist "%UserFolder%" (
	md "%UserFolder%"
)
if not exist "%LightsFolder%" (
	robocopy.exe "%SetupFolder%" "%LightsFolder%" *.* /Z /r:0 /w:0
)
robocopy.exe "%ProdFolder%" "%LightsFolder%" *.exe system.dbf /H /O /Z /r:0 /w:0

net.exe use G: /delete >NUL 2>&1
net.exe use G: "\\dctucson\group$" /persistent:no
net.exe use lpt3 "\\RDS01\Office BoxTag Printer"
rem CD\
rem CD "%LightsFolder%" 
rem Lights.exe usermax2
start "" /MAX /D "%LightsFolder%" "%LightsFolder%\%Process%"

Open in new window

0
 
Glenn MSystems Engineer / ManagerAuthor Commented:
Great Job, the script works just as expected!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now