[fpc-pascal] Weird DosError values

Matthews dematthews at uwaterloo.ca
Tue Feb 17 00:06:13 CET 2004


Hello all.

I'm using a function and a procedure, both which deal with finding files in 
the same directory, and then either counting them, or reading them, but I'm 
getting some very weird values for DosError.  I'm using v1.0.10, and I 
continue to get a value of 183 from DosError in the function (which is in 
my unit), but a DosError value of 0 from the procedure inside the main 
file.  The code is below.
	
Does anybody know what DosError 183 means, and if so, can I avoid this in 
any way?

Thanks.

-Lukas M.

PS Does anybody have a more elegant solution to determine when there are no 
more files in a directory?  I had to use the workaround of duplicate 
filenames because DosError was always 0.

This is the setup:

var
     dir_in:string;
     dir_out:string;

Procedure Main;	
	begin
// Normally set from the command line, but dir_in and dir_out are always 
these values.
	        dir_in := 'C:\2dapad\in'; // This directory has 64 files in it 
(all 2DAs)
	        dir_out := 'C:\2dapad\pad';

                   num_files := NumberOfFiles(dir_in,'*.2da');
	        if num_files = 0 then EndProgram('There were no files.') // Just 
halts the program and writes a message on screen.
                   else if num_files = -1 then EndProgram('Unable to open ' 
+ dir_in)
                   else if num_files = -2 then EndProgram('DosError in ' + 
dir_in);
                   // Num_files always equals 1.
	        GetBatchFile;
	end;

Function NumberOfFiles(path,ext:string):longint;
// Searches through the passed directory (if one is given) for files of a 
particular extension.
// '' for ext = '*.*'
// Note: This is the function that does't work (the one in my unit)
      var
         i:longint;
         q:longint;
         curr,old:string;
         temp:TSearchRec;
         ex:string;
      begin
           i := 0;
           curr := GetCurrentDir;
           if (ext = '') OR (ReplaceChrStr(ext,'.','') = ext) then ex := '*.*'
           else ex := ext;
           if path <> '' then
              begin
                   GetShortName(path);
                   if SetCurrentDir(path) = FALSE then NumberOfFiles := -1; 
// If there's an error, exit soon.
              end;
           FindFirst(ex,anyfile,temp);
           q := DosError;
// Right here, this function ends, because DosError = 183.
           if q <> 0 then
              begin
                   writeln('DosError = ' + IntToStr(q));
                   repeat until readkey <> '';
                   NumberOfFiles := -2;
              end;
           inc(i);
           old := temp.name;
           FindNext(temp);
           q := DosError;
           if (q = 0) AND (temp.name <> old) then
              begin
                   inc(i);
                   old := '';
                   while ((old = '') OR (DosError = 0)) AND (temp.name <> 
old) do
                         begin
                              old := temp.Name;
                              FindNext(temp);
                              inc(i);
                         end;
              end;
           FindClose(temp);
           GetShortName(curr);
           if (path <> '') then SetCurrentDir(curr);
           NumberOfFiles := i;
      end;

// This the function in the main file, that does work.
// pads_skipped and pads_created are just global counters for number of 
files already dealth with.
// dir_out is also set by the user.
Procedure GetBatchFile;
      var
         f:tSearchRec;
         curr:string;
         i:integer;
         label batch_search;
      begin
           batch_search: // I know I'm bad...
           if name_in <> '' then last_file := ExtractFileName(name_in) else 
last_file := '';
           curr := GetCurrentDir;
           GetShortName(dir_in);
           if SetCurrentDir(dir_in) = FALSE then EndProgram('Error: Unable 
to change directory from ' + GetCurrentDir + ' to ' + dir_in);
           i := 0;
           FindFirst('*.2da',anyfile,f);
           i := DosError;
// Right here, and later, DosError is always 0.
           if i <> 0 then
              begin
                   Log('DosError = ' + IntToStr(i));
                   name_in := '??_BATCH_END';
                   FindClose(f);
                   GetShortName(curr);
                   SetCurrentDir(curr);
                   exit;
              end;
           i := 0;
           while (i < (pads_created + pads_skipped)) AND (DosError = 0) do 
// Keep going until a new file is found (the nth file in the list.)
                 begin
                      FindNext(f);
                      inc(i);
                 end;
           if (DosError <> 0) OR (last_file = f.Name) then
              begin
                   name_in := '??_BATCH_END';
                   FindClose(f);
                   GetShortName(curr);
                   SetCurrentDir(curr);
                   exit;
              end
           else
               begin
                    GetShortName(curr);
                    SetCurrentDir(curr);
                    name_in := dir_in + f.Name;
                    FindClose(f);
                    if dir_in[length(dir_in)] <> '\' then 
insert('\',name_in,length(dir_in)+1);
                    name_out := dir_out + 
ChangeFileExt(ExtractFileName(name_in),'.pad');
                    if dir_out[length(dir_out)] <> '\' then 
insert('\',name_out,length(dir_out)+1);
                    if (FileExists(name_out)) AND (overwrite = FALSE) then
                       begin
                            inc(pads_skipped);
                            Log('PAD for ' + ExtractFileName(name_in) + ' 
already exists - skipped.');
                            goto batch_search;
                       end;
               end;
      end;





More information about the fpc-pascal mailing list