Forum Discussion

mminnder's avatar
mminnder
Occasional Contributor
12 years ago

Text Streams and Troubles

So, I'm currently making a program that checks a json returned from a web service against a local json, and if the local doesn't exist, than it should create it using the return. When I do this I want it to return the info inside this newly created json to the previous function.



IE. return  text; 



Basically:

If (file doesn't exist)

{

      var createObject = new ActiveXObject("Scripting.FileSystemObject");

      var textDoc = createObject.CreateTextFile("./JSON/Returns/" + key + ".txt", 1);


      textDoc.close();


      


      var reopenedTextDoc = OpenTextFile("./JSON/Returns/" + key + ".txt", 1);


      var text = reopenedTextDoc.ReadAll();

}



it breaks on   var reopenedTextDoc = OpenTextFile("./JSON/Returns/" + key + ".txt", 1);

Error is : Object Expected



I'm assuming that the issue is that it's not done creating the text file by the time it tries to open it, is there any way to say "if this file is done being created, open it"?
  • Hi Mike, no, you do not have to replace the single "\" s returned by Project.Path



    JScript uses the "\" character to escape special characters in a literal string, e.g. \t (tab) and \r (carriage return), therefore, "\\" is simply the escape sequence for "\"



    E.g. the literal string "C:\\MyFiles\\DemoFile.txt" evaluates to "C:\MyFiles\DemoFile.txt"



    For the case with Project.Path as it not a literal string, the escape sequence is not required, e.g. 



    Project.Path + "\\
    JSON\\Returns\\test.txt" would evaluate to



    "C:\Users\Phil\Documents\TestComplete 9 Projects\TestProject1\JSON\Returns\test.txt"



    depending.of course, the actual path to the Project



    Phil
  • Philip_Baird's avatar
    Philip_Baird
    Community Expert
    Hi Mike, no, you do not have to replace the single "\" s returned by Project.Path



    JScript uses the "\" character to escape special characters in a literal string, e.g. \t (tab) and \r (carriage return), therefore, "\\" is simply the escape sequence for "\"



    E.g. the literal string "C:\\MyFiles\\DemoFile.txt" evaluates to "C:\MyFiles\DemoFile.txt"



    For the case with Project.Path as it not a literal string, the escape sequence is not required, e.g. 



    Project.Path + "\\
    JSON\\Returns\\test.txt" would evaluate to



    "C:\Users\Phil\Documents\TestComplete 9 Projects\TestProject1\JSON\Returns\test.txt"



    depending.of course, the actual path to the Project



    Phil
  • Philip_Baird's avatar
    Philip_Baird
    Community Expert
    Another quite handy trick is to take advantage of the CLR Bridge to access the .Net System.IO.Path class to combine paths , e.g



    var path = dotNET.System_IO.Path.Combine_2( Project.Path, "JSON\\Returns\\test.txt" );



    This will also evaluate to, depending on the value of  Project.Path,



    "C:\Users\Phil\Documents\TestComplete 9 Projects\TestProject1\JSON\Returns\test.txt"



    This has the advantage of the Path::Combine() method making sure all path separators are inserted if required when joining the Paths instead of having to write logic to do so.



    Test Complete provides a separate method for each of the four Path::Combine() overloads.



    Anyway, just something else to consider,

    Phil
  • Philip_Baird's avatar
    Philip_Baird
    Community Expert
    Hi Mike, have you considered using the aqFile object for file access?



    I find it alot more robust and simpler to use than using the ActiveXObject("Scripting.FileSystemObject") and may alleviate your issue.



    Regards,

    Phil Baird
  • mminnder's avatar
    mminnder
    Occasional Contributor
    Hey Phil,



    I have been messing with it all day and tried the aqFile way recently, the result was "Unable to open the file"



    It's terribly annoying how limited the information is in the debugging.
  • Philip_Baird's avatar
    Philip_Baird
    Community Expert
    Hi Mike,  you appear to be attempting to use relative paths, the Path parameter passed to the aqFile.WriteToTextFile method needs to be "The fully-qualified path to the file.", i.e starting from the drive letter as per the example in the documentation



    "C:\\MyFiles\\DemoFile.txt"



    You can use the following to obtain the root directory  of the running Project



    var projectPath = Project.Path;



    From there, you can hopefully build the path to your JSON files.



    Regards

    Phil Baird
  • TanyaYatskovska's avatar
    TanyaYatskovska
    SmartBear Alumni (Retired)

    Hi Mike,


     


    Also, pay attention to the fact that you need to use double slashes to specify a path in JScript. Like Phil posted:


    "C:\\MyFiles\\DemoFile.txt"


     

  • mminnder's avatar
    mminnder
    Occasional Contributor
    So from what you guys are saying, and a bit of investigating,  Project.Path returns a path without double slashes, but if I want to use aqFile to  reference a location I need to use double slashes.



    So this means if I want to apply the project path return to my aqFile method, I need to parse the return, and build code to insert extra slashes into it, as well as define the relative parts of my path in a variable before applying it to my method?



    Seems wierd,  I don't think I  had a problem with using a relative path or single slashes before, but I'll try it out now!
  • mminnder's avatar
    mminnder
    Occasional Contributor
    Also, I'm having trouble with replacing the slashes in the returned string.



    var changed = targetPath.replace("\", "//"); //won't work, IDE thinks it's commenting stuff out

    var changed = targetPath.replace("\", "/" + "/"); // doesn't work either



    any suggestions?
  • mminnder's avatar
    mminnder
    Occasional Contributor
    Alright, cool, thanks for all the assistance Phil!