Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!van-bc!ubc-cs!alberta!mts.ucs.UAlberta.CA!Al_Dunbar From: userAKDU@mts.ucs.UAlberta.CA (Al Dunbar) Newsgroups: comp.os.msdos.misc Subject: Re: Getting rid of "syntax error" from .bat files - how to? Message-ID: <2002@mts.ucs.UAlberta.CA> Date: 11 Dec 90 05:44:20 GMT References: <9467@pasteur.Berkeley.EDU> <5649@uafhp.uark.edu> <635@sun4dts.dts.ine.philips.nl> Organization: MTS Univ of Alberta Lines: 56 In article <635@sun4dts.dts.ine.philips.nl>, derek@sun4dts.dts.ine.philips.nl (derek) writes: >sab@engr.uark.edu (Steven A. Breuer) writes: > >>In article <9467@pasteur.Berkeley.EDU>, brand@cad.Berkeley.EDU (Graham Brand) writes: >>> I have written a batch file which accepts parameters from the command >>> line using the: >>> if %1==param1 >>> syntax. It works fine except that if I don't pass any parameters, I >>> get the above syntax error message. Is there any way to suppress this? <<>> > >You can also use the following trick that I often use: > > if "%1"=="" goto NOPARMS > > goto %1 > > rem here handle incorrect parameters. > >The "goto %1" branches to the label passed as parameter 1. This is especially >useful in a batch file calling itself. (Think about it!) > Still redundant. Try: goto _%1 rem handle incorrect parameters :_ rem process the NOPARMS case here :_asm rem process an argument of 'asm', 'Asm', 'ASM' or etc. Using a variable as a label makes argument processing case insensitive, in addition to reducing the number of IF clauses used. In addition, prefixing label names with '_' adds two more benefits: 1) no need to handle the NOPARMS case separately, 2) makes it possible to go only to those labels designated for this purpose. I always get an error when no label is found to match the parameter, aborting the batch file before it can handle the incorrect parameters. This is acceptable if these label parameters are generated by the batch file itself, but if used to process (random) input from users you want to protect from this kind of thing, try: if exist "argcheck"_%1 goto _%1 echo invalid argument: "%1" -------------------+------------------------------------------- Al Dunbar | Edmonton, Alberta | "this mind left intentionally blank" CANADA | - Manuel Writer -------------------+-------------------------------------------