Article View: comp.os.msdos.misc
Article #3926Re: overcoming the MSDOS errorlevel problem
From: Robert B. Clark
Date: Sat, 16 Dec 1995 00:00
Date: Sat, 16 Dec 1995 00:00
52 lines
2809 bytes
2809 bytes
John Savage <koala@sydney.dialix.com.au> wrote: >Amongst the myriad shortcomings of MSDOS we must include the pair: > > * FIND does not return an errorlevel to confirm a match > * COPY does not copy a file of length 0 Actually, FIND does return an errorlevel in MS-DOS v6 or later: 0 At least one match was found. 1 No matches were found. 2 An error occurred during the search. If the target audience is running v6+, you can dispense with the COPY trick altogether, as is shown below: find "rem " /i < \autoexec.bat > nul if not errorlevel 1 echo There were REMarked lines in your AUTOEXEC.BAT. Note: FIND in MS-DOS v5+ includes the /I switch, which performs a case-insensitive match on the search string. >type %1 | find "xyz" >tmp1 >copy tmp1 tmp0 > \nul >if exist tmp0 echo %1 contained the string xyz Here's a suggestion for a little extra performance. Under MS-DOS, copying a zero-byte file actually results in the deletion of the target file. So .... find "xyz" < %1 > tmp1 copy /b tmp1+,, > nul if exist tmp1 echo %1 contained the string "xyz". if exist tmp1 del tmp1 > nul By copying the semaphore file onto itself, we don't have to delete it if the test turns out negative, and we don't have to worry about deleting it beforehand. Of course, in a small batch like this one the savings are negligible; however, if this were a long, complicated batch file we could use an evar to hold the result of the first "if exist" test, and then simply use that evar throughout in order to avoid extra disk accesses. Also, I used redirection instead of a pipe to FIND in order to save a temp file--a pipe creates two temp files per instance. Again, this is a minor point in this example, but could add up to large savings in more extensive batch programs. -- Robert B. Clark <rclark@iquest.net> "Anyone who considers protocol unimportant has never dealt with a cat." --L. Long
Technical Details
Message-ID:
<30d260e6.16907452@news.iquest.net>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<4aou3r$h7o$1@sydney.DIALix.oz.au>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10d683,b04f11c9c214bbc0 X-Google-Attributes: gid10d683,public X-Google-Thread: f53ba,b04f11c9c214bbc0 X-Google-Attributes: gidf53ba,public X-Google-Thread: 104805,b04f11c9c214bbc0 X-Google-Attributes: gid104805,public X-Google-Thread: f71eb,b04f11c9c214bbc0 X-Google-Attributes: gidf71eb,public From: rclark@iquest.net (Robert B. Clark) Subject: Re: overcoming the MSDOS errorlevel problem Date: 1995/12/16 Message-ID: <30d260e6.16907452@news.iquest.net> X-Deja-AN: 122011267 sender: news@iquest.net (News Admin) x-nntp-posting-host: ts05-ind-13.iquest.net references: <4aou3r$h7o$1@sydney.DIALix.oz.au> followup-to: comp.os.msdos.misc,alt.msdos.batch organization: IQuest Internet, Inc. newsgroups: comp.os.msdos.misc,alt.msdos.batch,comp.sys.ibm.pc.misc,aus.computers.ibm-pc