Copy and rename a file using JavaScript
Hello,
I am trying to copy an xml file, and rename it so I can use it in my tests. Only I can't seem to figure out what I am doing wrong. I used the TestComplete documentation, as I got the code from there. I tried to modify it in different ways, but nu success.
This is the code I currently have:
aqFile.Copy("\\bs-international.nl\group\ICT\Applicatiebeheer\TestComplete\NewPackingTable xml files\NewPackingTable_BIT-ERP_Order.xml", "\\bs-international.nl\group\ICT\Applicatiebeheer\TestComplete\NewPackingTable xml files\NewPackingTable_BIT-ERP_Order-copy.xml");
function RenameXMLFile()
{
var OldPath = "\\bs-international.nl\group\ICT\Applicatiebeheer\TestComplete\NewPackingTable xml files\NewPackingTable_BIT-ERP_Order-copy.xml";
var NewPath = "\\bs-international.nl\group\ICT\Applicatiebeheer\TestComplete\NewPackingTable xml files\NewPackingTable_BIT-ERP_Order- "+ VarToStr(aqDateTime.Now())+ ".xml";
// Renames the file
aqFileSystem.RenameFile(OldPath, NewPath);
}
I hope someone can help me get back on track.
Hi Luukdb!
Your issue here lies in the AqDateTime.Now function. This will return the current date/time in a format that is not compatible with Windows file system naming conventions.
We have a format function you can use to retrieve the date/time and format it in various ways to match the date/time required and for Windows file name conventions. This is the 'aqConvert.DateTimeToFormatStr' method.
You can find information for this method here = https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqconvert/datetimetoformatstr.html
An example of how to integrate this into your code is below. Note I have a 'log.Message' operation so that we can see what the raw and formatted dates look like.
aqFile.Copy("c:\\file1.txt", "c:\\copy\\file1.txt"); function RenameXMLFile() { var timeNowRaw = aqDateTime.Now() var timeNowFormatted = aqConvert.DateTimeToFormatStr(timeNowRaw,"%m%d%Y_%H%M") Log.Message("Unformatted raw date from aqDateTime.Now = " + aqDateTime.Now()) Log.Message("Formatted Date = " + aqConvert.DateTimeToFormatStr(timeNowRaw, "%m%d%Y_%H%M")) var OldPath = "c:\\file1.txt"; var NewPath = "c:\\copy\\file1copy"+ timeNowFormatted + ".xml"; // Renames the file aqFileSystem.RenameFile(OldPath, NewPath); }