<html>
<head>
    <base href="https://wiki.asterisk.org/wiki">
            <link rel="stylesheet" href="/wiki/s/en/2172/18/9/_/styles/combined.css?spaceKey=AST&amp;forWysiwyg=true" type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://wiki.asterisk.org/wiki/display/AST/Asterisk+11+Function_FILE">Asterisk 11 Function_FILE</a></h2>
    <h4>Page  <b>added</b> by             <a href="https://wiki.asterisk.org/wiki/display/~wikibot">Wiki Bot</a>
    </h4>
         <br/>
    <div class="notificationGreySide">
         <h1><a name="Asterisk11Function_FILE-FILE%28%29"></a>FILE()</h1>

<h3><a name="Asterisk11Function_FILE-Synopsis"></a>Synopsis</h3>

<p>Read or write text file.</p>
<h3><a name="Asterisk11Function_FILE-Description"></a>Description</h3>



<p>Read and write text file in character and line mode.</p>

<p>Examples:</p>



<p>Read mode (byte):</p>

<p>;reads the entire content of the file.</p>

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

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

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

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

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



<p>Read mode (line):</p>

<p>; reads the 3rd line of the file.</p>

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

<p>; reads the 3rd and 4th lines of the file.</p>

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

<p>; reads from the third line to the end of the file.</p>

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

<p>; reads the last three lines of the file.</p>

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

<p>; reads the 3rd line of a DOS-formatted file.</p>

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



<p>Write mode (byte):</p>

<p>; truncate the file and write "bar"</p>

<p>Set(FILE(/tmp/test.txt)=bar)</p>

<p>; Append "bar"</p>

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

<p>; Replace the first byte with "bar" (replaces 1 character with 3)</p>

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

<p>; Replace 10 bytes beginning at the 21st byte of the file with "bar"</p>

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

<p>; Replace all bytes from the 21st with "bar"</p>

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

<p>; Insert "bar" after the 4th character</p>

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



<p>Write mode (line):</p>

<p>; Replace the first line of the file with "bar"</p>

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

<p>; Replace the last line of the file with "bar"</p>

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

<p>; Append "bar" to the file with a newline</p>

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

<h3><a name="Asterisk11Function_FILE-Syntax"></a>Syntax</h3>

<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent">
<pre>FILE(filename,offset,length,options,format)</pre>
</div></div>
<h5><a name="Asterisk11Function_FILE-Arguments"></a>Arguments</h5>


<ul>
        <li>filename</li>
        <li>offset - Maybe specified as any number. If negative, <em>offset</em> specifies the number of bytes back from the end of the file.</li>
        <li>length - If specified, will limit the length of the data read to that size. If negative, trims <em>length</em> bytes from the end of the file.</li>
        <li>options
        <ul>
                <li>l - Line mode: offset and length are assumed to be measured in lines, instead of byte offsets.</li>
                <li>a - In write mode only, the append option is used to append to the end of the file, instead of overwriting the existing file.</li>
                <li>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.</li>
        </ul>
        </li>
        <li>format - The <em>format</em> parameter may be used to delimit the type of line terminators in line mode.
        <ul>
                <li>u - Unix newline format.</li>
                <li>d - DOS newline format.</li>
                <li>m - Macintosh newline format.</li>
        </ul>
        </li>
</ul>


<h3><a name="Asterisk11Function_FILE-SeeAlso"></a>See Also</h3>


<ul>
        <li><a href="/wiki/pages/createpage.action?spaceKey=AST&amp;title=Asterisk+11+Function_FILE_COUNT_LINE&amp;linkCreation=true&amp;fromPageId=20187184" class="createlink">Asterisk 11 Function&#95;FILE&#95;COUNT&#95;LINE</a></li>
        <li><a href="/wiki/pages/createpage.action?spaceKey=AST&amp;title=Asterisk+11+Function_FILE_FORMAT&amp;linkCreation=true&amp;fromPageId=20187184" class="createlink">Asterisk 11 Function&#95;FILE&#95;FORMAT</a></li>
</ul>


<h3><a name="Asterisk11Function_FILE-ImportVersion"></a>Import Version</h3>

<p>This documentation was imported from Asterisk Version SVN-trunk-r370322</p>
    </div>
    <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;" class="grey">
                        <a href="https://wiki.asterisk.org/wiki/users/removespacenotification.action?spaceKey=AST">Stop watching space</a>
            <span style="padding: 0px 5px;">|</span>
                <a href="https://wiki.asterisk.org/wiki/users/editmyemailsettings.action">Change email notification preferences</a>
</div>
       <a href="https://wiki.asterisk.org/wiki/display/AST/Asterisk+11+Function_FILE">View Online</a>
              |
       <a href="https://wiki.asterisk.org/wiki/display/AST/Asterisk+11+Function_FILE?showComments=true&amp;showCommentArea=true#addcomment">Add Comment</a>
           </div>
</div>
</div>
</div>
</div>
</body>
</html>