Iterating Through Files in a Folder. Example

Applies to TestComplete 15.73, last modified on March 04, 2025

The sample script routine below searches for .txt files in a folder and changes the date and time attributes of the files which date is earlier than 01/01/2004. The sample code uses functions provided by the aqFileSystem, aqFile, aqDateTime object.

JavaScript

function FileFinder()
{
  var Path, foundFiles, aFile, Path, attr, i, e, dyear;
  var DateVal, Handle, FileDate, ReadOnlyFile;
 
  Path = "C:\\"; //Folder where we will search for files
  foundFiles=aqFileSystem.FindFiles(Path, "*.txt");
  
  if (!strictEqual(foundFiles, null))
   while (foundFiles.HasNext())
      {
       aFile=foundFiles.Next();
       Log.Message("Processing the file " + Path + aFile.Name);
       
       // Checks whether the file date is earlier than 01/01/2004
       FileDate = aqFile.GetLastWriteTime(Path + aFile.Name); // Obtains the file date
       dyear = aqDateTime.GetYear(FileDate); // Gets the year
       if (dyear < 2004)
       {
        // Changes the file date
        DateVal = aqConvert.StrToDateTime("1/1/2004 00:01:00"); // New date & time

        // Checks whether the file is read-only
        ReadOnlyFile = (aFile.Attributes & 1) != 0;
        // If the file is read-only, we should make it writable.
        // Else, the file date cannot be changed.
        if (ReadOnlyFile)
          {
          // Removes the ReadOnly attribute
          attr = aFile.Attributes & (~1);
          aqFile.SetFileAttributes(Path + aFile.Name, attr);
          };
      
        // Changes the file date
        e = aqFile.SetLastWriteTime(Path + aFile.Name, DateVal);
        // Reports whether the change was performed successfully
        if (e)
          Log.Message ("File date was changed successfully!");
        else 
          Log.Warning ("File date was not changed. Error: " + SysErrorMessage(e));
         
        // Restores the ReadOnly attribute, if necessary
        if (ReadOnlyFile)
          aqFile.SetFileAttributes(Path + aFile.Name, 1);
      };
      }
   else Log.Message("No files were found.");
}
 

JScript

function FileFinder()
{
  var Path, foundFiles, aFile, Path, attr, i, e, dyear;
  var DateVal, Handle, FileDate, ReadOnlyFile;
 
  Path = "C:\\"; //Folder where we will search for files
  foundFiles=aqFileSystem.FindFiles(Path, "*.txt");
  
  if (foundFiles!=null)
   while (foundFiles.HasNext())
      {
       aFile=foundFiles.Next();
       Log.Message("Processing the file " + Path + aFile.Name);
       
       // Checks whether the file date is earlier than 01/01/2004
       FileDate = aqFile.GetLastWriteTime(Path + aFile.Name); // Obtains the file date
       dyear = aqDateTime.GetYear(FileDate); // Gets the year
       if (dyear < 2004)
       {
        // Changes the file date
        DateVal = aqConvert.StrToDateTime("1/1/2004 00:01:00"); // New date & time

        // Checks whether the file is read-only
        ReadOnlyFile = (aFile.Attributes & 1) != 0;
        // If the file is read-only, we should make it writable.
        // Else, the file date cannot be changed.
        if (ReadOnlyFile)
          {
          // Removes the ReadOnly attribute
          attr = aFile.Attributes & (~1);
          aqFile.SetFileAttributes(Path + aFile.Name, attr);
          };
      
        // Changes the file date
        e = aqFile.SetLastWriteTime(Path + aFile.Name, DateVal);
        // Reports whether the change was performed successfully
        if (e)
          Log.Message ("File date was changed successfully!");
        else 
          Log.Warning ("File date was not changed. Error: " + SysErrorMessage(e));
         
        // Restores the ReadOnly attribute, if necessary
        if (ReadOnlyFile)
          aqFile.SetFileAttributes(Path + aFile.Name, 1);
      };
      }
   else Log.Message("No files were found.");
}
 

Python

def FileFinder():
  Path = "C:\\Temp\\" # Folder where we will search for files
  foundFiles = aqFileSystem.FindFiles(Path, "*.txt")
  
  if (foundFiles != None):
    while (foundFiles.HasNext()):
      aFile=foundFiles.Next()
      Log.Message("Processing the file " + Path + aFile.Name)
       
      # Checks whether the file date is earlier than 01/01/2004
      FileDate = aqFile.GetLastWriteTime(Path + aFile.Name) # Obtains the file date 
      dyear = aqDateTime.GetYear(FileDate) # Gets the year 
      if (dyear < 2016): 
        # Changes the file date 
        DateVal = aqConvert.StrToDateTime("3/3/2016 00:01:00") # New date & time 

        # Checks whether the file is read-only 
        ReadOnlyFile = (aFile.Attributes and 1) != 0
        # If the file is read-only, we should make it writable.
        # Else, the file date cannot be changed.
        if (ReadOnlyFile): 
          # Removes the ReadOnly attribute 
          attr = aFile.Attributes & (~1)
          aqFile.SetFileAttributes(Path + aFile.Name, attr)
      
        # Changes the file date 
        e = aqFile.SetLastWriteTime(Path + aFile.Name, DateVal)
        # Reports whether the change was performed successfully 
        if (e): 
          Log.Message ("File date was changed successfully!")
        else: 
          Log.Warning ("File date was not changed. Error: " + SysErrorMessage(e))
        
        # Restores the ReadOnly attribute, if necessary 
        if (ReadOnlyFile): 
          aqFile.SetFileAttributes(Path + aFile.Name, 1) 
  else: Log.Message("No files were found.")

