Write Method

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

Description

Use the Write method to write the specified value to a text file. The value is written starting from the current position. If the position is not the end of the file, the specified value overwrites the previous data.

Declaration

aqTextFileObj.Write(Value)

aqTextFileObj An expression, variable or parameter that specifies a reference to an aqTextFile object
Value [in]    Required    Variant    
Result Boolean

Applies To

The method is applied to the following object:

Parameters

The method has the following parameter:

Value

Defines the variant-compatible value to be written to a file.

Result Value

True if the value was written successfully, and False otherwise.

Remarks

On success, the cursor position is shifted by the length of the written data.

To use this method, you should open the needed file with the faWrite or faReadWrite access permission. See the description of the OpenTextFile method.

Example

The code below opens the specified file for writing, adds some text to it and then closes the file saving the changes.

JavaScript, JScript

function WriteTextToFile()
{
  var sPath = "D:\\Files\\MyFile.txt";
  
  // Opens the specified file for writing
  var myFile = aqFile.OpenTextFile(sPath, aqFile.faWrite, aqFile.ctANSI);
  
  // Writes data to the file
  myFile.Write("Hello, world.");
    
  // Closes the file and saves the changes
  myFile.Close();
}

Python

def WriteTextToFile():
  sPath = "D:\\Files\\MyFile.txt"
  # Opens the specified file for writing
  myFile = aqFile.OpenTextFile(sPath, aqFile.faWrite, aqFile.ctANSI)
  # Writes data to the file
  myFile.Write("Hello, world.")
  # Closes the file and saves the changes
  myFile.Close()

VBScript

Sub WriteTextToFile

  sPath = "D:\Files\MyFile.txt"
  
  ' Opens the specified file for writing
  Set myFile = aqFile.OpenTextFile(sPath, aqFile.faWrite, aqFile.ctANSI)
  
  ' Writes data to the file
  myFile.Write("Hello, world.")
    
  ' Closes the file and saves the changes
  myFile.Close
  
End Sub

DelphiScript

function WriteTextToFile;
var sPath, myFile;
begin
  sPath := 'D:\Files\MyFile.txt';
  
  // Opens the specified file for writing
  myFile := aqFile.OpenTextFile(sPath, aqFile.faWrite, aqFile.ctANSI);
  
  // Writes data to the file
  myFile.Write('Hello, world.');
    
  // Closes the file and saves the changes
  myFile.Close();
end;

C++Script, C#Script

function WriteTextToFile()
{
  var sPath = "D:\\Files\\MyFile.txt";
  
  // Opens the specified file for writing
  var myFile = aqFile["OpenTextFile"]( sPath, aqFile["faWrite"], aqFile["ctANSI"] );
  
  // Writes data to the file
  myFile["Write"]("Hello, world.");
    
  // Closes the file and saves the changes
  myFile["Close"]();
}

See Also

Working With Files From Scripts
WriteLine Method
ReadLine Method
ReadString Method
ReadToSymbol Method
ReadAll Method

Highlight search results