Random filenames in BTP7.0 
Author Message
 Random filenames in BTP7.0

Quote:

> Help!
> I am having the hardest time creating simple random filenames.  I know
> about the Randomize function but how do I specify the format that the
> random characters should take?
> Any help,clues,tips,snips would be gratefully appreciated.
> thanks,
> -shawn

There is a DOS interrupt which will give you a guaranteed random
filename.  It's interrupt 21h, Function ???.  Look at any interrupt
reference for DOS.


Wed, 18 Jun 1902 08:00:00 GMT  
 Random filenames in BTP7.0

Quote:

> Help!
> I am having the hardest time creating simple random filenames.  I know
> about the Randomize function but how do I specify the format that the
> random characters should take?
> Any help,clues,tips,snips would be gratefully appreciated.
> thanks,
> -shawn

Well, dos file names use anywhere from 1 to 11 characters, at least one
of which must be in the prefix. The prefix uses 1 to 8 characters, and
the extention uses 1 to 3 characters. But I guess that's not your
question. I am working on a sorting program which creates a randomly
named temporary directory. I've discovered that there are 51 characters
which Dos will allow you to use in a filename or directory name. Here
they are.


If your problem is using the ranomdize function, then consider the
following program.

program randname;

var i,j,k:integer;
    fname:string[11];
begin
 randomize;
 j:=random(8)+1;
 for i:=1 to j do begin
  k:=random(51)+1;
  fname:=fname+copy(good,k,1);
 end;
 j:=random(4);
 if(j>0) then begin {Put on extention if j>0}
  fname:=fname+'.';
  for i:=1 to j do begin
   fname:=fname+copy(good,k,1);
  end;
 end;
 writeln('Randomly chosen filename is ',fname);
end.

Hope this helps.
-Patrick-



Wed, 18 Jun 1902 08:00:00 GMT  
 Random filenames in BTP7.0




Quote:
>If your problem is using the ranomdize function, then consider the
>following program.

>program randname;

>var i,j,k:integer;
>    fname:string[11];
>begin
> randomize;
> j:=random(8)+1;
> for i:=1 to j do begin
>  k:=random(51)+1;
>  fname:=fname+copy(good,k,1);
> end;
> j:=random(4);
> if(j>0) then begin {Put on extention if j>0}
>  fname:=fname+'.';
>  for i:=1 to j do begin
>   fname:=fname+copy(good,k,1);
>  end;
> end;
> writeln('Randomly chosen filename is ',fname);
>end.

program randname ;  {$IFDEF WINDOWS} uses WinCrt ; {$ENDIF}
var j : word ; fname : string[12] ;
procedure F(R : shortint) ;

    Chars : string [Length(good)] = good ;
  var i : shortint ;
  begin for i := 0 to R do fname := fname+Chars[Succ(Random(41))] end ;
begin
randomize ;
for j := 1 to 15 do begin fname := '' ;
  F(random(8)) ; fname := fname+'.' ; F(shortint(random(4))-1) ;
  writeln('Randomly chosen filename is ', fname) ;
  end {j} ;
end.

N.B.
You didn't first clear fname.
You didn't randomise the extension characters.
"filename" and "filename." are equivalent, except in contexts which
  add a default extension.
"fname" is [12] is "NameStr".
Copy( , , 1) can often be improved on.
"51" should be "41" should be Length(good).

There remains the question of whether the filename is truly random, as
the probability of getting "R" is the same as that of getting
"R???????.???".
Actually, I'd go for an 8.3 name each time, with either the 8. or .3
parts fixed; that could ensure no clash with other files (in other
words, the original question was not the best).
Mine makes 15 tests.

Newsgroups trimmed; this is a TP/BP question.

--

  Web URL: http://www.merlyn.demon.co.uk/ -- includes FAQqish topics and links.
  Correct 4-line sig separator is as above, a line comprising "-- " (SoRFC1036)
  Before a reply, quote with ">" / "> ", known to good news readers (SoRFC1036)



Wed, 18 Jun 1902 08:00:00 GMT  
 Random filenames in BTP7.0

On Wed, 02 Jul 1997 16:08:25 -0700, "Patrick D. Rockwell"

This routine off course should be called within a loop like this:
Repeat
  NewName := RandName;
Until NOT(FileExists(NewName));

Marco.

Quote:

