Asterisk func FILE

Synopsis

Read or write text file.

Description

Read and write text file in character and line mode.

Examples:


Read mode (byte):

;reads the entire content of the file.

Set(foo=${FILE(/tmp/test.txt)})

;reads from the 11th byte to the end of the file (i.e. skips the first 10).

Set(foo=${FILE(/tmp/test.txt,10)})

;reads from the 11th to 20th byte in the file (i.e. skip the first 10, then read 10 bytes).

Set(foo=${FILE(/tmp/test.txt,10,10)})

Read mode (line):

; reads the 3rd line of the file.

Set(foo=${FILE(/tmp/test.txt,3,1,l)})

; reads the 3rd and 4th lines of the file.

Set(foo=${FILE(/tmp/test.txt,3,2,l)})

; reads from the third line to the end of the file.

Set(foo=${FILE(/tmp/test.txt,3,,l)})

; reads the last three lines of the file.

Set(foo=${FILE(/tmp/test.txt,-3,,l)})

; reads the 3rd line of a DOS-formatted file.

Set(foo=${FILE(/tmp/test.txt,3,1,l,d)})

Write mode (byte):

; truncate the file and write "bar"

Set(FILE(/tmp/test.txt)=bar)

; Append "bar"

Set(FILE(/tmp/test.txt,,,a)=bar)

; Replace the first byte with "bar" (replaces 1 character with 3)

Set(FILE(/tmp/test.txt,0,1)=bar)

; Replace 10 bytes beginning at the 21st byte of the file with "bar"

Set(FILE(/tmp/test.txt,20,10)=bar)

; Replace all bytes from the 21st with "bar"

Set(FILE(/tmp/test.txt,20)=bar)

; Insert "bar" after the 4th character

Set(FILE(/tmp/test.txt,4,0)=bar)

Write mode (line):

; Replace the first line of the file with "bar"

Set(FILE(/tmp/foo.txt,0,1,l)=bar)

; Replace the last line of the file with "bar"

Set(FILE(/tmp/foo.txt,-1,,l)=bar)

; Append "bar" to the file with a newline

Set(FILE(/tmp/foo.txt,,,al)=bar)


Syntax

{noformat}FILE(filename[,offset[,length[,options[,format]]]]){noformat}


Arguments

  • {{filename}}
  • {{offset}} - Maybe specified as any number. If negative, _offset_ specifies the number of bytes back from the end of the file.
  • {{length}} - If specified, will limit the length of the data read to that size. If negative, trims _length_ bytes from the end of the file.
  • {{options}}
    • {{l}} - Line mode: offset and length are assumed to be measured in lines, instead of byte offsets.
    • {{a}} - In write mode only, the append option is used to append to the end of the file, instead of overwriting the existing file.
    • {{d}} - In write mode and line mode only, this option does not automatically append a newline string to the end of a value. This is useful for deleting lines, instead of setting them to blank.
  • {{format}} - The _format_ parameter may be used to delimit the type of line terminators in line mode.
    • {{u}} - Unix newline format.
    • {{d}} - DOS newline format.
    • {{m}} - Macintosh newline format.


Asterisk | Applications | Functions | Variables | Expressions | Asterisk FAQ

Synopsis

Read or write text file.

Description

Read and write text file in character and line mode.

Examples:


Read mode (byte):

;reads the entire content of the file.

Set(foo=${FILE(/tmp/test.txt)})

;reads from the 11th byte to the end of the file (i.e. skips the first 10).

Set(foo=${FILE(/tmp/test.txt,10)})

;reads from the 11th to 20th byte in the file (i.e. skip the first 10, then read 10 bytes).

Set(foo=${FILE(/tmp/test.txt,10,10)})

Read mode (line):

; reads the 3rd line of the file.

Set(foo=${FILE(/tmp/test.txt,3,1,l)})

; reads the 3rd and 4th lines of the file.

Set(foo=${FILE(/tmp/test.txt,3,2,l)})

; reads from the third line to the end of the file.

Set(foo=${FILE(/tmp/test.txt,3,,l)})

; reads the last three lines of the file.

Set(foo=${FILE(/tmp/test.txt,-3,,l)})

; reads the 3rd line of a DOS-formatted file.

Set(foo=${FILE(/tmp/test.txt,3,1,l,d)})

Write mode (byte):

; truncate the file and write "bar"

Set(FILE(/tmp/test.txt)=bar)

; Append "bar"

Set(FILE(/tmp/test.txt,,,a)=bar)

; Replace the first byte with "bar" (replaces 1 character with 3)

Set(FILE(/tmp/test.txt,0,1)=bar)

; Replace 10 bytes beginning at the 21st byte of the file with "bar"

Set(FILE(/tmp/test.txt,20,10)=bar)

; Replace all bytes from the 21st with "bar"

Set(FILE(/tmp/test.txt,20)=bar)

; Insert "bar" after the 4th character

Set(FILE(/tmp/test.txt,4,0)=bar)

Write mode (line):

; Replace the first line of the file with "bar"

Set(FILE(/tmp/foo.txt,0,1,l)=bar)

; Replace the last line of the file with "bar"

Set(FILE(/tmp/foo.txt,-1,,l)=bar)

; Append "bar" to the file with a newline

Set(FILE(/tmp/foo.txt,,,al)=bar)


Syntax

{noformat}FILE(filename[,offset[,length[,options[,format]]]]){noformat}


Arguments

  • {{filename}}
  • {{offset}} - Maybe specified as any number. If negative, _offset_ specifies the number of bytes back from the end of the file.
  • {{length}} - If specified, will limit the length of the data read to that size. If negative, trims _length_ bytes from the end of the file.
  • {{options}}
    • {{l}} - Line mode: offset and length are assumed to be measured in lines, instead of byte offsets.
    • {{a}} - In write mode only, the append option is used to append to the end of the file, instead of overwriting the existing file.
    • {{d}} - In write mode and line mode only, this option does not automatically append a newline string to the end of a value. This is useful for deleting lines, instead of setting them to blank.
  • {{format}} - The _format_ parameter may be used to delimit the type of line terminators in line mode.
    • {{u}} - Unix newline format.
    • {{d}} - DOS newline format.
    • {{m}} - Macintosh newline format.


Asterisk | Applications | Functions | Variables | Expressions | Asterisk FAQ

Created by: RussP, Last modification: Tue 11 of Sep, 2012 (00:46 UTC) by admin
Please update this page with new information, just login and click on the "Edit" or "Discussion" tab. Get a free login here: Register Thanks! - Find us on Google+