File Operations

Overview


file.mjs is a library for dealing with files on your local machine. There is no corresponding library on the davinci web because the browser has security restrictions that prevents access to files. The file operations library wraps Node.js file operations with promise enabled code and provides utilities for running standard ETL processes. The library can be found here: file.mjs

File Operations


file.mjs contains functions for most basic file manipulations:

  • read - reads an entire file
  • stas - returns the file statistics which typically includes things like size and last modified date
  • append - appends text to a file
  • remove - deletes the file



(async function(){

  const fl = await import('./file.mjs');
  let data = await fl.read('c:/path.txt');		
  let stats = await fl.read('c:/path.txt');	
  await fl.append('c:/path.txt', 'new line');
  await fl.remove('c:/path.txt');

  // params - path, fullpath, recurse
  //fullpath indicates whether the function should return full paths
  //recurse indicates whether to recursively search all sub folders
  let files = await fl.listDirectory('c:/directory/');
})();

					

Reading File Line by Line


file.js includes a method for reading a file line by line.


(async function(){
  const fl = import('./file.mjs');
  let process = (line) => {
    //do something with the line
  }
  await fl.readLines('C:/Testing/test/app.js', process)								
})();
					

Reading Data


The load method will use the file extension to determine how to parse the data.

let data = fl.load('data.json');
					

Listing Directories


file.js provides a couple options for listing the contents of a directory.


(async function(){
  const fl = import('./file.mjs');
  let files = fl.listDirectory('c:/somefolder/');						
})();

					


The files returned by listDirectory are returned as filenames, no additional path information.

The recurseDirectory method returns files with the complete path name. In addition, it takes a second optional paramter, which when set to true, recurses all subdirectories and returns their contents as well.


(async function(){
  const fl = import('./file.mjs');
  let files = fl.recurseDirectory('c:/somefolder/', true);						
})();
					

Contents