>> Help!
>> I am having the hardest time creating simple random filenames.  I know
>> about the Randomize function but how do I specify the format that the
>> random characters should take?
>> Any help,clues,tips,snips would be gratefully appreciated.
>> thanks,
>> -shawn

>Well, dos file names use anywhere from 1 to 11 characters, at least one
>of which must be in the prefix. The prefix uses 1 to 8 characters, and
>the extention uses 1 to 3 characters. But I guess that's not your
>question. I am working on a sorting program which creates a randomly
>named temporary directory. I've discovered that there are 51 characters
>which Dos will allow you to use in a filename or directory name. Here
>they are.


>If your problem is using the ranomdize function, then consider the
>following program.

>program randname;

>var i,j,k:integer;
>    fname:string[11];
>begin
> randomize;
> j:=random(8)+1;
> for i:=1 to j do begin
>  k:=random(51)+1;
>  fname:=fname+copy(good,k,1);
> end;
> j:=random(4);
> if(j>0) then begin {Put on extention if j>0}
>  fname:=fname+'.';
>  for i:=1 to j do begin
>   fname:=fname+copy(good,k,1);
>  end;
> end;
> writeln('Randomly chosen filename is ',fname);
>end.

>Hope this helps.
>-Patrick-



Wed, 18 Jun 1902 08:00:00 GMT  
 Random filenames in BTP7.0

Quote:
> Help!
> I am having the hardest time creating simple random filenames.  I know
> about the Randomize function but how do I specify the format that the
> random characters should take?
> Any help,clues,tips,snips would be gratefully appreciated.
> thanks,
> -shawn


http://www.lesobank.ru)

uses Dos;

const
  TmpExt : String[4] = '.TMP';
  Prefix : String[5] = '$SWP';

