Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

create and get all the files in a directory with nodejs

const fs = require("fs");

// create a new directory 'assets' in the root directory
const folderPath = "./assets";

fs.mkdirSync(folderPath);

// create a file named 'shakespear.txt'
let fileContent = "Now is the winter of our discontent
Made glorious summer by this sun of York;
And all the clouds that lour'd upon our house
In the deep bosom of the ocean buried";

let filePath = folderPath + '/shakespear.txt';
fs.writeFileSync(filepath, fileContent);

// Read and returns the name of all files in the directory
try{
  files = fs.readdirSync(folderPath);
}catch(error){
  console.log(error);
}
Source by www.codementor.io #
 
PREVIOUS NEXT
Tagged: #create #files #directory #nodejs
ADD COMMENT
Topic
Name
4+2 =