Getting File Size

Applies to TestComplete 15.48, last modified on March 01, 2023

The examples below demonstrate how to get the size of a file. They use two approaches. The first one uses the aqFileSystem object. The second one uses the FileSystemObject OLE object (for more information on it, see the FileSystemObject Object article in the MSDN library).

Related file operations are explained in Working With Files From Scripts, Processing Files in a Folder and Reading Text Files.

Get the file size by using the aqFileSystem object

In the Main routine below, specify the path to the directory where the needed file is located and a search pattern to find the needed file. Run the Main routine. It will call the GetFileSize routine that will return the size of the first file it finds (if any) using the provided location and search pattern.

JavaScript

function Main()
{
  // Specify the file location and search pattern
  let path = "C:\\Users\\Public\\Documents";
  let searchPattern = "*.txt";

  let size = GetFileSize(path, searchPattern);
  Log.Message(aqString.Format("File size: %d", size));
}

function GetFileSize(Path, SearchPattern)
{
  let foundFiles, Size;

  foundFiles = aqFileSystem.FindFiles(Path, SearchPattern);
  Size = -1;
  if (!strictEqual(foundFiles, null))
    Size = foundFiles.Item(0).Size;
  return Size;
}

JScript

function Main()
{
  // Specify the file location and search pattern
  var path = "C:\\Users\\Public\\Documents";
  var searchPattern = "*.txt";

  var size = GetFileSize(path, searchPattern);
  Log.Message(aqString.Format("File size: %d", size));
}

function GetFileSize(Path, SearchPattern)
{
  var foundFiles, Size;

  foundFiles = aqFileSystem.FindFiles(Path, SearchPattern);
  Size = -1;
  if (foundFiles!=null)
    Size = foundFiles.Item(0).Size;
  return Size;
}

Python

def Main():
  # Specify the file location and search pattern
  path = "C:\\Users\\Public\\Documents"
  searchPattern = "*.txt"
  
  size = GetFileSize(path, searchPattern)
  Log.Message(aqString.Format("File size: %d", size))

def GetFileSize(Path, SearchPattern):

  foundFiles = aqFileSystem.FindFiles(Path, SearchPattern)
  Size = -1
  if foundFiles != None:
    Size = foundFiles.Item(0).Size
  return Size

VBScript

Sub Main
  ' Specify the file location and search pattern
  path = "C:\Users\Public\Documents"
  searchPattern = "*.txt"

  size = GetFileSize(path, searchPattern)
  Log.Message(aqString.Format("File size: %d", size))

End Sub

Function GetFileSize(Path, SearchPattern)
  Dim foundFiles, Size
  
  Set foundFiles = aqFileSystem.FindFiles(Path, SearchPattern)
  Size = -1
  If Not foundFiles Is Nothing Then
    Size = foundFiles.Item(0).Size
  End If
  GetFileSize = Size
End Function

DelphiScript

function GetFileSize(Path, SearchPattern);
var 
  foundFiles, Size;
begin 
  foundFiles := aqFileSystem.FindFiles(Path, SearchPattern);
  Size := -1;
  if foundFiles<>nil then
    Size := foundFiles.Item(0).Size;
  Result := Size;
end;

procedure Main();
var path, searchPattern, size;
begin
  // Specify the file location and search pattern
  path := 'C:\Users\Public\Documents';
  searchPattern := '*.txt';

  size := GetFileSize(path, searchPattern);
  Log.Message(aqString.Format('File size: %d', size));
end;

C++Script, C#Script

function Main()
{
  // Specify the file location and search pattern
  var path = "C:\\Users\\Public\\Documents";
  var searchPattern = "*.txt";

  var size = GetFileSize(path, searchPattern);
  Log["Message"](aqString["Format"]("File size: %d", size));
}

function GetFileSize(Path, SearchPattern)
{
  var foundFiles, Size;
  
  foundFiles = aqFileSystem["FindFiles"](Path, SearchPattern);
  Size = -1;
  if (foundFiles!=null)
    Size = foundFiles["Item"](0)["Size"];
  return Size;
}

Get the file size by using FileSystemObject

In the Main routine below, specify the fully-qualified path (including the file name and extension) to the file whose size you want to get. Run the Main routine. It will call the GetFileSize routine that will return the size of the specified file.

JavaScript

function Main()
{
  // Specify the fully-qualified file name
  let fileName = "C:\\Users\\Public\\Documents\\test1.txt";
  let size = GetFileSize(fileName);
  Log.Message(aqString.Format("File size: %d", size));
}

function GetFileSize(FileName)
{
  var fs, file;
  fs = getActiveXObject("Scripting.FileSystemObject");

  if (fs.FileExists(FileName))
  {
    file = fs.GetFile(FileName);
    return file.Size;
  }
  else
    return -1;
}

JScript

function Main()
{
  // Specify the fully-qualified file name
  var fileName = "C:\\Users\\Public\\Documents\\test1.txt";
  var size = GetFileSize(fileName);
  Log.Message(aqString.Format("File size: %d", size));
}

function GetFileSize(FileName)
{
  var fs, file;
  fs = Sys.OleObject("Scripting.FileSystemObject");
  // Note that you can create the FileSystemObject
  // using the following code:
  // fs = new ActiveXObject("Scripting.FileSystemObject")

  if (fs.FileExists(FileName))
  {
    file = fs.GetFile(FileName);
    return file.Size;
  }
  else
    return -1;
}

Python

def Main():
  # Specify the fully-qualified file name
  fileName = "C:\\Users\\Public\\Documents\\test1.txt"
  
  size = GetFileSize(fileName)
  Log.Message(aqString.Format("File size: %d", size))

def GetFileSize(FileName):
  fs = Sys.OleObject["Scripting.FileSystemObject"]

  if fs.FileExists(FileName):
    file = fs.GetFile(FileName)
    return file.Size
  else:
    return -1

VBScript

Sub Main
  ' Specify the fully-qualified file name
  fileName = "C:\Users\Public\Documents\test1.txt"
  size = GetFileSize(fileName)
  Log.Message(aqString.Format("File size: %d", size))
End Sub

Function GetFileSize(FileName)
  Set fs = Sys.OleObject("Scripting.FileSystemObject")
  ' Note that you can create the FileSystemObject
  ' using the following code:
  ' Set fs = CreateObject("Scripting.FileSystemObject")

  If fs.FileExists(FileName) Then
    Set file = fs.GetFile(FileName)
    GetFileSize = file.Size
  Else
    GetFileSize = -1
  End If
End Function

DelphiScript

function GetFileSize(FileName);
var
  fs, f;
begin
  fs := Sys.OleObject('Scripting.FileSystemObject');
  if fs.FileExists(FileName) then
  begin
    f := fs.GetFile(FileName);
    Result := f.Size;
  end
  else
    Result := -1;
end;

procedure Main();
var fileName, size;
begin
  // Specify the fully-qualified file name
  fileName := 'C:\Users\Public\Documents\test1.txt';
  size := GetFileSize(fileName);
  Log.Message(aqString.Format('File size: %d', size));
end;

C++Script, C#Script

function Main()
{
  // Specify the fully-qualified file name
  var fileName = "C:\\Users\\Public\\Documents\\test1.txt";
  var size = GetFileSize(fileName);
  Log["Message"](aqString["Format"]("File size: %d", size));
}

function GetFileSize(FileName)
{
  var fs, file;
  fs = Sys["OleObject"]("Scripting.FileSystemObject");

  if (fs["FileExists"](FileName))
  {
    file = fs["GetFile"](FileName);
    return file["Size"];
  }
  else
    return -1;
}

See Also

About File Checkpoints
Processing Files in a Folder
Reading Text Files
Working With Files From Scripts

Highlight search results