C++Builder How To Generate A Random File Name on Windows

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
How To Generate A Random File Name on Windows
By Yilmaz Yoru September 17, 2021

How can I generate a random file name? Can I generate a unique temporary file? Can I generate a GUID file name? What is the GetRandomFileName method? What is the GetTempFileName method? What is the GetGUIDFileName method?

C++ Builder has a lot of specific methods in its Для просмотра ссылки Войди или Зарегистрируйся library that are included in VCL and FMX libraries. Some of these are grouped as Для просмотра ссылки Войди или Зарегистрируйся that allows user to edit, extract, get and set drive name, directory name, file name and file extensions. These methods are combined in Для просмотра ссылки Войди или Зарегистрируйся, Для просмотра ссылки Войди или Зарегистрируйся, Для просмотра ссылки Войди или Зарегистрируйсяlibraries. These methods are easy to use and with them it is easy to get or set file path strings in that operating system. These methods can be used with other component properties like FileName property of OpenDialog, SaveDialog components. We can also check drives, files or directories to see if they are exist or not on a given path. We can include and exclude path delimiters on Windows.

What is the C++ GetRandomFileName method ?​

The Для просмотра ссылки Войди или Зарегистрируйся Method (System::IOUtils::TPath::GetRandomFileName) is an IOUtils Method that generates a new random file name. GetRandomFileName does not guarantee a unique file name across the drives or network. If an absolutely, definitely unique file name under all circumstances is desired, use the GetGUIDFileName method instead. To generate a real uniquely-named temporary file, use the GetTempFileName method.

What is the syntax of GetRandomFileName method ?​

Here is the GetRandomFileName Method:
C++:
UnicodeString __fastcall GetRandomFileName();

What is the C++ GetTempFileName method ?​

The Для просмотра ссылки Войди или Зарегистрируйся Method (System::IOUtils::TPath:: GetTempFileName ) is an IOUtils Method that generates a unique temporary file name. We can call GetTempFileName to generate a new uniquely-named temporary file. GetTempFileName actually creates a zero-sized file in a temporary location and returns its name. The caller must delete the file after it is not used anymore. Note: GetTempFileName raises an exception if the user has no access to the system’s temporary directory.

On Linux, it creates a new file which name is based on GUID (Globally Unique IDentifier) in the following format: ‘File_%8x%4x%4x%16x_tmp’:
  • %8x stands for GUID.D1
  • %4x stands for GUID.D2
  • %4x stands for GUID.D3
  • %16x stands for GUID.D4

What is the syntax of GetTempFileName method ?​

Here is the GetTempFileName Method:
C++:
UnicodeString __fastcall GetTempFileName();

What is the C++ GetGUIDFileName method ?​

Для просмотра ссылки Войди или Зарегистрируйся Method (System::IOUtils::TPath::GetGUIDFileName ) is a IOUtils Method that Generates a unique temporary file. We can call GetGUIDFileName to generate a new GUID (Globally Unique IDentifier) suitable as a unique name for a file or directory. GetGUIDFileName only generates a file name and does not create a real file.

The following table lists the parameters expected by this method:

NameMeaning
UseSeparatorIndicates whether the GUID separator char (the minus sign) is preserved in the generated name.

What is the syntax of GetGUIDFileName method ?​

Here is the GetGUIDFileName Method:
C++:
UnicodeString __fastcall GetGUIDFileName(const bool UseSeparator = false);

What is a simple example of using the C++ GetRandomFileName, GetTempFileName, GetGUIDFileName methods?​

GetRandomFileName, GetTempFileName, GetGuidFileName methods return UnicodeString. We can use them by including System.IOutils.hpp and TPath:: as given below,
C++:
String fname1= TPath::GetRandomFileName();
ShowMessage( fname1 ) ;
 
String fname2 = TPath::GetTempFileName();
ShowMessage( fname2 ) ;
 
String fname3 = TPath::GetGUIDFileName();
ShowMessage( fname3 ) ;
and the outputs will be as follows respectively,
Код:
c3ld53Et.Bq3
C:\Users\ata\AppData\Local\Temp\tmp22D.tmp
C56B164910334081BA8ED28320140F7A

Here is a full example of how to use the C++ GetRandomFileName, GetTempFileName, GetGUIDFileName methods​

Here is the full C++ Builder VCL example with a Memo component
C++:
#include <vcl.h>
#include <System.IOutils.hpp>
 
#pragma hdrstop
 
#include "Include_Exclude_Path_Delimiters_Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
 String path1, path2, path, fname;
 
 fname= TPath::GetRandomFileName();
 Memo1->Lines->Add( L"GetRandomFileName: " + fname ) ;
 path = TPath::GetFullPath(fname);
 Memo1->Lines->Add( L"Full Path Random File Name: " + path ) ;
 
 fname = TPath::GetTempFileName();
 Memo1->Lines->Add( L"GetTempFileName: " + fname ) ;
 
 fname = TPath::GetGUIDFileName();
 Memo1->Lines->Add( L"GetGUIDFileName: " + fname ) ;
 path = TPath::GetFullPath(fname);
 Memo1->Lines->Add( L"Full GUID File Name: " + path ) ;
}
and the output will be as follows respectively,
Код:
GetRandomFileName: G0FBymX6.cPx
Full Path Random File Name: D:\#PROJECTS\#LearnCPlusPlus.org\#Path Manipulation Routines\Include Exclude Path Delimeters\Win32\Debug\G0FBymX6.cPx
GetTempFileName: C:\Users\ata\AppData\Local\Temp\tmp8402.tmp
GetGUIDFileName: 6080275648894A59A88C7191A4E46050
Full GUID File Name: D:\#PROJECTS\#LearnCPlusPlus.org\#Path Manipulation Routines\Include Exclude Path Delimeters\Win32\Debug\6080275648894A59A88C7191A4E46050