FILE(filename[,offset,length,options,format])
Description
Read and write text file in character and line mode.
Examples:
Read mode (byte):
Reads the entire content of the file
same => n,Set(foo=${FILE(/tmp/test.txt)})
Reads from the 11th byte to the end of the file (i.e. skips the first 10)
same => n,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)
same => n,Set(foo=${FILE(/tmp/test.txt,10,10)})
Read mode (line):
Reads the 3rd line of the file
same => n,Set(foo=${FILE(/tmp/test.txt,3,1,l)})
Reads the 3rd and 4th lines of the file
same => n,Set(foo=${FILE(/tmp/test.txt,3,2,l)})
Reads from the third line to the end of the file
same => n,Set(foo=${FILE(/tmp/test.txt,3,,l)})
Reads the last three lines of the file
same => n,Set(foo=${FILE(/tmp/test.txt,-3,,l)})
Reads the 3rd line of a DOS-formatted file
same => n,Set(foo=${FILE(/tmp/test.txt,3,1,l,d)})
Write mode (byte):
Truncate the file and write bar
same => n,Set(FILE(/tmp/test.txt)=bar)
Append bar
same => n,Set(FILE(/tmp/test.txt,,,a)=bar)
Replace the first byte with bar (replaces 1 character with 3)
same => n,Set(FILE(/tmp/test.txt,0,1)=bar)
Replace 10 bytes beginning at the 21st byte of the file with bar
same => n,Set(FILE(/tmp/test.txt,20,10)=bar)
Replace all bytes from the 21st with bar
same => n,Set(FILE(/tmp/test.txt,20)=bar)
Insert bar after the 4th character
same => n,Set(FILE(/tmp/test.txt,4,0)=bar)
Write mode (line):
Replace the first line of the file with bar
same => n,Set(FILE(/tmp/foo.txt,0,1,l)=bar)
Replace the last line of the file with bar
same => n,Set(FILE(/tmp/foo.txt,-1,,l)=bar)
Append bar to the file with a newline
same => n,Set(FILE(/tmp/foo.txt,,,al)=bar)
Note
If live_dangerously in asterisk.conf is set to no, this function can only be executed from the dialplan, and not directly from external protocols.
Arguments
filenamerequiredoffset- 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.optionsl- 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.
User Notes
Know a tip or gotcha for this topic? Share it below and help others.
Contribute a note
Share a tip, gotcha, or practical example. Keep it under 2000 characters. No questions (use the Asterisk community forums for support). Wrap code in backticks.
Related
Functions
ABS()
AES_DECRYPT()
AES_ENCRYPT()
AGC()
AGENT()
AMI_CLIENT()
ARRAY()
AST_CONFIG()
AST_SORCERY()
BASE64_DECODE()
BASE64_ENCODE()
BASENAME()
BLACKLIST()
CALENDAR_BUSY()
CALENDAR_EVENT()
CALENDAR_QUERY()
CALENDAR_QUERY_RESULT()
CALENDAR_WRITE()
CALLCOMPLETION()
CALLERID()
CALLERPRES()
CDR()
CDR_PROP()
CHANNEL()
CHANNELS()
CHANNEL_EXISTS()
CHECKSIPDOMAIN()
CONFBRIDGE()
CONFBRIDGE_CHANNELS()
CONFBRIDGE_INFO()
CONNECTEDLINE()
CSV_QUOTE()
CURL()
CURLOPT()
CUT()
DAHDI_CHANNEL()
DB()
DB_DELETE()
DB_EXISTS()
DB_KEYCOUNT()
DB_KEYS()
DEC()
DELETE()
DENOISE()
DEVICE_STATE()
DIALGROUP()
DIALPLAN_EXISTS()
DIGIT_SUM()
DIRNAME()
DUNDILOOKUP()
DUNDIQUERY()
DUNDIRESULT()
ENUMLOOKUP()
ENUMQUERY()
ENUMRESULT()
ENV()
EVAL()
EVAL_EXTEN()
EVAL_SUB()
EXCEPTION()
EXISTS()
EXPORT()
EXTENSION_STATE()
FAXOPT()
FEATURE()
FEATUREMAP()
FIELDNUM()
FIELDQTY()
FILE()
FILE_COUNT_LINE()
FILE_FORMAT()
FILTER()
FRAME_DROP()
FRAME_TRACE()
GEOLOC_PROFILE()
GLOBAL()
GLOBAL_DELETE()
GLOBAL_EXISTS()
GROUP()
GROUP_COUNT()
GROUP_LIST()
GROUP_MATCH_COUNT()
HANGUPCAUSE()
HANGUPCAUSE_KEYS()
HASH()
HASHKEYS()
HINT()
HOLD_INTERCEPT()
IAXPEER()
IAXVAR()
ICONV()
IF()
IFMODULE()
IFTIME()
IMPORT()
INC()
ISNULL()
JABBER_RECEIVE()
JABBER_STATUS()
JITTERBUFFER()
JSON_DECODE()
KEYPADHASH()
LEN()
LISTFILTER()
LOCAL()
LOCAL_PEEK()
LOCK()
LOG_GROUP()
LTRIM()
MAILBOX_EXISTS()
MASTER_CHANNEL()
MATH()
MAX()
MD5()
MEETME_INFO()
MESSAGE()
MESSAGE_DATA()
MIN()
MINIVMACCOUNT()
MINIVMCOUNTER()
MIXMONITOR()
MUTEAUDIO()
ODBC()
ODBC_FETCH()
PARK_GET_CHANNEL()
PASSTHRU()
PERIODIC_HOOK()
PITCH_SHIFT()
PJSIP_AOR()
PJSIP_CONTACT()
PJSIP_DIAL_CONTACTS()
PJSIP_DTMF_MODE()
PJSIP_ENDPOINT()
PJSIP_HEADER()
PJSIP_HEADERS()
PJSIP_HEADER_PARAM()
PJSIP_INHERITABLE_HEADER()
PJSIP_MEDIA_OFFER()
PJSIP_MOH_PASSTHROUGH()
PJSIP_PARSE_URI()
PJSIP_PARSE_URI_FROM()
PJSIP_RESPONSE_HEADER()
PJSIP_RESPONSE_HEADERS()
PJSIP_SEND_SESSION_REFRESH()
PJSIP_TRANSFER_HANDLING()
POLARITY()
POP()
PP_EACH_EXTENSION()
PP_EACH_USER()
PRESENCE_STATE()
PUSH()
QUEUE_EXISTS()
QUEUE_GET_CHANNEL()
QUEUE_MEMBER()
QUEUE_MEMBER_COUNT()
QUEUE_MEMBER_LIST()
QUEUE_MEMBER_PENALTY()
QUEUE_VARIABLES()
QUEUE_WAITING_COUNT()
QUOTE()
RAND()
REALTIME()
REALTIME_DESTROY()
REALTIME_FIELD()
REALTIME_HASH()
REALTIME_STORE()
RECORDING_INFO()
REDIRECTING()
REGEX()
REPLACE()
RTRIM()
SAYFILES()
SCRAMBLE()
SET()
SHA1()
SHARED()
SHELL()
SHIFT()
SIPPEER()
SIP_HEADER()
SIP_HEADERS()
SMDI_MSG()
SMDI_MSG_RETRIEVE()
SORT()
SPEECH()
SPEECH_ENGINE()
SPEECH_GRAMMAR()
SPEECH_RESULTS_TYPE()
SPEECH_SCORE()
SPEECH_TEXT()
SPRINTF()
SQL_ESC()
SQL_ESC_BACKSLASHES()
SRVQUERY()
SRVRESULT()
STACK_PEEK()
STAT()
STIR_SHAKEN()
STIR_SHAKEN_ATTESTATION()
STRBETWEEN()
STRFTIME()
STRPTIME()
STRREPLACE()
SYSINFO()
TALK_DETECT()
TESTTIME()
TIMEOUT()
TOLOWER()
TONE_DETECT()
TOUPPER()
TRIM()
TRYLOCK()
TXTCIDNAME()
UNLOCK()
UNSHIFT()
URIDECODE()
URIENCODE()
UUID()
VALID_EXTEN()
VARIABLE_EXISTS()
VERSION()
VMCOUNT()
VM_INFO()
VOLUME()