Computer Hope

Software => Computer programming => Topic started by: KCuHCeht on April 01, 2011, 12:47:27 PM

Title: Batch File If Statement
Post by: KCuHCeht on April 01, 2011, 12:47:27 PM
Hi, I'm fairly new with batch files and the syntax, but understand the logic of programming for the most part. I have 2 programs at work that both use pervasive databases, but they use different versions, Primavera P3 and Timberline Accounting. If one of the programs are running, i must close it and the database before i can open the other, and I would like to create a batch file that will do this for me  instead of doing it manually. So here is effectively what i need it to do:


\\This would be the batch file to open Primavera P3
If (tasklist contains "w3dbsmgr.exe") then
     Start "C:\Program Files\Pervasive Software\PSQL\bin\pvkillwg.exe"
End If
Start "C:\P3WIN\P3PROGS\P3.exe" /user:chuck
Exit

If someone could help me figure out the correct syntax to make this work, I believe I could figure out the other one.

Thanks,

Chuck
Title: Re: Batch File If Statement
Post by: Salmon Trout on April 01, 2011, 12:55:42 PM
Something along these lines shoud do it...

Code: [Select]
tasklist | find "w3dbsmgr.exe" && Start "C:\Program Files\Pervasive Software\PSQL\bin\pvkillwg.exe"
Start "C:\P3WIN\P3PROGS\P3.exe" /user:chuck
Title: Re: Batch File If Statement
Post by: KCuHCeht on April 01, 2011, 01:37:08 PM
it's not running the pvkillwg.exe file, the command window opens up with "C:\Program Files\Pervasive Software\PSQL\bin\pvkillwg.exe" in the title bar, and it doesn't appear that it is getting to the point to run P3.
Title: Re: Batch File If Statement
Post by: Salmon Trout on April 01, 2011, 01:42:54 PM
Try using the start "" "program.exe" syntax

tasklist | find "w3dbsmgr.exe" && Start "" "C:\Program Files\Pervasive Software\PSQL\bin\pvkillwg.exe"
Start "" "C:\P3WIN\P3PROGS\P3.exe" /user:chuck

Title: Re: Batch File If Statement
Post by: KCuHCeht on April 01, 2011, 01:49:06 PM
Awesome. Thanks. That works like a charm. Now not that it is important, but is there any way to hide the command windows from showing up at all?
Title: Re: Batch File If Statement
Post by: Salmon Trout on April 01, 2011, 01:55:12 PM
is there any way to hide the command windows from showing up at all?

try adding the /MIN switch after Start and before the ""

Start /MIN "" "program"

By the way, the && thing can be given a multiline structure if you prefer

tasklist | find "w3dbsmgr.exe" && (
        Start "" "C:\Program Files\Pervasive Software\PSQL\bin\pvkillwg.exe"
        )
Start "" "C:\P3WIN\P3PROGS\P3.exe" /user:chuck


The way it works is this: these two symbol pairs && and || have a specific meaning in post-Windows 2000 batch scripts:

&& tests for zero errorlevel so

command1 && command2 means "carry out command1 and if the errorlevel returned is zero, carry out command2"

and this...

command1 && (
     command2
     command3
     command4
     )

...means "carry out command1 and if the errorlevel returned is zero, carry out command2, command3 and command4"

Using the || symbol pair reverses the test so that a nonzero errorlevel is the trigger.
Title: Re: Batch File If Statement
Post by: KCuHCeht on April 01, 2011, 01:55:57 PM
This would be the other program, i dont think the taskkill is working

tasklist | find "w32mkde.exe" && taskkill /f /im "w32mkde.exe"
start "" "C:\Program Files\Pervasive Software\PSQL\bin\w3dbsmgr.exe"
start "" "C:\Program Files\Timberline Office\Accounting\IA.exe"
Title: Re: Batch File If Statement
Post by: KCuHCeht on April 04, 2011, 05:09:46 AM
Thanks for explaining that. I like knowing how/why things work. Any help on the taskkill would be greatly appreciated as well  :)
Title: Re: Batch File If Statement
Post by: KCuHCeht on April 04, 2011, 05:28:51 AM
OK, I've got everything working (almost), thanks to your help. Here are my final batch files for anyone looking for something similar.

Code: [Select]
tasklist | find "W32MKDE.EXE" && (
start /MIN "" "C:\Windows\System32\taskkill.exe" /f /im "W32MKDE.EXE"
)
tasklist | find "W32MKDE.EXE" || (
start /MIN "" "C:\Program Files\Pervasive Software\PSQL\bin\w3dbsmgr.exe"
)
start "" "C:\Program Files\Timberline Office\Accounting\IA.exe"

and...

Code: [Select]
tasklist | find "w3dbsmgr.exe" && (
Start /MIN "" "C:\Program Files\Pervasive Software\PSQL\bin\pvkillwg.exe"
)
tasklist | find "w3dbsmgr.exe" || (
start "" "C:\P3WIN\P3PROGS\P3.exe" /user:chuck
)

Now, one final tweak and it will be "perfect" I want to add a "Start in:" directory similar to what you can do in shortcut properties (see attached). The directory would be on a server so it would be in the format of "\\server\etc\etc\etc"

Thanks again.



[recovering disk space - old attachment deleted by admin]
Title: Re: Batch File If Statement
Post by: oldun on April 05, 2011, 03:28:15 AM
Add /D server_path to the Start command
Title: Re: Batch File If Statement
Post by: KCuHCeht on April 05, 2011, 05:28:50 AM
Where in the start command?

I've tried this:
Code: [Select]
start "" "C:\Program Files\Timberline Office\Accounting\IA.exe" /D "\\server2\system\tsgold\lantz\"and this:
Code: [Select]
start "" "C:\Program Files\Timberline Office\Accounting\IA.exe" /D \\server2\system\tsgold\lantz\
Thanks.
Title: Re: Batch File If Statement
Post by: oldun on April 05, 2011, 06:29:13 AM
start "" /D   \\server2\system\tsgold\lantz\ "C:\Program Files\Timberline Office\Accounting\IA.exe"
Title: Re: Batch File If Statement
Post by: KCuHCeht on April 05, 2011, 06:37:49 AM
Wonderful. Thanks a lot for everyone's help. It is working like a charm.
Title: Re: Batch File If Statement
Post by: mat123 on April 15, 2011, 05:28:07 PM
btw the syntax for if is

if a==b command

just that
Title: Re: Batch File If Statement
Post by: Salmon Trout on April 15, 2011, 11:59:40 PM
btw the syntax for if is

if a==b command

just that

This isn't what was being asked.
Title: Re: Batch File If Statement
Post by: mat123 on April 16, 2011, 07:42:04 AM
i know i just wanted to point that out
Title: Re: Batch File If Statement
Post by: Salmon Trout on April 16, 2011, 07:55:43 AM
i know i just wanted to point that out

You could also mention that the sky is blue and that the sun goes away at night.