aqFileSystem.RenameFile Method

Applies to TestComplete 15.47, last modified on January 20, 2023

Description

Use the RenameFile 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

aqFileSystem.RenameFile(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. This parameter accepts both fully-qualified and relative paths. 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.

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 RenameFile 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
 aqFileSystem.RenameFile(OldPath, NewPath);
}

Python

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

VBScript

Sub RenameFileExample

 OldPath = "C:\MyFiles\FileName_old.txt"
 NewPath = "C:\MyFiles\FileName.txt"
 
 ' Renames the file
 Call aqFileSystem.RenameFile(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
 aqFileSystem.RenameFile(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
 aqFileSystem["RenameFile"](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
 aqFileSystem.RenameFile(OldPath, NewPathMove);
 
 // or
 
 // Moves and renames the FileName.txt file
 aqFileSystem.RenameFile(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
  aqFileSystem.RenameFile(OldPath, NewPathMove)
  # or 
  # Moves and renames the FileName.txt file
  aqFileSystem.RenameFile(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 aqFileSystem.RenameFile(OldPath, NewPathMove)
 
 ' or
 
 ' Moves and renames the FileName.txt file
 Call aqFileSystem.RenameFile(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
 aqFileSystem.RenameFile(OldPath, NewPathMove);
 
 // or
 
 // Moves and renames the FileName.txt file
 aqFileSystem.RenameFile(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
 aqFileSystem["RenameFile"](OldPath, NewPathMove);
 
 // or
 
 // Moves and renames the FileName.txt file
 aqFileSystem["RenameFile"](OldPath, NewPathMoveAndRename);
}

See Also

Working With Files From Scripts
Exists Method
MoveFile Method
CopyFile Method
DeleteFile Method

Highlight search results