?
Solved

retrieving a file in a 'random' way

Posted on 2003-02-25
9
Medium Priority
?
175 Views
Last Modified: 2010-04-04
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
Comment
Question by:aztec
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 

Expert Comment

by:headgeek
ID: 8022869
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
 

Author Comment

by:aztec
ID: 8022890
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
 

Expert Comment

by:headgeek
ID: 8022944
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:aztec
ID: 8022962
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
 

Expert Comment

by:headgeek
ID: 8023058
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
 

Expert Comment

by:headgeek
ID: 8023111
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
 
LVL 17

Expert Comment

by:geobul
ID: 8023244
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
 
LVL 1

Accepted Solution

by:
Korsar_13 earned 200 total points
ID: 8027529
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
 

Author Comment

by:aztec
ID: 8527947
Thanks Korsar
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question