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

     

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

     

    • Luukdb's avatar
      Luukdb
      Contributor

      Thank you for the information and the code!

      After changing and running the code, it seems TestComplete cannot find the document. It looks like the path isn't a path anymore and just one word. Is there any reason this is the case?

       

  • Hi Luukdb!

     

    Interesting, probably something simple like a missing '\'. Would you mind sharing your new code that presents this error?

    • Luukdb's avatar
      Luukdb
      Contributor

      In the screenshot I only had one \ and tried \\ in my current code but that didn't do anything.

       

      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 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 = "\\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- "+ timeNowFormatted + ".xml";
      
       
       // Renames the file
       aqFileSystem.RenameFile(OldPath, NewPath);
      }

       

  • Hi Luukdb!

     

    After analyzing these functions a little more, I think I see what might be the issue. 

     

    The 'aqFileSystem.RenameFile' method is actually moving your files instead of copying them. So I suspect the error you are receiving is from the original file missing from the copy-from directory. 

     

    We can see the operation here in the description for this method = "If the NewPath parameter specifies another name of the folder, then this method moves and renames the file."

     

    To correct this, I just altered your 'OldPath' variable to point to the copy-to directory instead of the copy-from.

     

     

    var OldPath = "c:\\copyTo\\file1.txt";
    var NewPath = "c:\\copyTo\\file1copy"+ timeNowFormatted + ".xml";
    
     // Renames the file
     aqFileSystem.RenameFile(OldPath, NewPath);
    }

     

     

    Here is the doc that covers the rename method so that you can see how it's working under the hood;

    https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqfilesystem/renamefile.html

     

    I hope this helps!

     

    • Luukdb's avatar
      Luukdb
      Contributor

      Hi Nick!

       

      Thank you for you answer. For my understanding, do I need to create a folder copyTo?

      I also noticed a problem with the aqfile.Copy method. For some reason he cannot find the path to copy the existing file. The location we use is a network harddisk and our Virtual Machine has access to this drive. And to my understanding it shouldn't be an issue but it keeps appearing that he can't find the files.

      EDIT: I just found out that he can open the folder but when I add the document name to the path it's not possible for TestComplete and Windows to find the path.

       

      EDIT 2: I might found the problem why TestComplete can't find the file to copy and rename.

       

      The location where the file is stored is: \\bs-international.nl\group\ICT\Applicatiebeheer\TestComplete\NewPackingTable xml files

       

      Because of the Javascript we need to use double \ for a folder to open. I added an extra \ in the code so it looks like this: \\bs-international.nl\\group\\ICT\\Applicatiebeheer\\TestComplete\\NewPackingTable xml files\\test.xml

       

      But what I noticed is that the output TestComplete gives is this: 

      \bs-international.nl\group\ICT\Applicatiebeheer\TestComplete\NewPackingTable xml files\test.xml
       
      As you can see, the first \ is only one, and I need two. So I tried to add an extra \ but now the output of TestComplete is: 
      \s-international.nl\group\ICT\Applicatiebeheer\TestComplete\NewPackingTable xml files\test.xml
       
      So here I am missing the b.. Do you know anything about getting the double \ to work?
      • npaisley's avatar
        npaisley
        Staff

        Hi Luukdb!

         

        As for the 'copyTo' directory, that was just my example. You don't need to create a new directory, just rename the file with the 'renameFile' method from the same directory you are copying to, not from the original copy-from directory. 

         

        Full transparency I am not the most knowledgeable with JS, but a little googling on SO it looks like you can use 4 slashes for network drives, i.e. = \\\\bs-international.nl\group\ICT\Applicatiebeheer\TestComplete\NewPackingTable xml files\test.xml

         

        Here is the SO I referenced = https://stackoverflow.com/questions/34837954/use-node-js-to-access-a-local-network-drive

         

        And while this may not be an issue now, I notice there are spaces in the folder names, this can be a dangerous practice as handling spaces can be very difficult when writing code. I would highly recommend to replace any spaces in your folders/file names with an underscore (_). This will really help to avoid any issues down the road. 

         

        I hope all of this helps!