aqFile.Rename Method

Applies to TestComplete 15.46, last modified on January 09, 2023

Description

Use the aqFile.Rename method to change the name of a file. If the NewPath parameter specifies another name of the folder, then this method moves and renames the file.

Declaration

aqFile.Rename(OldPath, NewPath, RenameOnCollision)

OldPath [in]    Required    String    
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:

OldPath

Specifies the path to the file to be renamed. Both fully-qualified and relative paths are acceptable.

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. Both absolute and relative paths are acceptable.

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.

If the destination folder does not exist, it will be created if the NewPath parameter includes the file name or ends with a backslash, for instance, “C:\DestFolder\File.txt” or “C:\DestFolder\”.

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 specified file was not found, the method raises an exception. To verify whether the file exists, use the Exists method.

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";
 
 // Renames the file
 aqFile.Rename(OldPath, NewPath);
}

Python

def RenameFileExample():
  OldPath = "C:\\MyFiles\\FileName_old.txt"
  NewPath = "C:\\MyFiles\\FileName.txt"
  # Renames the file
  aqFile.Rename(OldPath, NewPath)

VBScript

Sub RenameFileExample

 OldPath = "C:\MyFiles\FileName_old.txt"
 NewPath = "C:\MyFiles\FileName.txt"
 
 ' Renames the file
 Call aqFile.Rename(OldPath, NewPath)

End Sub

DelphiScript

function RenameFileExample;
var OldPath, NewPath;
begin

 OldPath := 'C:\MyFiles\FileName_old.txt';
 NewPath := 'C:\MyFiles\FileName.txt';
 
 // Renames the file
 aqFile.Rename(OldPath, NewPath);

end;

C++Script, C#Script

function RenameFileExample()
{
 var OldPath = "C:\\MyFiles\\FileName_old.txt";
 var NewPath = "C:\\MyFiles\\FileName.txt";
 
 // Renames the file
 aqFile["Rename"](OldPath, 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";
 
 // Moves the FileName.txt file from the MyFiles folder to NewFolder
 aqFile.Rename(OldPath, NewPathMove);
 
 // or
 
 // Moves and renames the FileName.txt file
 aqFile.Rename(OldPath, NewPathMoveAndRename);
}

Python

def MoveAndRenameFileExample():
  OldPath = "C:\\MyFiles\\FileName.txt"
  NewPathMove = "C:\\NewFolder\\FileName.txt"
  NewPathMoveAndRename = "C:\\NewFolder\\NewName.txt"
  # Moves the FileName.txt file from the MyFiles folder to NewFolder
  aqFile.Rename(OldPath, NewPathMove)
  # or 
  # Moves and renames the FileName.txt file
  aqFile.Rename(OldPath, NewPathMoveAndRename)

VBScript

Sub MoveAndRenameFileExample

 OldPath = "C:\MyFiles\FileName.txt"
 NewPathMove = "C:\NewFolder\FileName.txt"
 NewPathMoveAndRename = "C:\NewFolder\NewName.txt"
 
 ' Moves the FileName.txt file from the MyFiles folder to NewFolder
 Call aqFile.Rename(OldPath, NewPathMove)
 
 ' or
 
 ' Moves and renames the FileName.txt file
 Call aqFile.Rename(OldPath, NewPathMoveAndRename)
End Sub

DelphiScript

function MoveAndRenameFileExample;
var OldPath, NewPathMove, NewPathMoveAndRename;
begin

 OldPath := 'C:\MyFiles\FileName.txt';
 NewPathMove := 'C:\NewFolder\FileName.txt';
 NewPathMoveAndRename := 'C:\NewFolder\NewName.txt';
 
 // Moves the FileName.txt file from the MyFiles folder to NewFolder
 aqFile.Rename(OldPath, NewPathMove);
 
 // or
 
 // Moves and renames the FileName.txt file
 aqFile.Rename(OldPath, 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";
 
 // Moves the FileName.txt file from the MyFiles folder to NewFolder
 aqFile["Rename"](OldPath, NewPathMove);
 
 // or
 
 // Moves and renames the FileName.txt file
 aqFile["Rename"](OldPath, NewPathMoveAndRename);
}

See Also

Working With Files From Scripts
Exists Method
Move Method
Copy Method
Delete Method
Create Method

Highlight search results