[ Usenet FAQs | Web FAQs | Documents | RFC Index ]
    Search the FAQ Archives

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

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?


- How can a batch file test existence of a directory?



 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/tsfaqp.zip>



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 October 22 2009 @ 05:28 AM

Some parts © 2009 Advameg, Inc.