VBScript

Sub Test
  Path = "C:\" ' Folder where we will search for files
  Set foundFiles = aqFileSystem.FindFiles(Path, "*.txt")
  
  If Not foundFiles is Nothing Then
    While foundFiles.HasNext
      Set aFile=foundFiles.Next
      Log.Message "Processing the file " + Path + aFile.Name
                
      ' Checks whether the file date is earlier than 01/01/2004
      FileDate = aqFile.GetLastWriteTime(Path + aFile.Name) ' Obtains the file date
      dyear = aqDateTime.GetYear(FileDate) ' Gets the year
      If (dyear < 2004) Then 
        ' Changes the file date
        DateVal = aqConvert.StrToDateTime("1/1/2004 00:01:00") ' New date & time

        ' Checks whether the file is read-only
        ReadOnlyFile = (aFile.Attributes And 1)<>0
        ' If the file is read-only, we should make it writable.
        ' Else, the file date cannot be changed.
        If ReadOnlyFile Then
          ' Removes the ReadOnly attribute
          attr = aFile.Attributes And Not 1
          Call aqFile.SetFileAttributes(Path + aFile.Name, attr)
        End If
      
        ' Changes the file date
        e = aqFile.SetLastWriteTime(Path + aFile.Name, DateVal)
        ' Reports whether the change was performed successfully
        If e Then
          Log.Message "File date was changed successfully!"
        Else 
          Log.Warning "File date was not changed. Error: " + SysErrorMessage(e)
        End If 
      
        ' Restores the ReadOnly attribute, if necessary
        If ReadOnlyFile Then 
          Call aqFile.SetFileAttributes(Path + aFile.Name, 1)
        End If
      End If
    Wend
  Else
    Log.Message("No files were found.")
  End If
End Sub

DelphiScript

procedure Test;
var
  foundFiles, aFile, Path, attr, i, e, dyear, Handle, FileDate, ReadOnlyFile, DateVal : OleVariant;
begin 
  Path := 'C:\'; // Folder where we will search for files
  foundFiles := aqFileSystem.FindFiles(Path, '*.txt');
  
  if foundFiles<>nil then
    while (foundFiles.HasNext()) do
        begin
         aFile := foundFiles.Next();
         Log.Message('Processing the file ' + Path + aFile.Name);
       
         // Checks whether the file date is earlier than 01/01/2004
         FileDate := aqFile.GetLastWriteTime(Path + aFile.Name); // Obtains the file date
         dyear := aqDateTime.GetYear(FileDate); // Gets the year
         if (dyear < 2004) then 
         begin 
          // Changes the file date
          DateVal := aqConvert.StrToDateTime('1/1/2004 00:01:00'); // New date & time

          // Checks whether the file is read-only
          ReadOnlyFile := (aFile.Attributes and 1) <> 0;
          // If the file is read-only, we should make it writable.
          // Else, the file date cannot be changed.
          if ReadOnlyFile then 
            begin
            // Removes the ReadOnly attribute
            attr := aFile.Attributes and not 1;
            aqFile.SetFileAttributes(Path + aFile.Name, attr);
            end;
      
          // Changes the file date
          e := aqFile.SetLastWriteTime(Path + aFile.Name, DateVal);
          // Reports whether the change was performed successfully
          if e then 
            Log.Message ('File date was changed successfully!')
          else 
            Log.Warning ('File date was not changed. Error: ' + SysErrorMessage(e));
         
          // Restores the ReadOnly attribute, if necessary
          if ReadOnlyFile then
            aqFile.SetFileAttributes(Path + aFile.Name, 1);
        end 
        end
      else Log.Message('No files were found.');
end;

C++Script, C#Script

function Test()
{
  var Path, foundFiles, aFile, Path, attr, i, e, dyear;
  var DateVal, Handle, FileDate, ReadOnlyFile;

  Path = "C:\\"; // Folder where we will search for files
  foundFiles = aqFileSystem["FindFiles"](Path, "*.txt");
  
  if (foundFiles!=null)
   while (foundFiles["HasNext"]())
      {
       aFile=foundFiles["Next"]();
       Log["Message"]("Processing the file " + Path + aFile["Name"]);
       
       // Checks whether the file date is earlier than 01/01/2004
       FileDate = aqFile["GetLastWriteTime"](Path + aFile["Name"]); // Obtains the file date
       dyear = aqDateTime["GetYear"](FileDate); // Gets the year
       if (dyear < 2004)
       {
        // Changes the file date
        DateVal = aqConvert["StrToDateTime"]("1/1/2004 00:01:00"); // New date & time

        // Checks whether the file is read-only
        ReadOnlyFile = (aFile["Attributes"] & 1) != 0;
        // If the file is read-only, we should make it writable.
        // Else, the file date cannot be changed.
        if (ReadOnlyFile)
          {
          // Removes the ReadOnly attribute
          attr = aFile["Attributes"] & (~1);
          aqFile["SetFileAttributes"](Path + aFile["Name"], attr);
          };
      
        // Changes the file date
        e = aqFile["SetLastWriteTime"](Path + aFile["Name"], DateVal);
        // Reports whether the change was performed successfully
        if (e)
          Log["Message"]("File date was changed successfully!");
        else 
          Log["Warning"]("File date was not changed. Error: " + SysErrorMessage(e));
         
        // Restores the ReadOnly attribute, if necessary
        if (ReadOnlyFile)
          aqFile["SetFileAttributes"](Path + aFile["Name"], 1);
      };
      }
   else Log["Message"]("No files were found.");

}

Highlight search results