FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour RANDOM FileName ?
Posts: 1772
Joined: Thu Sep 05, 2019 05:32 AM
RANDOM FileName ?
Posted: Sat Aug 13, 2022 12:23 AM
hi,

i know
Code (fw): Select all Collapse
GetTempFileName( LPCSTR tmpdir, LPCSTR prefix, UINT unique, LPSTR filename )

but it seems not available under harbour :-)

did somebody have made a HB_FUNC( GetTempFileName ) and can share it :-)

p.s. need "A" and/or "W" Function
greeting,

Jimmy
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: RANDOM FileName ?
Posted: Sat Aug 13, 2022 08:37 AM

Try cNewFileName() or cTempFile(). Look at source\function\filename.prg.

Posts: 1772
Joined: Thu Sep 05, 2019 05:32 AM
Re: RANDOM FileName ?
Posted: Sun Aug 14, 2022 05:21 PM
hi,
Enrico Maria Giordano wrote:Try cNewFileName() or cTempFile(). Look at source\function\filename.prg.

Ok,
Thx for Answer
greeting,

Jimmy
Posts: 1789
Joined: Tue Oct 11, 2005 05:01 PM
Re: RANDOM FileName ?
Posted: Mon Aug 15, 2022 04:35 PM
Code (fw): Select all Collapse
#include "fileio.ch"

FUNCTION TempFile( cDir, cExt, nAttr )

   LOCAL cName
   LOCAL fhnd

   IF HB_ISSTRING( cDir )
      cDir := hb_DirSepAdd( cDir )
   ENDIF

   IF HB_ISSTRING( cExt ) .AND. !( Left( cExt, 1 ) == "." )
      cExt := "." + cExt
   ENDIF

   hb_default( @nAttr, SetFCreate() )

   fhnd := hb_FTempCreateEx( @cName, cDir,, cExt, nAttr )

   IF fhnd != F_ERROR
      FClose( fhnd )
      RETURN cName
   ENDIF

   RETURN ""

this function, exist in in hbct lib, please try
Salu2

Carlos Vargas

Desde Managua, Nicaragua (CA)
Posts: 1772
Joined: Thu Sep 05, 2019 05:32 AM
Re: RANDOM FileName ?
Posted: Mon Aug 15, 2022 07:28 PM
hi
carlos vargas wrote:this function, exist in in hbct lib, please try

Aha, OK
Thx for Advice
greeting,

Jimmy
Posts: 2706
Joined: Fri Oct 07, 2005 01:50 PM
Re: RANDOM FileName ?
Posted: Mon Aug 15, 2022 07:32 PM
Been using this for years ... not complicated ..



Code (fw): Select all Collapse
cDEFA :="C"

DO WHILE .T.
   SITEDBF := "TEMP"+(SUBSTR(TIME(),7,2)+SUBSTR(TIME(),4,2))
   IF .not. FILE( cDefa+"\DBTMP\"+SITEDBF+".DBF" )
      EXIT
   ENDIF
ENDDO


Rick Lipkin
Posts: 1772
Joined: Thu Sep 05, 2019 05:32 AM
Re: RANDOM FileName ?
Posted: Mon Aug 15, 2022 07:39 PM
hi,
Rick Lipkin wrote:Been using this for years ... not complicated ..

Yes,
i have use something like this as Workaround.
Code (fw): Select all Collapse
cFile := "T"+DTOS(Date())+STRZERO(Seconds(),5)+".TXT"
greeting,

Jimmy
Posts: 6983
Joined: Fri Oct 07, 2005 07:07 PM
Re: RANDOM FileName ?
Posted: Tue Aug 16, 2022 05:58 AM
Dear Antonio,
can you please show how we can include this API function?

I tried different ways, but I am missing something. I get errors.

Best regards,
Otto

These are my tests:

Code (fw): Select all Collapse
function main


/*
UINT GetTempFileNameA(
  [in]  LPCSTR lpPathName,
  [in]  LPCSTR lpPrefixString,
  [in]  UINT   uUnique,
  [out] LPSTR  lpTempFileName
);
*/
? GetTmpFileName('c:\fwh\samples\',"ABC","123")

DLL32 Function GetTmpFileName  (  lpszPath As String,   lpPrefixString As String,   wUnique As Long,   lpTempFileName As String) As Long PASCAL ;
FROM "GetTempFileNameA" LIB "kernel32.dll" 


 /*
 
 #pragma BEGINDUMP

#include <stdio.h>
#include <hbapi.h>
#include <windows.h>

HB_FUNC( GETTEMPFILENAMEA )
{
     
  hb_retl( GetTempFileNameA( hb_parnl( 1 ), hb_parnl( 2 ), hb_parnl( 3 ), hb_parnl( 4 ) ) );
}
  

#pragma ENDDUMP

//----------------------------------------------------------------------------//
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: RANDOM FileName ?
Posted: Tue Aug 16, 2022 06:23 AM
Dear Otto,

Using dynamic linking:
Code (fw): Select all Collapse
#include "FiveWin.ch"

function Main()

  local cFileName := Space( 50 )

  /*
  UINT GetTempFileNameA(
    [in]  LPCSTR lpPathName,
    [in]  LPCSTR lpPrefixString,
    [in]  UINT   uUnique,
    [out] LPSTR  lpTempFileName
  );
  */

  GetTmpFileName( 'c:\fwh\samples', "ABC", 0, @cFileName )

  MsgInfo( cFileName )

return nil  

DLL32 Function GetTmpFileName( lpszPath AS LPSTR, lpPrefixString AS LPSTR, nUnique AS LONG, @lpTempFileName AS LPSTR ) AS LONG PASCAL ;
   FROM "GetTempFileNameA" LIB "kernel32.dll"


Using static linking: (in this case we directly return the created name)
Code (fw): Select all Collapse
#include "FiveWin.ch"

function Main()

  local cFileName := Space( 50 )

  /*
  UINT GetTempFileNameA(
    [in]  LPCSTR lpPathName,
    [in]  LPCSTR lpPrefixString,
    [in]  UINT   uUnique,
    [out] LPSTR  lpTempFileName
  );
  */

  ? GetTmpFileName( 'c:\fwh\samples', "ABC", 0, @cFileName )

return nil  

// DLL32 Function GetTmpFileName( lpszPath AS LPSTR, lpPrefixString AS LPSTR, nUnique AS LONG, @lpTempFileName AS LPSTR ) AS LONG PASCAL ;
//    FROM "GetTempFileNameA" LIB "kernel32.dll"

#pragma BEGINDUMP

#include <hbapi.h>
#include <windows.h>

HB_FUNC( GETTMPFILENAME )
{
   char * cFileName = ( char * ) hb_xgrab( 50 );  
  
   GetTempFileNameA( hb_parc( 1 ), hb_parc( 2 ), hb_parnl( 3 ), cFileName );
   hb_retc( cFileName );
   hb_xfree( cFileName );
}

#pragma ENDDUMP
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 6983
Joined: Fri Oct 07, 2005 07:07 PM
Re: RANDOM FileName ?
Posted: Tue Aug 16, 2022 07:44 AM

Dear Antonio,
thank you so much.

Best regards,
Otto
PS; Have you seen the email from Renate?

Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: RANDOM FileName ?
Posted: Tue Aug 16, 2022 07:03 PM

Dear Otto,

I haven't got any email from her

Please send your family my best regards

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: RANDOM FileName ?
Posted: Tue Aug 16, 2022 08:17 PM

Windows API function GetTempFileName() always uses ".TMP" extension.
FWH functions are better.

Regards



G. N. Rao.

Hyderabad, India

Continue the discussion