Search the FAQ Archives

3 - A - B - C - D - E - F - G - H - I - J - K - L - M
N - O - P - Q - R - S - T - U - V - W - X - Y - Z
faqs.org - Internet FAQ Archives

comp.os.msdos.programmer FAQ part 3/5
Section - - How can a batch file test existence of a directory?

( Part1 - Part2 - Part3 - Part4 - Part5 - Single Page )
[ Usenet FAQs | Web FAQs | Documents | RFC Index | Neighborhoods ]


Top Document: comp.os.msdos.programmer FAQ part 3/5
Previous Document: - How can I disable access to a drive?
Next Document: - Why won't my C program open a file with a path?
See reader questions & answers on this topic! - Help others by sharing your knowledge

 The standard way, which in fact is documented in the DOS manual, is:

 if exist d:\path\nul goto found

 Unfortunately, this is not entirely reliable. I found it failed in
 Pathworks (a/k/a PCSA, DEC's network that connects PCs and VAXes), or on
 a MARS box that uses an OEM version of MS-DOS 5.0. Readers have reported
 that it gave the wrong answer on Novell networks, on DR-DOS, and in a
 DOS window under OS/2. By "failed" I mean that it "found" a directory
 that didn't exist, or failed to find one that did exist, or both. (It
 has been reported that IBM fixed the OS/2 bug in version 2.11 of OS/2.)
 As a legacy from earlier versions of DOS it always succeeds if the path
 is DEV.

 There appears to be no foolproof way to use pure batch commands to test
 for existence of a directory. The real solution is to write a program,
 which returns a value that your batch program can then test with an "if
 errorlevel". Reader Duncan Murdoch kindly posted the following Turbo
 Pascal version:

   program existdir;
   { Confirms  the  existence of a directory  given  on  the
     command  line.  Returns errorlevel 2 on error,  1  if  not
     found, 0 if found. }
  
   uses dos;
  
   var
     s : searchrec;
  
   begin
     if paramcount <> 1 then
       begin
         writeln('Syntax:  EXISTDIR directory');
         halt(2);
       end
     else
       begin
         findfirst(paramstr(1),Directory,S);
         while (Doserror = 0) and ((Directory and S.Attr) =  0) do
           findnext(S);
  
        if Doserror <> 0 then
          begin
            Writeln('Directory not found.');
            halt(1);
          end
        else
          begin
            Writeln('Directory found.');
            halt(0);
          end;
       end;
   end.

 Timo Salmi also has a Turbo Pascal version in his Turbo Pascal FAQ,
 which is downloadable as <ftp://garbo.uwasa.fi/pc/link/>

User Contributions:

Comment about this article, ask questions, or add new information about this topic:




Top Document: comp.os.msdos.programmer FAQ part 3/5
Previous Document: - How can I disable access to a drive?
Next Document: - Why won't my C program open a file with a path?

Part1 - Part2 - Part3 - Part4 - Part5 - Single Page

[ Usenet FAQs | Web FAQs | Documents | RFC Index ]

Send corrections/additions to the FAQ Maintainer:
jeffrey@carlyle.org (Jeffrey Carlyle)





Last Update March 27 2014 @ 02:11 PM