locked
FileIO with writeTextAsync and appendTextAsync Problems

    Question

  • hi guys,

    i have 2 problems, i hope you can help me :)

    (this program is for a school with windows surface rt tablets to connect printers and network drives)

    1) in my function LoginButtonClickHandler  i create 3 files with content

    - > So all files will created fine, but i have only in my first created file a content.

    2) content for the files (line 2,3,4...)

    -> i see only my first line and not the other...


    function LoginButtonClickHandler(eventInfo) { // VAR for FN: LoginButtonClickHandler var del_nt_connection = "del-nt-connections.bat"; var connect_printer = "connect-printers.bat"; var connect_rdrive = "connect-network-drive.bat"; var localFolder = Windows.Storage.KnownFolders.picturesLibrary; var VARUsername = document.getElementById("UsernameFeld").value; var VARPassword = document.getElementById("PasswordFeld").value; var VARDomain = document.getElementById("DomainFeld").value; var TestInputVARS = VARUsername + VARPassword + VARDomain; // Write files localFolder.createFileAsync(connect_printer, Windows.Storage.CreationCollisionOption.replaceExisting).then(function (FileContentPrinter) { Windows.Storage.FileIO.writeTextAsync(FileContentPrinter, "FirstLine"); Windows.Storage.FileIO.appendTextAsync(FileContentPrinter, "SecondLine"); Windows.Storage.FileIO.appendTextAsync(FileContentPrinter, "NextLine...."); Windows.Storage.FileIO.appendTextAsync(FileContentPrinter, "NextLine........."); document.getElementById("Output2").innerText = "file1 created..."; // test execution }); localFolder.createFileAsync(connect_rdrive, Windows.Storage.CreationCollisionOption.replaceExisting).then(function (FileContentNetwork) { Windows.Storage.FileIO.writeTextAsync(FileContentNetwork, "TEST FirstLine"); Windows.Storage.FileIO.appendTextAsync(FileContentNetwork, "SecondLine"); document.getElementById("Output3").innerText = "file2 created..."; // test execution }); localFolder.createFileAsync(del_nt_connection).then(function (FileContentDisconnect) { Windows.Storage.FileIO.writeTextAsync(FileContentDisconnect, "TEST TEST FirstLine"); Windows.Storage.FileIO.appendTextAsync(FileContentDisconnect, "SecondLine"); document.getElementById("Output4").innerText = "file3 created..."; // test execution }); document.getElementById("Output1").innerText = TestInputVARS; // test input / output of TestInputVARS }



    Sunday, March 23, 2014 7:17 PM

Answers

  • I'm not sure what you're asking now, with the VB script. Are you trying to make the async calls inside a processing loop? If so, then, there are techniques you can use with WinJS.Promise.join to make each subseqeuent async call dependent on the ones that came before it. Appendix A of my second edition book (including the preview) shows some examples of this--it shows it using an array reduce method, but you could use the approach for a loop. See the section "Parallel Promises with Sequential Results."
    Thursday, March 27, 2014 6:51 PM

All replies

  • Hi Ace,

    As you can see the method name, it is a Async/Await method, i'm afraid that there is something wrong here.

    When the system is writing text to file by using: Windows.Storage.FileIO.writeTextAsync(FileContentPrinter, "FirstLine"), the second method is executed at the same time, but the file is kept by first thread and therefore the second method cannot access the file and write text in.

    I would suggest you to use done method or then method for helping with such situation. When the first method completed, execute the next method.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, March 24, 2014 4:48 AM
    Moderator
  • By that I believe you mean making sure to chain the async operations together, as merely calling .then/.done on each without chaining will result in the same behavior.

    For example, the first block of code should look like this:

    var file;
    
    localFolder.createFileAsync(connect_printer, Windows.Storage.CreationCollisionOption.replaceExisting)
    .then(function (FileContentPrinter) {
        file = FileContentPrinter;
        return Windows.Storage.FileIO.writeTextAsync(file, "FirstLine");
    }).then(function () {
        return Windows.Storage.FileIO.appendTextAsync(file, "SecondLine");
    }).then(function () {
        return Windows.Storage.FileIO.appendTextAsync(file, "NextLine....");
    }).then(function () {
        return Windows.Storage.FileIO.appendTextAsync(file, "NextLine.........");
    }).done(function () {
        document.getElementById("Output2").innerText = "file1 created..."; // test execution
    }, function (err) {
        //Handle any errors from the chain
    });
    

    I have to ask, with this code structure, why you wouldn't just create a single string with all your text, and then do one write to the file. Then you'd avoid all this chaining with the same result.

    Kraig

    Author, Programming Windows Store Apps with HTML, CSS, and JavaScript, Second Edition, a free ebook from Microsoft Press. First edition (for Windows 8) also available.


    Monday, March 24, 2014 3:11 PM
  • hi kraig,

    yes i know... but i would like to create a batch file like this demo:

    @echo off REM Connect Printer \\192.168.255.50\PDFCreator \\192.168.255.50\PDFCreator2 \\192.168.255.50\PDFCreator3" \\192.168.255.50\PDFCreator4"

    from my vars... var PRINTER05 = "\\192.168.255.50\PDFCreator5"; var PRINTER06 = "\\192.168.255.50\PDFCreator6"; var PRINTER07 = "\\192.168.255.50\PDFCreator7"; var PRINTER08 = "\\192.168.255.50\PDFCreator8"; var PRINTER09 = "\\192.168.255.50\PDFCreator9"; var PRINTER10 = "\\192.168.255.50\PDFCreator10";

    and allways wen i use a single string i have only one large line with my content...

    in "perl" i use "\n" to jump in the next line and i really don't know how this works with "writeTextAsync".

    -------

    arrrrrrrrr

    i dont know whats wrong with my script now the output on the html page comes and go after a sec.

    And the ".then(function..." does not work, the VB only execute the first function and the second dont work. the files are created but only the first file have a content and only the first line :/

    here is a download link of the small script:

    http://smc-alpenglueher.com/VirtualSchoolConnect.zip


    • Edited by Ace - 1994 Wednesday, March 26, 2014 8:20 PM
    Wednesday, March 26, 2014 8:19 PM
  • I'm not sure what you're asking now, with the VB script. Are you trying to make the async calls inside a processing loop? If so, then, there are techniques you can use with WinJS.Promise.join to make each subseqeuent async call dependent on the ones that came before it. Appendix A of my second edition book (including the preview) shows some examples of this--it shows it using an array reduce method, but you could use the approach for a loop. See the section "Parallel Promises with Sequential Results."
    Thursday, March 27, 2014 6:51 PM