Getting File Size

Applies to TestComplete 14.0, last modified on January 23, 2019

This topic shows how to read the size of a file from its directory. Related file operations are explained in Working With Files From Scripts, Processing Files in a Folder and Reading Text Files.

Two different types of services can be used. The first type is found in the aqFileSystem object. The second type is supplied by the FileSystemObject OLE object (for more information on it, see the FileSystemObject Object article in the MSDN library):

  • Getting file size by using the aqFileSystem object

    JavaScript

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

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

    JScript

    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 GetFileSize(Path, SearchPattern):
    
      foundFiles = aqFileSystem.FindFiles(Path, SearchPattern)
      Size = -1
      if foundFiles != None:
        Size = foundFiles.Item[0].Size
      return Size

    VBScript

    Sub 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 Sub

    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;

    C++Script, C#Script

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

  • Getting file size by using FileSystemObject

    JavaScript

    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 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 GetFileSize(FileName):
      fs = Sys.OleObject["Scripting.FileSystemObject"]
    
      if fs.FileExists(FileName):
        file = fs.GetFile(FileName)
        return file.Size
      else:
        return -1

    VBScript

    Sub 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 Sub

    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;

    C++Script, C#Script

    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