Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch File If Statement  (Read 11259 times)

0 Members and 1 Guest are viewing this topic.

KCuHCeht

    Topic Starter


    Greenhorn

    • Experience: Beginner
    • OS: Unknown
    Batch File If Statement
    « 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

    Salmon Trout

    • Guest
    Re: Batch File If Statement
    « Reply #1 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

    KCuHCeht

      Topic Starter


      Greenhorn

      • Experience: Beginner
      • OS: Unknown
      Re: Batch File If Statement
      « Reply #2 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.

      Salmon Trout

      • Guest
      Re: Batch File If Statement
      « Reply #3 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


      KCuHCeht

        Topic Starter


        Greenhorn

        • Experience: Beginner
        • OS: Unknown
        Re: Batch File If Statement
        « Reply #4 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?

        Salmon Trout

        • Guest
        Re: Batch File If Statement
        « Reply #5 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.
        « Last Edit: April 01, 2011, 02:05:24 PM by Salmon Trout »

        KCuHCeht

          Topic Starter


          Greenhorn

          • Experience: Beginner
          • OS: Unknown
          Re: Batch File If Statement
          « Reply #6 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"

          KCuHCeht

            Topic Starter


            Greenhorn

            • Experience: Beginner
            • OS: Unknown
            Re: Batch File If Statement
            « Reply #7 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  :)

            KCuHCeht

              Topic Starter


              Greenhorn

              • Experience: Beginner
              • OS: Unknown
              Re: Batch File If Statement
              « Reply #8 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]

              oldun

              • Guest
              Re: Batch File If Statement
              « Reply #9 on: April 05, 2011, 03:28:15 AM »
              Add /D server_path to the Start command

              KCuHCeht

                Topic Starter


                Greenhorn

                • Experience: Beginner
                • OS: Unknown
                Re: Batch File If Statement
                « Reply #10 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.

                oldun

                • Guest
                Re: Batch File If Statement
                « Reply #11 on: April 05, 2011, 06:29:13 AM »
                start "" /D   \\server2\system\tsgold\lantz\ "C:\Program Files\Timberline Office\Accounting\IA.exe"

                KCuHCeht

                  Topic Starter


                  Greenhorn

                  • Experience: Beginner
                  • OS: Unknown
                  Re: Batch File If Statement
                  « Reply #12 on: April 05, 2011, 06:37:49 AM »
                  Wonderful. Thanks a lot for everyone's help. It is working like a charm.

                  mat123



                    Hopeful

                    Thanked: 16
                    • Yes
                    • Yes
                    • Yes
                  • Experience: Familiar
                  • OS: Windows XP
                  Re: Batch File If Statement
                  « Reply #13 on: April 15, 2011, 05:28:07 PM »
                  btw the syntax for if is

                  if a==b command

                  just that



                  Salmon Trout

                  • Guest
                  Re: Batch File If Statement
                  « Reply #14 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.