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 :: convert days in years js 
Javascript :: sort array of strings 
Javascript :: Object.values returns 
Javascript :: javascript multidimensional array 
Javascript :: how to change input value in javascript using class 
Javascript :: fetch method post handing the post add on php 
Javascript :: axios get data from json file 
Javascript :: cheat sheet javascript 
Javascript :: lodash find array of strings 
Javascript :: split string with last character and return array in javascript 
Javascript :: remove the .cache folder from angular 13 project 
Javascript :: vuex store watch 
Javascript :: como eliminar un elemento del dom con javascript 
Javascript :: upload image to server next js 
Javascript :: setProps jest 
Javascript :: mysql JSON_SEARCH LIKE 
Javascript :: Mongoose and multiple database in single node.js project 
Javascript :: ejs express layouts 
Javascript :: how to convert a number to a string in javascript 
Javascript :: kick members node js 
Javascript :: random color js 
Javascript :: how to check for enter keyPress in react native 
Javascript :: js math random 
Javascript :: manually set jquery text box 
Javascript :: string methods javascript 
Javascript :: add parameter submit form javascript 
Javascript :: multiple image upload in react js 
Javascript :: how to set css in hbs in express 
Javascript :: numero random en js 
Javascript :: JQuery datatable with ajax, post API call hook 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =