Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

fs.writeFileSync in nodeJs

//to import nodejs module fs
var fs = require('fs');

//first lets check if the file exists in the same folder 
if (fs.existsSync('mynewfile.txt')){ //to check the file in the other directories please change the path of the file from "mynewfile.txt" to the path of your file
  console.log('The file exists.'); //instead of consoling you can also remove or modify the file

}else{ //this will be exevuted if the files doesn't exist
    //this will create a file in the same directory
fs.writeFileSync('mynewfile.txt', 'Hello content!', function (err) { //to create the file in the other directories please change the path of the file from "mynewfile.txt" to the path of your file
    if (err){ throw err;  //if any error happens then this will through the detailed error
    }else{
    console.log('Saved!'); //if no error found then this code will be executed. instead of consoling you can do other stuff like calling any other function etc
    }
  });

}
Comment

fs.appendFileSync in nodeJs

var fs = require('fs');

try {
  fs.appendFileSync('message.txt', 'data to append');
  console.log('The "data to append" was appended to file!');
} catch (err) {
  /* Handle the error */
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: js foreach .childern 
Javascript :: how to find out which version of react 
Javascript :: jquery select clear options 
Javascript :: nodejs express hot reload 
Javascript :: mongoose custom object schema 
Javascript :: nodejs wait function 
Javascript :: js datetime local 
Javascript :: js get vh value 
Javascript :: vue shortcut to create component 
Javascript :: react onclick function 
Javascript :: xmlhttprequest get request 
Javascript :: json.stringify parameters 
Javascript :: js get object properties 
Javascript :: express get host url 
Javascript :: jquery remove focus 
Javascript :: angular elementref 
Javascript :: jquery on hover dynamic elements 
Javascript :: How to calc() height in react native for styling 
Javascript :: array remove index from array 
Javascript :: how to get the max value of two variables in math 
Javascript :: discord.js leave voice channel 
Javascript :: javascript negative infinity 
Javascript :: when does localstorage get cleared 
Javascript :: faker.js name 
Javascript :: js input hidden get value 
Javascript :: javascript join 
Javascript :: jquery get element innertext 
Javascript :: javascripte list length 
Javascript :: usestate remove from array 
Javascript :: javascript date method 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =