• <GetStart>
  • CSPro User's Guide
    • The CSPro System
    • Data Dictionary Module
    • The CSPro Language
    • Data Entry Module
    • Batch Editing Applications
    • Tabulation Applications
    • Data Sources
    • CSPro Statements and Functions
    • Templated Reporting System
    • HTML and JavaScript Integration
    • Action Invoker
      • Overview
      • Execution Environments
      • Security and Formatting Options
      • Base Actions
      • Application Namespace
      • Clipboard Namespace
      • Data Namespace
      • Dictionary Namespace
      • File Namespace
        • File Action Invoker Namespace
        • File.copy Action
        • File.readBytes Action
        • File.readLines Action
        • File.readText Action
        • File.writeBytes Action
        • File.writeLines Action
        • File.writeText Action
      • Hash Namespace
      • Localhost Namespace
      • Logic Namespace
      • Message Namespace
      • Path Namespace
      • Settings Namespace
      • Sqlite Namespace
      • System Namespace
      • UI Namespace
    • Appendix
  • <CSEntry>
  • <CSBatch>
  • <CSTab>
  • <DataViewer>
  • <TextView>
  • <TblView>
  • <CSFreq>
  • <CSDeploy>
  • <CSPack>
  • <CSDiff>
  • <CSConcat>
  • <Excel2CSPro>
  • <CSExport>
  • <CSIndex>
  • <CSReFmt>
  • <CSSort>
  • <ParadataConcat>
  • <ParadataViewer>
  • <CSCode>
  • <CSDocument>
  • <CSView>
  • <CSWeb>

File.readLines Action

Format
s = CS.File.readLines(path := ...ʃ, encoding := ...ʅ)
ArgumentDescriptionTypes / Required
pathThe path of the file to read.string
required
encodingThe encoding to use to decode the file's text.
The default value is "UTF-8".
string
not required
Description
The File.readLines action reads a file, specified as path, as text, returning the entire file's content as an array of strings. The file's content is split on newline characters, with each line returned as an element of the array without the newline character included.
The encoding argument allows you specify how the contents of the file are decoded to text. There are few cases where this may be necessary, as the default of reading UTF-8 text will generally meet the needs of most users. Options include:
  • "ANSI": The contents are decoded as part of the Windows code page.
  • "UTF-8": The contents are decoded as UTF-8, regardless of whether there is a byte order mark (BOM).
  • "UTF-8-BOM": The contents are decoded as UTF-8, regardless of whether there is a BOM.
Return Value
The action returns an array of strings containing the file's content, with each line as an entry in the array.
Exceptions
The action throws an exception if any of its arguments are not specified in a valid form or if the file does not exist or cannot be read.
Example (JavaScript)
// read all the lines from a file
CS.File.readLinesAsync({
    path: 
"text-file.txt"
})
.then(lines => {
   
// remove whitespace from the end of each line
    const trimmedLines = lines.map(line => line.trimEnd());

   
// write the lines
    return CS.File.writeLinesAsync({
        path: 
"text-file (trimmed).txt",
        lines: trimmedLines
    });
})
.then(() => console.log(
"Successfully read, trimmed, and wrote the lines."))
.
catch(error => console.log(error));
See also: File Action Invoker Namespace, File.writeLines Action, File.readBytes Action, File.readText Action, File.read Function