• <GetStart>
  • CSPro User's Guide
    • The CSPro System
    • Data Dictionary Module
    • The CSPro Language
    • Data Entry Module
    • Batch Editing Applications
    • Tabulation Applications
    • Data Sources
    • Synchronization
    • CSPro Statements and Functions
    • Text Templates
    • Templated Reporting System
    • HTML, Markdown, and JavaScript Integration
    • Action Invoker
      • Overview
      • Execution Environments
      • Security, Resource Management, 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
      • Network Namespace
      • Path Namespace
      • Settings Namespace
      • Sqlite Namespace
      • Sync Namespace
      • System Namespace
      • UI Namespace
    • Appendix
  • <CSEntry>
  • <CSBatch>
  • <CSTab>
  • <DataManager>
  • <TextView>
  • <TblView>
  • <CSFreq>
  • <CSDeploy>
  • <CSPack>
  • <CSDiff>
  • <CSConcat>
  • <Excel2CSPro>
  • <CSExport>
  • <CSIndex>
  • <CSReFmt>
  • <CSSort>
  • <ParadataConcat>
  • <ParadataViewer>
  • <CSCode>
  • <CSDocument>
  • <CSView>
  • <CSWeb>

File.writeLines Action

Format
CS.File.writeLines(path := ..., lines := ...
               
ʃ, encoding := ...ʅ
               
ʃ, newline := ...ʅ)
ArgumentDescriptionTypes / Required
pathThe path of the file to write.string
required
linesThe array of strings to write.array
required
encodingThe encoding to use while writing the text.
The default value is "UTF-8".
string
not required
newlineThe control characters used to write line endings.
The default value is "LF".
string
not required
Description
The File.writeLines action writes an array of strings, lines, as lines to a text file specified as path. After writing each line, the action writes a newline character: '\n'. If a file already exists at path, it is overwritten with the new contents.
The encoding argument allows you to specify how the contents of the file are encoded to text. Options include:
  • "ANSI": The contents are encoded as part of the Windows code page.
  • "UTF-8": The contents are encoded as UTF-8 and written without a byte order mark (BOM).
  • "UTF-8-BOM": The contents are encoded as UTF-8 and written with a three-byte BOM.
The newline argument allows you to specify the control characters used when writing newlines. Options include:
  • "LF": Newlines are written using only the line feed character: "\n".
  • "CRLF": Newlines are written using a combination of a carriage return and a line feed character: "\r\n".
Return Value
The action returns undefined.
Exceptions
The action throws an exception if any of its arguments are not specified in a valid form or if the file cannot be created or fully written.
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.readLines Action, File.writeBytes Action, File.writeText Action, File.write Function