AVEVA. PML BASICS.Read file

Today I will briefly explain the basic principles of working with file, using PML code.

ae113fa1a66eb85c4150c181256863d9

The main tasks for PML Developers, when they work with files are:
-Process directories and filter certain file extensions
-Open and read file(line by line or fully)
-Search through file and process data
-Add info to file or overwrite its content

Here are some techniques for working with files.

Open certain files with name ‘text.txt’, read line by line to Pdms console window and output line content, if line content matches string:

Method 1 (For files with lines number less then 10 000)

–create string variable with file path
!fileName = ‘C:\test.txt’
–create object variable of type FILE
!fileObj = OBJECT FILE(!fileName)
–create array variable and fill all content of that file into it using method ReadFile
!fileContent = !fileObj.Readfile()
–Loop through the array of file content using line numbers variable !x
do !x from 1 to !fileContent.Size()
 –Check line content for matching of criteria, should contain ‘*Valve*’
 if (!fileContent[!x].Matchwild(‘*Valve*’)) then
  –if matches print line content in Pdms\E3D command line
  $P Line number is $!x and the value is  $!fileContent[$!x]
 endif
enddo

Method 2 (for files with line number greater then 10 000)

–create string variable with file path
!fileName = ‘C:\test.txt’
–create object variable of type FILE
!oFileName= OBJECT FILE(!fileName)
–Open file for read
!oFileName.Open(‘READ’)
–Loop through the lines of file
do !recordInd
–Save the value of line
!line = !oFileName.readRecord()
–Check if it is exists, if not stop reading
if !line.set() then
  –Check line content for matching of criteria, should contain ‘*Valve*’
  if(!line.Matchwild(‘*Valve*’))then
   –if matches print line content in Pdms\E3D command line
    $P Line number is $!recordInd and the value is  $!line
  endif
 else
  break
 endif
enddo
–Close file
!oFileName.Close()

Read more : pmlinpdms

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s