aqFileSystem.CreateTempFileName Method

Applies to TestComplete 15.70, last modified on December 17, 2024

Description

Generates a unique file name which you can use to create a temporary file. You can customize the folder path and file extension for the generated file name.

Declaration

aqFileSystem.CreateTempFileName(Extension, Path)

Extension [in]    Optional    String Default value: tmp   
Path [in]    Optional    String Default value: User’s temporary folder   
Result String

Applies To

The method is applied to the following object:

Parameters

The method has the following parameters:

Extension

The file extension for the generated file name. Default is tmp. Do not add a period ( . ) before the extension.

Path

An absolute or relative folder path (with or without a trailing slash) to prepend to the file name. Examples:

C:

C:\

C:\Work\Temp

Work\Temp

..\folder\subfolder

This folder does not have to exist. Relative folder paths are included as-is in the resulting file path, that is, are not resolved.

If it is omitted or an empty string is specified, the user’s temporary folder is used. This is the path returned by Sys.OSInfo.TempDirectory, typically C:\Users\<USERNAME>\AppData\Local\Temp\.

Result Value

A string that contains a unique temporary file name and path. The file name itself is generated using a combination of timestamp and UUID, for example:

C:\Path\To\2020_7_10-16_36_3_{749CE608-69AC-4F1D-AB6D-569CA4DB8F7A}.tmp

Remarks

aqFileSystem.CreateTempFileName does not actually create a file. It only generates a file name that you can use in aqFile.WriteToTextFile or similar methods to create a temporary file.

Example

aqFileSystem.CreateTempFileName()

returns C:\Users\tester\AppData\Local\Temp\2020_7_10-17_4_10_{749CE608-69AC-4F1D-AB6D-569CA4DB8F7A}.tmp.

aqFileSystem.CreateTempFileName("txt")

returns C:\Users\tester\AppData\Local\Temp\2020_7_10-17_4_20_{117FA928-DE71-4AE9-A30B-B81C06ACBE25}.txt.

Note that the file extension is txt instead of tmp.

aqFileSystem.CreateTempFileName("txt", "C:\Tests\Temp")

returns C:\Tests\Temp\2020_7_10-17_4_30_{A3C828B6-40C6-4039-863C-2A66B277EAC1}.txt

The following example creates a temporary file on the user’s desktop:

JavaScript

function TemporaryFileDemo()
{
  let fldrDesktop = WshShell.SpecialFolders.Item("Desktop");
  let fName = aqFileSystem.CreateTempFileName("txt", fldrDesktop);

  Log.Message("Creating a new file: " + fName);
  aqFile.WriteToTextFile(fName, "Hello, world!", aqFile.ctUTF8, true);
}

JScript

function TemporaryFileDemo()
{
  var fldrDesktop = WshShell.SpecialFolders.Item("Desktop");
  var fName = aqFileSystem.CreateTempFileName("txt", fldrDesktop);

  Log.Message("Creating a new file: " + fName);
  aqFile.WriteToTextFile(fName, "Hello, world!", aqFile.ctUTF8, true);
}

Python

def TemporaryFileDemo():
  fldrDesktop = WshShell.SpecialFolders.Item("Desktop")
  fName = aqFileSystem.CreateTempFileName("txt", fldrDesktop)

  Log.Message("Creating a new file: " + fName)
  aqFile.WriteToTextFile(fName, "Hello, world!", aqFile.ctUTF8, True)

VBScript

Sub TemporaryFileDemo
  Dim fldrDesktop, fName
  fldrDesktop = WshShell.SpecialFolders.Item("Desktop")
  fName = aqFileSystem.CreateTempFileName("txt", fldrDesktop)

  Call Log.Message("Creating a new file: " & fName)
  Call aqFile.WriteToTextFile(fName, "Hello, world!", aqFile.ctUTF8, True)
End Sub

DelphiScript

procedure TemporaryFileDemo;
var fldrDesktop, fName;
begin
  fldrDesktop := WshShell.SpecialFolders.Item('Desktop');
  fName := aqFileSystem.CreateTempFileName('txt', fldrDesktop);

  Log.Message('Creating a new file: ' + fName);
  aqFile.WriteToTextFile(fName, 'Hello, world!', aqFile.ctUTF8, true);
end;

C++Script, C#Script

function TemporaryFileDemo()
{
  var fldrDesktop = WshShell["SpecialFolders"]["Item"]("Desktop");
  var fName = aqFileSystem["CreateTempFileName"]("txt", fldrDesktop);

  Log["Message"]("Creating a new file: " + fName);
  aqFile["WriteToTextFile"](fName, "Hello, world!", aqFile["ctUTF8"], true);
}

See Also

Working With Files From Scripts
TempDirectory Property
WriteToTextFile Method
OpenTextFile Method
OpenBinaryFile Method

Highlight search results