var
  TMPF : String;
  F : Text;

  function GetTempDir : String;
  begin
    GetTempDir := GetEnv('TMP');
  end;

  function MakeTempFilename(TD : String) : String;
  var
    TmpName : String[12];
    i : Byte;
    DIR : SearchRec;

  begin
    repeat
      TmpName := '';
      for i := 1 to 8 do
        TmpName := TmpName + Char(Random(26)+65);
      TmpName := TmpName + TmpExt;
      move(Prefix[1], TmpName[1], Byte(Prefix[0]));
      MakeTempFilename := TD + '\' + TmpName;
      FindFirst(TD + '\' + TmpName, AnyFile, DIR);
      if DosError = 0 then Write(^G);
    until DosError <> 0;
  end;

begin
  Randomize;
  repeat
    TMPF := MakeTempFilename(GetTempDir);
    Assign(F, TMPF);
    ReWrite(F);
    Close(F);
    WriteLn(TMPF);
  until false;
end.



Wed, 18 Jun 1902 08:00:00 GMT  
 Random filenames in BTP7.0

Quote:

>On Wed, 02 Jul 1997 16:08:25 -0700, "Patrick D. Rockwell"

>This routine off course should be called within a loop like this:
..SNIP...

>>If your problem is using the ranomdize function, then consider the
>>following program.

>>program randname;


..SNIP...

You might want to consider cross platform functionality... the '~' character
could cause problems with Linux and other UNIXs'.  IMHO, stick with
alpha-numerics.

Later...
Ken L.



Wed, 18 Jun 1902 08:00:00 GMT  
 Random filenames in BTP7.0

Quote:
> > Help!
> > I am having the hardest time creating simple random filenames.  I know
> > about the Randomize function but how do I specify the format that the
> > random characters should take?
> > Any help,clues,tips,snips would be gratefully appreciated.
> > thanks,
> > -shawn

A year ago I had the same problem. Robert B. Clark had a great solution
(thanks again, this routine has served me very well).

The only problem I found with it is, that if you call the routine twice
in a very short time (under 1 sec), which could happen on a RAM drive
when you're not actually creating the file, the same filename could come
up twice. I added a delay (not implemented in the posted original
below).

Kurt

------------------------------------------------------------------------

Quote:

>Using Turbo Pascal 7.0 for DOS, I need to make several files in a
>directory, where the same program may have created files before. I
>thought there was some kind of function that would find me an unused
>filename, but I can't find anything. Does anybody know of such a
>function or will I have to make some algorithm for calculating a
>filename from the current time or so and check whether the filename is

You could easily write a tmpnam()-type function (as found in C) where
you supply a three- or four-letter base name and the function appends a
numeric suffix from 0000 to 9999, using an iterative
findfirst()/findnext() loop and incrementing the suffix until you find a
unique filename.  For example, tmpnam('\TEMP\TMP$') would return
'\TEMP\TMP$0003.TMP' if TMP$0001.TMP and TMP$0002.TMP already existed.

Alternatively, you could use the DOS interrupt 21/5a, which creates a
temporary file whose name is based upon the current time and date and
consists of eight alphabetic characters. These filenames are of the same
form as the ones that DOS creates during a piping operation (e.g., dir |
more).

Here's some code that utilizes the latter idea:

program UniqueName;
{
  UNIQUE.PAS - Written 8 September 1995   TP 4.0 code
  Makes a unique DOS filename using Int 21/5ah.

Quote:
}

Uses Dos;

TYPE asciiz = array [0..63] of char;
     charptr = ^char;

procedure IncPtr(var p: charptr);
begin
   p:=Ptr(Seg(p^),Ofs(p^)+1)
end;

PROCEDURE ASCIIZtoStr(zptr: charptr; var pStr: string);
{ Converts ASCIIZ pointed to by zptr to Pascal string pstr}
var pptr: charPtr;
    len: integer;
begin
   pptr:=Ptr(Seg(pStr),Ofs(pStr)+1);
   len:=0;
   while (zptr^ <> #0) do
   begin
      pptr^:=zptr^;
      IncPtr(pptr); IncPtr(zptr); Inc(len);
   end;
   pStr[0]:=Chr(len)
end; {ASCIIZtoStr}

FUNCTION UniqueFileName(var fname: string): boolean;
{ Req. DOS 3+. Names and creates a unique filename using Int 21/5A. }
{ These names are the same as those used in DOS piping operations.  }
var zfname: asciiz;
    i,len: integer;
    regs: registers;

begin
   if (length(fname)>50) then      { File path too long }
      UniqueFileName:=false
   else
   begin
      len:=length(fname);
      move(fname[1],zfname[0],len);  { Make an ASCIIZ string of fname }
      if (zfname[len-1] <> '\') then
      begin
         zfname[len-1]:='\';         { Terminate path }
         inc(len)
      end;
      for i:=0 to 12 do
         zfname[len+i]:=#0;     { Leave room for ASCIIZ filename spec }

      regs.AH := $5a;           { Want Int 21/5ah - Create temp file }
      regs.CX := $00;           { Normal attributes }
      regs.DS := Seg(zfname);   { Address of ASCIIZ filename buffer }
      regs.DX := Ofs(zfname);
      MsDos(regs);            { Returns AX=handle r/w compat, mode }
                              {         DS:DX = pathname + filename }
                              { CF set if error, AX=ext. errorcode }

      if (regs.Flags and FCarry)=0 then
      begin
         regs.BX := regs.AX;
         regs.AH := $3e;        { Close file handle in BX - DOS 2+ }
         MsDos(regs);           { CF set if error; ignored here }
         UniqueFileName:=true
      end
      else UniqueFileName:=false; { Error opening temp file }

   end;                           { but you may not have been able }
end; {UniqueFileName}             { to open it }

VAR foospec: string;
    f: TEXT;

BEGIN
   foospec:='a:\temp\';
   if (UniqueFileName(foospec)) then
   begin
      Assign(f,foospec);
      ReWrite(f);
      writeln('Writing to temp file "',foospec,'"...');
      writeln(f,'This text is written to "',foospec,'"');
      close(f)
   end
   else writeln('Error in UniqueFileName("',foospec,'")')
END.

===========================================================================

 PGP key available upon request.     |  Indiana, but don't you believe it."
===========================================================================

--

                         http://www.pinboard.com/kurt/   private



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Random filenames in BTP7.0

2. BP7 questions about random filenames and text input

3. BP7 questions about random filenames and text input

4. BP7 questions about random filenames and text input

5. BP7 questions about random filenames and text input

6. Server/socket code for BTP7

7. Random isn't random?

8. Random where no random should be!

9. Random isn't random?

10. Long FileName - Short Filename conversion

11. Unknown filenames/unit filenames

12. If Exist(filename) alway returns true on new faster computer

 

 
Powered by phpBB® Forum Software