4 Replies Latest reply on Aug 22, 2012 9:04 AM by Jörg S.

    Exported text and carriage return

    Jörg S.

      Hi,

       

      after exporting a selected story with a javascript into an external text file, there's always a carriage return too much at the end of the exported file.

      How can I get rid of that CR?

       

      Thanks!

        • 1. Re: Exported text and carriage return
          [Jongware] Most Valuable Participant

          Open in a text editor and remove it

           

          How are you 'exporting'? Using writeln, or with the built-in "exportFile" method, as ExportFormat.TEXT_TYPE?

           

          .. I just tested, the built-in function indeed does add a carriage return where there is none in my test story. (Testing some more--) Oh yeah, also if there already is one as well.

           

          Since you cannot change the behavior of built-in functions, all you can do is have your script immediately load the exported file, remove the final return, and then save it again.

          • 2. Re: Exported text and carriage return
            Jörg S. Level 1

            Well, that was my first thought too.

            But this function returns the same result. An additional return at the end.

            What's wrong?

             

            function delCR(file){

                var myFile = file.open("e") ;

                var myCont = file.read();

                var myShortCont = myCont.substr(0, (myCont.length - 1));

                file.write(myShortCont);

                file.close();

                return;

            }

            • 3. Re: Exported text and carriage return
              [Jongware] Most Valuable Participant

              I *think* the 'e' mode is messing things up here, i.e., you *are* changing the string but re-writing it overwrites the original text minus the carriage returns at the end, and since you stop writing there, the original file data remains. Not sure if that's how "e" is supposed to work, but then again, removing it from the equation makes things work.

               

              (It actually also needed a 'file.seek(0)' to work.)

               

              This is a slightly better way: open for reading only, read, close; remove carriage returns (and line feeds as well, since I happen to work on Windows here); open for writing, and save.

               

              For safety, I replaced the blind culling of 'any last character' with a; any number of hard returns at the end will be removed, or none if there weren't any to begin with.

               

              function delCR(file)
              {
                  var myFile = file.open("r") ;
                  var myCont = file.read();
                  file.close();
                  var myShortCont = myCont;
                  while (myShortCont.substr(myShortCont.length-1) == '\r' || myShortCont.substr(myShortCont.length-1) == '\n')
                   myShortCont = myShortCont.substr(0, myShortCont.length - 1);
                  myFile = file.open("w") ;
                  file.write(myShortCont);
                  file.close();
              }
              
              • 4. Re: Exported text and carriage return
                Jörg S. Level 1

                Great! That helped me out.

                Thank you very much!!