6 Replies Latest reply on Nov 17, 2014 1:01 PM by Hal DeVore

    fOpen - Return the first line of the file

    Flavio Bonacordi

      Hi Everyone,

       

      I'm working to have a script that will analyze a value present into a log file.

       

      All my log lines are the same and the must updated information is located in the very first line of the file.

       

      So, basically I need read the very fist line of my log file.

       

      But... I'm facing a issue.

       

      I have my script working and i'm getting the first line..... for some files...

       

      Here is the script

       

      @codepage(1252)
      
      
      benchmark WebBenchmarkName
      
      
      use"WebAPI.bdh"
      use"Kernel.bdh"
          
      dcluser 
          user
            WebUser   
          transactions
              TMain : 1;
      
      
      dcltrans
          transaction
            TMain
            var
               hFile, nRead, nSize, nPos1, cpt, nPos: number;
               sData, sLine : string;
               sFile : string;
               bFileExists : boolean;
      
      
            begin
               sFile:="\\\\dalapp65\\sms_logs\\Connlogprd.log"; 
      
      
               bFileExists := FExists(sFile);
         
               if bFileExists then
                   FOpen(hFile, sFile, OPT_FILE_ACCESS_READ, OPT_FILE_OPEN, OPT_FILE_SHARE_READ);
                   // calculate and display the number of bytes in the file
                   FSizeGet(hFile, nSize);
                   print("file size = "+string(nSize)); 
                   // read nSize number of bytes into sData
                   FRead(hFile, sData, nSize, nRead);
                   print(string(nRead)+" bytes read");
                   print(sFile);
                   print(sData);
                else
                  Print("No Tests to be executed.");
                  sData:=" ";
                end;
            
            nPos1 := StrSearch(sData, "\r", STR_SEARCH_FIRST);
      
      
            print("line1: " + substr(sData,sLine,1,nPos1));
      
      
               FClose(hFile);
            end TMain;
      

       

       

       

      When I run this I got the following information on the Output screen on Workbench server: ( see Output1 file attached)

       

      But when I run this trying to read the file that I need I got the following information on the output screen (see output2 file attached).

       

      There is no error messages, no warnings... the only that don't happen is read the file that I need.

       

      Both files are located in the same directory which clear out the level of access to the files..I'm able to open the file that I need using Notepad++ or Notepad and I don't get any errors.

       

      Could someone have any hint about it?

       

      Maybe Hal DeVore, Dan Egner will have something to me...

       

      Message was edited by: Flavio Bonacordi The copy/paste of the code wasn't correct... this is the right version

        • 1. Re: fOpen - Return the first line of the file
          Hal DeVore

          I don't see where you are actually reading the contents of the file.

           

          FOpen opens the file and initializes the file handle.  FSizeGet retrieves the size of the file.  Neither of those actually reads data from the file.  So your sData variable remains uninitialized and can contain garbage.

           

          Is there an FRead call somewhere that you edited out for the above post?

           

          --Hal

          • 2. Re: fOpen - Return the first line of the file
            Flavio Bonacordi

            Hey Hal,

             

            The copy/paste wasn't correct... some lines were missing... if I didn't have the fRead at first... the first file will not work...

             

            Just paste the correct code now...

             

            fRead is there...

            • 3. Re: fOpen - Return the first line of the file
              Hal DeVore

              I didn't think it would be that easy! 

               

              Two thoughts:

              1. The size might be excessive.  I can't recall the max size of a string variable in BDL (if there is one).  Reading an entire multi-megabyte file when you want only the first line might be the issue.  Thoughts on this below
              2. Have you verified that the "real" file is text and not binary or encoded in some form?  If the file is encoded (say UTF-8) you might need to decode it using the FromEncoding function.

               

              Thoughts for reading a smaller amount of data:

              • The easiest would be brute force.  Just pick a number that is highly likely to be larger than the line of text you want and use that in the FRead in place of the size of the entire file.
              • You might be able to use the File...Load functions but they will load the entire file into memory.
              • A third approach would be to use a script (Windows cmd file) to extract the first line, possibly putting it into a different file, then read that file from your BDL.  Hunt up "Process Control Functions" in the Workbench help.

               

              --Hal

              • 4. Re: Re: fOpen - Return the first line of the file
                Flavio Bonacordi

                Hey Hal DeVore,

                 

                I have limited the read nSize to 500 bytes... all the lines of my file are like the following one

                 

                Verified at 11/17/2014 12:03:18 -- 2014-11-17 12:03:17,541 Total number of available connection ==> |185|
                Verified at 11/17/2014 12:02:19 -- 2014-11-17 12:02:16,715 Total number of available connection ==> |185|
                Verified at 11/17/2014 12:01:19 -- 2014-11-17 12:01:17,190 Total number of available connection ==> |185|
                Verified at 11/17/2014 12:00:19 -- 2014-11-17 12:00:18,590 Total number of available connection ==> |185|
                Verified at 11/17/2014 11:59:19 -- 2014-11-17 11:52:03,499 Total number of available connection ==> |170|
                Verified at 11/17/2014 11:58:18 -- 2014-11-17 11:52:03,499 Total number of available connection ==> |170|
                Verified at 11/17/2014 11:57:19 -- 2014-11-17 11:52:03,499 Total number of available connection ==> |170|
                Verified at 11/17/2014 11:56:19 -- 2014-11-17 11:52:03,499 Total number of available connection ==> |170|
                Verified at 11/17/2014 11:55:19 -- 2014-11-17 11:52:03,499 Total number of available connection ==> |170|
                Verified at 11/17/2014 11:54:19 -- 2014-11-17 11:52:03,499 Total number of available connection ==> |170|
                Verified at 11/17/2014 11:53:18 -- 2014-11-17 11:52:03,499 Total number of available connection ==> |170|
                Verified at 11/17/2014 11:52:19 -- 2014-11-17 11:52:03,499 Total number of available connection ==> |170|
                

                 

                File has been created using a powershell script using the Add-Contend instruction.

                 

                I believe this is why I may not able to read the file correctly... When I create a file in Windows using the Notepad++ the software show me in the status bar... ANSI (that means the file encode of that file is ANSI)

                 

                When I open the file created by the powershell script is showing up as UCS-2 Little Endian... and I need to confess that I never heard about that kind of encoding before.

                 

                Looks like I need to deal with the encoding... I will research for the FromEncoding funciton. Thanks for your hint!

                • 5. Re: Re: fOpen - Return the first line of the file
                  Flavio Bonacordi

                  Hey Hal DeVore,

                   

                  YOU ARE AWESOME!

                   

                  Change my powershell script to create the file with ANSI and not UCS-2 and my script is able to read the very fist line of my file! :-)

                   

                  YOU ROCK MAN!!!

                   

                  Thanks!!

                  • 6. Re: Re: fOpen - Return the first line of the file
                    Hal DeVore

                    Thanks, Flavio!  I was gonna suggest that you could set the encoding during creation if you owned the Powershell script but obviously you figured that out.

                     

                    --Hal

                    South Coast Software Consulting