Rename Method

Applies to TestComplete 15.44, last modified on November 10, 2022

Description

Use the Rename method to change the name of the file to which the aqFileInfoObj object relates.

Declaration

aqFileInfoObj.Rename(NewPath, RenameOnCollision)

aqFileInfoObj An expression, variable or parameter that specifies a reference to an aqFileInfo object
NewPath [in]    Required    String    
RenameOnCollision [in]    Optional    Boolean Default value: True   
Result Boolean

Applies To

The method is applied to the following object:

Parameters

The method has the following parameters:

NewPath

Specifies a new path to the file. Both fully-qualified and relative paths are acceptable. If the parameter specifies another folder name, the file is first moved to the new location and then renamed.

If NewPath specifies a folder path, it may or may not include a trailing backslash (\). You can add a trailing backslash to or remove it from the path by using the IncludeTrailingBackSlash or ExcludeTrailingBackSlash method. At that, the destination folder must exist.

RenameOnCollision

Specifies the method behavior in case the destination file already exists. If RenameOnCollision is True, which is the default value, the method copies the source file to the Copy of <FileName> file in the destination folder. Otherwise, the method overwrites the destination file.

Result Value

True if the file was renamed successfully, and False otherwise.

Remarks

If the method fails to rename the file, TestComplete will post an information message to the test log explaining the cause of the failure.

Example

The examples below demonstrate how you can use the Rename method to rename and move the desired files.

The code below shows how you can use the method to rename a file.

JavaScript, JScript

function RenameFileExample()
{
 var OldPath = "C:\\MyFiles\\FileName_old.txt";
 var NewPath = "C:\\MyFiles\\FileName.txt";

 // Gets information about the file
 var FileInfo = aqFileSystem.GetFileInfo(OldPath);
 
 // Renames the file
 FileInfo.Rename(NewPath);
}

Python

def RenameFileExample():
  OldPath = "C:\\MyFiles\\FileName_old.txt"
  NewPath = "C:\\MyFiles\\FileName.txt"
  # Gets information about the file
  FileInfo = aqFileSystem.GetFileInfo(OldPath)
  # Renames the file
  FileInfo.Rename(NewPath)

VBScript

Sub RenameFileExample

 OldPath = "C:\MyFiles\FileName_old.txt"
 NewPath = "C:\MyFiles\FileName.txt"

 ' Gets information about the file
 Set FileInfo = aqFileSystem.GetFileInfo(OldPath)
 
 ' Renames the file
 Call FileInfo.Rename(NewPath)

End Sub

DelphiScript

function RenameFileExample;
var OldPath, NewPath, FileInfo;
begin

 OldPath := 'C:\MyFiles\FileName_old.txt';
 NewPath := 'C:\MyFiles\FileName.txt';

 // Gets information about the file
 FileInfo := aqFileSystem.GetFileInfo(OldPath);
 
 // Renames the file
 FileInfo.Rename(NewPath);

end;

C++Script, C#Script

function RenameFileExample()
{
 var OldPath = "C:\\MyFiles\\FileName_old.txt";
 var NewPath = "C:\\MyFiles\\FileName.txt";

 // Gets information about the file
 FileInfo = aqFileSystem["GetFileInfo"](OldPath);
 
 // Renames the file
 FileInfo["Rename"](NewPath);
}

The code below shows how you can use the method to both rename and move a file.

JavaScript, JScript

function MoveAndRenameFileExample()
{
 var OldPath = "C:\\MyFiles\\FileName.txt";
 var NewPathMove = "C:\\NewFolder\\FileName.txt";
 var NewPathMoveAndRename = "C:\\NewFolder\\NewName.txt";

 // Gets information about the file
 var FileInfo = aqFileSystem.GetFileInfo(OldPath);
 
 // Moves the FileName.txt file from the MyFiles folder to NewFolder
 FileInfo.Rename(NewPathMove);
 
 // or
 
 // Moves and renames the FileName.txt file
 FileInfo.Rename(NewPathMoveAndRename);
}

Python

def MoveAndRenameFileExample():
  OldPath = "C:\\MyFiles\\FileName.txt"
  NewPathMove = "C:\\NewFolder\\FileName.txt"
  NewPathMoveAndRename = "C:\\NewFolder\\NewName.txt"
  # Gets information about the file
  FileInfo = aqFileSystem.GetFileInfo(OldPath)
  # Moves the FileName.txt file from the MyFiles folder to NewFolder
  FileInfo.Rename(NewPathMove)
  # or
  # Moves and renames the FileName.txt file
  FileInfo.Rename(NewPathMoveAndRename)

VBScript

Sub MoveAndRenameFileExample

 OldPath = "C:\MyFiles\FileName.txt"
 NewPathMove = "C:\NewFolder\FileName.txt"
 NewPathMoveAndRename = "C:\NewFolder\NewName.txt"

 ' Gets information about the file
 Set FileInfo = aqFileSystem.GetFileInfo(OldPath)
 
 ' Moves the FileName.txt file from the MyFiles folder to NewFolder
 Call FileInfo.Rename(NewPathMove)
 
 ' or
 
 ' Moves and renames the FileName.txt file
 Call FileInfo.Rename(NewPathMoveAndRename)
End Sub

DelphiScript

function MoveAndRenameFileExample;
var OldPath, NewPathMove, NewPathMoveAndRename, FileInfo;
begin

 OldPath := 'C:\MyFiles\FileName.txt';
 NewPathMove := 'C:\NewFolder\FileName.txt';
 NewPathMoveAndRename := 'C:\NewFolder\NewName.txt';

 // Gets information about the file
 FileInfo := aqFileSystem.GetFileInfo(OldPath);
 
 // Moves the FileName.txt file from the MyFiles folder to NewFolder
 FileInfo.Rename(NewPathMove);
 
 // or
 
 // Moves and renames the FileName.txt file
 FileInfo.Rename(NewPathMoveAndRename);

end;

C++Script, C#Script

function MoveAndRenameFileExample()
{
 var OldPath = "C:\\MyFiles\\FileName.txt";
 var NewPathMove = "C:\\NewFolder\\FileName.txt";
 var NewPathMoveAndRename = "C:\\NewFolder\\NewName.txt";

 // Gets information about the file
 FileInfo = aqFileSystem["GetFileInfo"](OldPath);
 
 // Moves the FileName.txt file from the MyFiles folder to NewFolder
 FileInfo["Rename"](NewPathMove);
 
 // or
 
 // Moves and renames the FileName.txt file
 FileInfo["Rename"](NewPathMoveAndRename);
}

See Also

Working With Files From Scripts
Copy Method
Move Method

Highlight search results