5 Replies Latest reply on Oct 3, 2011 6:25 AM by Harbs.

    .jsx read from tabbed delimited text

    jmituzas Level 1

      I need to know how to read data from tabbed delimited text file in order to export merged documents to correct location.

      • will always be 4th tab second row (group number) directory will be named by group number
      • have directories already created by privious .vbs script

       

      within previous .vbs script to work with this data file was written as so:

       


          Call TwoDimensionArrayTest
      Sub TwoDimensionArrayTest

      Dim fso
          Dim oFile
          Dim arrline
          Dim arrItem
          Dim i
          Dim arrMain()
          Dim sFileLocation, strResults
          Dim filesys, folder, path 

          Const forReading = 1
          
      strFolder = "P:\RxCut\In Design Implementation\build\Co-Brand\"
      mkdir = "P:\RxCut\In Design Implementation\"
      Set objFSO = CreateObject("Scripting.FileSystemObject")
      For Each objFile In objFSO.GetFolder(strFolder).Files
          If Right(LCase(objFile.Name), 4) = LCase(".txt") Then
          
              ' The file contains on each line:
          ' Text1 (tab) Text2 (tab) Text3 (tab) Text4
          ' Text5 (tab) Text6 (tab) Text7 (tab) Text8
      'etc etc

      Set fso = CreateObject("Scripting.FileSystemObject")
              sFileLocation = objFile.Name
              
              Set oFile = fso.OpenTextFile(objFile.Name, forReading, False)
              
          Do While oFile.AtEndOfStream <> True
              strResults = oFile.ReadAll
          Loop
          
          ' Close the file
          oFile.Close
          
      ' Release the object from memory
          Set oFile = Nothing
          
      ' Return the contents of the file if not Empty
          If Trim(strResults) <> "" Then
              
              ' Create an Array of the Text File
              arrline = Split(strResults, vbNewLine)
          End If

          For i = 0 To UBound(arrline)
              If arrline(i) = "" Then
                  ' checks for a blank line at the end of stream
                  Exit For
              End If 
              
              ReDim Preserve arrMain(i)
              
                  arrMain(i) = Split(arrline(i), vbTab)

          Next
          
          path = arrMain(1)(3)
          dir = mkdir & path
          set filesys=CreateObject("Scripting.FileSystemObject") 
      If Not filesys.FolderExists(dir) Then 
      Set folder = filesys.CreateFolder(dir) 
      End If
          
         dir = mkdir & path
         Set folder = filesys.CreateFolder(dir & "\Web")
         Set folder = filesys.CreateFolder(dir & "\Web\Web Cards")
         Set folder = filesys.CreateFolder(dir & "\Web\Web Headers")
         
             fso.MoveFile sFileLocation, arrMain(1)(3) & ".txt"
             
       End If 
       Next
      End Sub ' TwoDimensionArrayTest

       

      How can I achieve something simular in .jsx?

      Thanks in advance,

      Joe

        • 1. Re: .jsx read from tabbed delimited text
          jmituzas Level 1

          forgot to add...

           

          data will have to be placed:

          app.open(File(dirPath + "Card~" + (group number) + ".indd"));   

          and also:

          fileName = (group number) + " " + myPageName + " " + date + ".pdf";        
                  myFilePath
          = dirPath + docType + "/" + fileName;
                  myFile
          = new File(myFilePath);
                  myDocument
          .exportFile(ExportFormat.pdfType, myFile, false);
          • 2. Re: .jsx read from tabbed delimited text
            John Hawkinson Level 5

            Hi, Joe:

            I need to know how to read data from tabbed delimited text file in order to export merged documents to correct location.

            • will always be 4th tab second row (group number) directory will be named by group number

            I hope it is not necessary to read through all that VB to answer your question.

            I assume you are aware of how to read a file in JS? If so, the question is merely how you split a line on tabs?

            Just use

             

            var fields = linestring.split("\t");
            

             

            where linestring is a String representing a line from the file. Than fields[0] is the first field, field[3] is the 4th field, etc.

            Your bulleted quoted text above does not appear to be grammatical english.

            1 person found this helpful
            • 3. Re: .jsx read from tabbed delimited text
              Harbs. Level 6

              I'm not sure I'm following exactly where you are getting stuck.

               

              I did not see anywhere code to actually read the file. app.open() is for opening InDesign files (or idml). If you want to read a text file and parse that, you need something like this.

               

              var file = File(filePath);
              file.open('r');
              var fContents = file.read();
              file.close();
              var lines = fContents.split(/[\r\n]+/);
              

               

              You would then loop through the lines to do what you need...

               

              Harbs

              • 4. Re: .jsx read from tabbed delimited text
                jmituzas Level 1

                Well figured it out... after lots of searching I have found this link which helped out still had to edit a bit:
                http://extendscript.blogspot.com/2009/06/read-tab-delimited-text-file.html

                 

                var aFile = File("P:/RxCut/In Design Implementation/build/automate/automate.txt");

                var fileData = readTabDelimitedFile ( aFile ) ;

                //$.writeln(fileData[1][3]); //should contain first tab on second line

                tstdata = fileData[1][3];

                       var group = tstdata;

                 

                 

                function readTabDelimitedFile ( fPath ) {

                 

                var returnArray = new Array ( ) ;

                 

                //-- Verify that the file exists

                var fileObject = File ( fPath ) ;

                if ( ! fileObject.exists ) {

                return returnArray ; // an empty array because the file doesn't exist.

                }

                //-- Create a regular expression for a tab.

                var tabExpression = new RegExp ( '\\t' ) ;

                 

                //-- Read the file.

                try {

                //-- The file has to be open.

                fileObject.open ('r') ; //-- Open for reading.

                 

                while ( ! fileObject.eof ) {

                var currentLine = fileObject.readln () ;

                 

                if ( tabExpression.test( currentLine ) ) {

                 

                returnArray.push(currentLine.split ('\t')) ;

                }

                }

                fileObject.close() ;

                }

                 

                catch (errMain) {

                try {

                 

                fileObject.close() ;

                }

                //-- if the close generates an error skip it.

                catch (errInner ) { /* nothing here */ }

                }

                 

                return returnArray ;

                 

                var group = returnArray

                }

                 

                Thanks everyone for your help,

                Joe

                • 5. Re: .jsx read from tabbed delimited text
                  Harbs. Level 6

                  FWIW, I always make file reading operations as short as possible. You don't want to leave files open if your script aborts in middle.

                   

                  I'm pretty sure it's quicker to read the whole file and then split the string into an array too.