• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 176
  • Last Modified:

retrieving a file in a 'random' way

Hi...
   My app reads and processes data files (files ending in a .dat extension) from a certain directory on disk. I currently do it this way:


if FindFirst(homedir+'tempfile\*.dat',faArchive,tsrch)=0 then
  begin  {* find the first available data file *}
    hfname:=tsrch.Name;
    delete(hfname, length(hfname)-2, 3);
    hfname:=hfname+'use'); //rename to show it's in use
    renamefile(homedir+'tempfile\'+tsrch.Name, homedir+'tempfile\'+hfname);
    assignfile(tinfile, homedir+'tempfile\'+hfname);
    reset(tinfile);
  end;
  SysUtils.FindClose(tsrch);


...this works fine, but it always gets the very *next* file in the list (ie. if file 01 and 02 have been done, it will get 03). I want to be able to get the next data file in a 'random' way. In other words, if there was 50 data files, it would get 04 then 28, then 13, etc.

Is there a *neat* way to do this?

Thanks
   Shawn
0
aztec
Asked:
aztec
1 Solution
 
headgeekCommented:
You will need to find the number of files in the directory then use a random call.

something like

 Randomize;
 RandFile := randomRange(0, NumFiles);

RandFile will bring back a random number which you can use to put your file together...
0
 
aztecAuthor Commented:
Yes, I had thought of this, but doing it this way would often return the number of a file which I *already* had processes... requiring this routine to be run again, and maybe several times in a row to get an "unused" file number. See what I mean?

What I need is a way to pick a random file quickly from the remaining "unused" data files.

Thanks  
   Shawn
0
 
headgeekCommented:
instead of renaming the file once its been read, move it to another folder?  Then you will always get a fresh file and won't have to loop.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
aztecAuthor Commented:
I don't see how this will make a difference, then it will still sometimes just generate a number of a file that has been moved, instaed of a number of a file that has been renamed to "use".
0
 
headgeekCommented:
ah sorry, yes.  How about putting the dir into an array then picking the array index instead of the filename, use the array length as the max range, refresh the array whenever you go to pick another file.  You could also use a list.
0
 
headgeekCommented:
ah sorry, yes.  How about putting the dir into an array then picking the array index instead of the filename, use the array length as the max range, refresh the array whenever you go to pick another file.  You could also use a list.
0
 
geobulCommented:
Hi Shawn,

Try something like:

var
  sl: TStringList;
  oriname: string;

implementation

{$R *.DFM}

// returns a filename or empty string if no file is found
function GetRandomFile(sPath,sMask: string): string;
var
  SRec  : TSearchRec;
  i: integer;
begin
  result := '';
  // fill a list of all files
  sl.Clear;
  if FindFirst(sPath+sMask,faArchive,SRec) = 0 then begin
    repeat
      sl.Add(SRec.Name);
    until FindNext(Srec) <> 0;
    FindClose(Srec);
  end;
  // get a random file
  if sl.Count > 0 then begin
    i := Trunc(Random(sl.Count)); // 0 <= i < sl.Count
    result := sPath + sl[i];
  end;
end;

...
  oriname := GetRandomFile(homedir+'tempfile\','*.dat');
  if oriname <> '' then begin
    hfname:=oriname+'use'); //rename to show it's in use
    renamefile(oriname, hfname);
    assignfile(tinfile, hfname);
    reset(tinfile);
    ...
  end;
...

initialization
  sl := TStringList.Create;
  Randomize;

finalization
  sl.Free;

end.

Regards, Geo
0
 
Korsar_13Commented:
if you need
a) touch fiules in random order
b) touch EACH file ONLY ONCE

then

1. Load all Filenames in TStringList;
2. for each element in list, exchange it with random element
3. mark current postion in list as 0

and function GetRandomFile simply return string in current position and move forward or return '' on end of list
0
 
aztecAuthor Commented:
Thanks Korsar
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now