Ask a Question

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

Azeddin_Margani
Contributor

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

 

A.M.
3 REPLIES 3
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();
}

Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
Azeddin_Margani
Contributor

Hi, 

 

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

A.M.

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.


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
cancel
Showing results for 
Search instead for 
Did you mean: