• <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.writeBytes Action

Format
CS.File.writeBytes(path := ..., bytes := ...ʃ, bytesFormat := ...ʅ)
ArgumentDescriptionTypes / Required
pathThe path of the file to write.string
required
bytesThe bytes to write.string
required
bytesFormatThe format of bytes.
The default value is "autodetect".
string
not required
Description
The File.writeBytes action writes bytes to a binary file specified as path. The optional bytesFormat argument dictates how binary data is converted from string format. If a file already exists at path, it is overwritten with the new contents.
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 bytes from a file
CS.File.readBytesAsync({
    path: 
"legacy.amo",
    bytesFormat: 
"hex"
})
.then(hexBytes => {
   
// remove every other byte, represented in hexadecimal format (two characters per byte), from the file
    let removeNextByte = false;

   
for( let i = 0; i < hexBytes.length; ) {
       
if( removeNextByte ) {
            hexBytes = hexBytes.substring(0, i) + hexBytes.substr(i + 2);
            removeNextByte = 
false;
        }
       
else {
            removeNextByte = 
true;
            i += 2;
        }
    }

   
// write the bytes
    return CS.File.writeBytesAsync({
        path: 
"legacy (every-other-byte).amo",
        bytes: hexBytes,
        bytesFormat: 
"hex"
    });
})
.then(() => console.log(
"Successfully read, removed half the content, and wrote the bytes."))
.
catch(error => console.log(error));
See also: File Action Invoker Namespace, File.readBytes Action, File.writeBytes Action, File.writeLines Action