Forum Discussion

Azeddin_Margani's avatar
Azeddin_Margani
Contributor
7 years ago

Unable to open the file error when trying to open .txt file

I'm getting "Unable to open the file" error when trying to open .txt file. I'm using the following code: 

 

function RetrieveExistingPaitent()
  {
  var sPath = "C:\Files\MyFile.txt";

  // Opens the specified file for reading
  var myFile = aqFile.OpenTextFile(sPath, aqFile.faRead, aqFile.ctUnicode);
  Log.Message("File by lines:");

  // Reads text lines from the file and posts them to the test log
  while(! myFile.IsEndOfFile())
  {
    s = myFile.ReadLine();
    Log.Message(s);
  }

  // Closes the file
  myFile.Close();
}

 

3 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    The problem is in your declaration of sPath.  Because you're using JavaScript, the backslash (\) character is a control character.  In order to have that character in a string, you need to double it.  Change your code as noted below paying special attention to the bolded line.

    function RetrieveExistingPaitent()
      {
      var sPath = "C:\\Files\\MyFile.txt";
    
      // Opens the specified file for reading
      var myFile = aqFile.OpenTextFile(sPath, aqFile.faRead, aqFile.ctUnicode);
      Log.Message("File by lines:");
    
      // Reads text lines from the file and posts them to the test log
      while(! myFile.IsEndOfFile())
      {
        s = myFile.ReadLine();
        Log.Message(s);
      }
    
      // Closes the file
      myFile.Close();
    }
  • Hi, 

     

    Changed the code to:  var sPath = "C:\\Files\\MyFile.txt"; and still getting the same error.

    • tristaanogre's avatar
      tristaanogre
      Esteemed Contributor

      I set up the same file path and file and ran that code without error.  So, the problem is not in the code.

       

      Check your permissions on the "Files" folder.  What it seems is that the file is currently not available to be opened, possibly because it is permission controlled by something.  Also, if the file is open in some other process, the automation might not be able to open it.