Forum Discussion

Luukdb's avatar
Luukdb
Contributor
3 years ago

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 ...
  • npaisley's avatar
    3 years ago

    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);
    }