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

Author Topic: Batch file programming  (Read 2575 times)

0 Members and 1 Guest are viewing this topic.

kukua

  • Guest
Batch file programming
« on: October 19, 2006, 12:11:13 PM »
I am putting some error checks into to some batch files that I inherited and need some help with the following:

if not exist \\server\share\folder\%1.txt
   echo cute message
   pause
   goto :finish

Could anyone tell me why this is not working?  The share and server exist.  What is happening is it is falling through and processing the code that follows.  I have deliberately enter an incorrect parameter to see if this section works.

 :-?

2k_dummy



    Specialist
  • A word, once spoken, can never be recalled.
  • Thanked: 14
    Re: Batch file programming
    « Reply #1 on: October 19, 2006, 02:02:25 PM »
    if not exist \\server\share\folder\%1.txt echo "cute message" goto :finish
    echo "another cute message"
    pause
    goto :finish
      
    If you don't stand for something, you'll fall for anything.
    _______________________________________ ________
    BlackViper

    Software and utilities

    GuruGary



      Adviser
      Re: Batch file programming
      « Reply #2 on: October 19, 2006, 11:58:57 PM »
      I think there is a basic logic issue with the code.  I think 2k_dummy has the right idea, but it looks like there may be a syntax issue with his code.  Assuming you are running Windows 2000 / XP / 2003 / Vista, you could use the following:
      if not exist \\server\share\folder\%1.txt [highlight]([/highlight]
         echo cute message
         pause
         goto :finish
         [highlight])[/highlight]

      2k_dummy



        Specialist
      • A word, once spoken, can never be recalled.
      • Thanked: 14
        Re: Batch file programming
        « Reply #3 on: October 20, 2006, 02:48:39 AM »
        I assumed "inherited" batch files were for real DOS and were real .bat files, rather than .cmd files as used by XP. Real DOS doesn't have XP's CMD extensions and the syntax is very different. Although CMD will run most .bat files, real DOS will rarely run .cmd files. Batch files in XP should actually be saved with a .cmd extension, and only those intended to run under DOS command.com should be saved with a .bat extension. It seems that, judging from most questions posed on this board, most people don't know the difference between a .cmd file and .bat file.
        If you don't stand for something, you'll fall for anything.
        _______________________________________ ________
        BlackViper

        Software and utilities

        kukua

        • Guest
        Re: Batch file programming
        « Reply #4 on: October 20, 2006, 07:13:09 AM »
        I tried GuruGary's suggestion but it didn't work.  Could you come back at me.

        kukua

        • Guest
        Re: Batch file programming
        « Reply #5 on: October 20, 2006, 07:21:00 AM »
        It's me again.  It worked.... I had another error checking routine in the batch file and I switched the order and voila!  Don't ask me why.

        QBasicMac

        • Guest
        Re: Batch file programming
        « Reply #6 on: October 20, 2006, 09:19:14 AM »
        Quote
        Could anyone tell me why this is not working?

        Well, you probably guessed this from GuruGary's correct response, but anyway here is an explicit answer to your question.

        command ---> if not exist \\server\share\folder\%1.txt

        tells BAT that if the file does not exist, do nothing
        which is what will also happen if the file does exist.

        After that command finishes doing nothing, it goes on to the next instruction.
        command ---> echo cute message

        So instead, you want to tell it to do SOMETHING.
        command ---> if not exist \\server\share\folder\%1.txt (
        tells BAT that if the files exists, do commands until ")" and if the file does not exist, skip everything until the next ")".

        Mac

        [Well, the parentheses can be nested, so interpret my word "next" in that context]
        « Last Edit: October 20, 2006, 09:21:33 AM by QBasicMac »