home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / files.swg / 0077_Updated File Exists.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-09-04  |  2.5 KB  |  65 lines

  1. (*
  2. In an earlier release of SWAG Andrew Eigus provided code for a fast
  3. 'FileExist' function.
  4.  
  5. The function however has a potential bug in that the filename passed
  6. into the function must be ASCIIZ (null terminated) to work correctly.
  7. It will also return a TRUE result for a directory name or the volume ID.
  8.  
  9. My 'EntryExists' function below includes the extra code to convert the
  10. filename to ASCIIZ and has been optimised for speed slightly by replacing
  11. the Jump instruction.
  12.  
  13. My 'FileExists' function below is an extended version which returns TRUE
  14. for files only (not a directory entry or volume ID).
  15.  
  16. *)
  17.  
  18. FUNCTION EntryExists(FileName : String) : Boolean; ASSEMBLER;
  19. ASM
  20.   PUSH DS          {Save DS                         }
  21.   LDS  SI,Filename {DS:SI => Filename               }
  22.   XOR  BX,BX       {Clear BX                        }
  23.   MOV  BL,[SI]     {BX = Length(Filename)           }
  24.   INC  SI          {DS:SI => Filename[1]            }
  25.   MOV  DX,SI       {DS:DX => Filename[1]            }
  26.   MOV  [SI+BX],BH  {Append Ascii 0 to Filename      }
  27.   MOV  AX,4300h    {Get Attribute Function Code     }
  28.   INT  21h         {Get File Attributes             }
  29.   MOV  AL,BH       {Set Default Result to FALSE     }
  30.   CMC              {Toggle Carry Flag               }
  31.   ADC  AL,AL       {Change Result to TRUE if Failed }
  32.   POP  DS          {Restore DS                      }
  33. END; {EntryExists}
  34.  
  35. FUNCTION FileExists(FileName : String) : Boolean; ASSEMBLER;
  36. ASM
  37.   PUSH DS          {Save DS                         }
  38.   LDS  SI,Filename {DS:SI => Filename               }
  39.   XOR  BX,BX       {Clear BX                        }
  40.   MOV  BL,[SI]     {BX = Length(Filename)           }
  41.   INC  SI          {DS:SI => Filename[1]            }
  42.   MOV  DX,SI       {DS:DX => Filename[1]            }
  43.   MOV  [SI+BX],BH  {Append Ascii 0 to Filename      }
  44.   MOV  AX,4300h    {Get Attribute Function Code     }
  45.   INT  21h         {Get File Attributes             }
  46.   MOV  AL,BH       {Default Result = FALSE          }
  47.   ADC  CL,CL       {Attribute * 2 + Carry Flag      }
  48.   AND  CL,31h      {Directory or VolumeID or Failed }
  49.   JNZ  @@Done      {Yes - Exit                      }
  50.   INC  AL          {No - Change Result to TRUE      }
  51. @@Done:
  52.   POP  DS          {Restore DS                      }
  53. END; {FileExists}
  54.  
  55. CONST
  56.   Found : ARRAY[Boolean] OF String[10] = ('Not Found', 'Found');
  57. VAR
  58.   FileName : String;
  59.  
  60. BEGIN
  61.   Write('Enter file name to search: ');
  62.   ReadLn(FileName);
  63.   WriteLn('File "', FileName, '" ', Found[EntryExists(FileName)], '.');
  64. END.
  65.