Node FS ENOENT error - node.js

I am trying to make this script create a directory, then write stuff in it. Simple XML dumps & stuff.
I cant get anything of FS to work. isDirectory, createDirectory, readFile, writeFile. My reading in another SO post suggested a lot of changes to fs.stat().
I already set the permissions for the folder, so it should not be having any permissions problem (Windows).
My 1st step in checking for previous files & creating new or opening them:
. . .
var logsDir = './logs/';
var logAccess = logsDir + 'stuff.log';
try { // The code says its an ENOENT
console.log(fs.statSync(logsDir))
if (!fs.statSync(logsDir).isDirectory()) {
fs.mkdirSync(logsDir);
}
} catch (e) {
console.error('Error ' + e.code);
}
try { // Another ENOENT
if (!fs.statSync(logAccess).isFile()) {
fs.writeFileSync(logAccess, 'die', 'utf8');
}
//data = fs.readFileSync(logAccess, 'utf8');
} catch (e) {
console.error('Error errors are dumb');
}
. . .

fs.stat(logsDir, function(err,stat){
try{
if(err)
{
var e = err.code;
throw e;
}
}
catch(e)
{
if(e == 'ENOENT')
{
fs.mkdir(logsDir);
}
else
{
console.log('Some other error ' + err.code);
}
}
});
fs.stat(logAccess, function(err,stat){
try{
if(err)
{
var e = err.code;
throw e;
}
}
catch(e)
{
if(e == 'ENOENT')
{
fs.writeFileSync(logAccess, 'die', 'utf8');
}
else
{
console.log('Some other error ' + err.code);
}
}
});

var fs = require('fs');
var express = require("express");
var app = express();
var logsDir = './logs/';
var logAccess = logsDir + 'stuff.log';
function directoryExits(logsDir){
try {
return fs.statSync(logsDir).isDirectory();
} catch (err) {
return false;
}
}
function fileExits(logAccess){
try {
return fs.statSync(logAccess).isFile();
} catch (err) {
return false;
}
}
var statsDir = directoryExits(logsDir);
var statsFile = fileExits(logAccess);
if(!statsDir)
{
fs.mkdirSync(logsDir);
console.log("Directory Created")
}
else
console.log('Directory already exists');
if(!statsFile)
{
fs.writeFileSync(logAccess, 'die', 'utf8');
}
else
console.log('File already exists');
app.listen(4000, function(){
console.log('Server running ');
});

Related

How to copy files to folder with same name even the files are different format ( PDF, EXCEL, PPT, WORD ) ? ( Using Node )

I am trying to copy multiple files even the same name with a different format. ( file.ppt , file.pdf, etc.. )
So far I can able to copy to a folder in any one format file ( if the files are the same name with different format )
For Example: If I have name1.ppt, name1.doc, name1.pdf. I need to copy all the files into the respective folder. But now anyone of a format file from the same name is copying to the folder. I need to copy all the files from the same name.
Please check my below code,
router.get('/segregate', async(req, res, next) => {
removeDir('C:\\IND\\Processfile') // I just resetting folder.
createDir();
try {
// console.log(res)
return res.status(200).json({ status: 200, data: 'success' });
} catch (err) {
next(err);
}
})
const removeDir = function(path) {
if (fs.existsSync(path)) {
const files = fs.readdirSync(path)
if (files.length > 0) {
files.forEach(function(filename) {
if (fs.statSync(path + "/" + filename).isDirectory()) {
removeDir(path + "/" + filename)
} else {
fs.unlinkSync(path + "/" + filename)
}
})
fs.rmdirSync(path)
} else {
fs.rmdirSync(path)
}
} else {
console.log("Directory path not found.")
}
}
async function createDir() {
console.log("Yes ssssssss");
// let newList = [];
let clearDirFlag = false;
let masterData = await MasterModel.find().lean();
// This is the folder where i need to copy the segregated files
fs.mkdir("C:\\IND\\Processfile", (err) => {
if (err) {
return;
}
clearDirFlag = true;
// console.log("clearDirFlagclearDirFlagclearDirFlagclearDirFlag ", clearDirFlag)
});
if(masterData.length!=0){
Object.keys(masterData).map( value => {
let newList = []
if (!masterData[value]['CLIENTCODE'].toString().match(/^[0-9a-z]+$/)){
// console.log("Yes ", data)
let answer = masterData[value]['CLIENTCODE'].toString().replace(/[^0-9]/g, '');
// console.log("answer ", answer);
newList.push(
{
"clientcode": answer,
"interm" : masterData[value]['INTERMEDIARYNAME']
}
)
} else{
// console.log("No ")
newList.push(
{
"clientcode": masterData[value]['CLIENTCODE'],
"interm" : masterData[value]['INTERMEDIARYNAME']
}
)
}
let filename;
let interm;
let boo = false;
let name = newList[0]['clientcode'];
var to_zip = fs.readdirSync("C:\\IND\\BGLHYD");
to_zip.forEach((name1) => {
if (name1.toString().includes(name)) {
// console.log(name1);
filename = name1;
interm = newList[0]['interm'];
boo = true;
}
});
if(boo == true) {
const pathToFile = "C:\\IND\\BGLHYD\\"+ filename;
// console.log("hello123", pathToFile);
fs.mkdir("C:\\IND\\Processfile\\" + interm, (err) => {
if (err) {
return;
}
});
const pathToNewDestination = "C:\\IND\\Processfile\\" + interm +"\\"+ filename;
let readStream = fs.createReadStream(pathToFile);
readStream.once('error', (err) => {
console.log(err);
});
readStream.once('end', () => {
console.log('done copying');
});
readStream.pipe(fs.createWriteStream(pathToNewDestination));
}
})
}
}
Here is the place i doing the copy functionality
if(boo == true) {
const pathToFile = "C:\\IND\\BGLHYD\\"+ filename;
// console.log("hello123", pathToFile);
fs.mkdir("C:\\IND\\Processfile\\" + interm, (err) => {
if (err) {
return;
}
});
const pathToNewDestination = "C:\\IND\\Processfile\\" + interm +"\\"+ filename;
let readStream = fs.createReadStream(pathToFile);
readStream.once('error', (err) => {
console.log(err);
});
readStream.once('end', () => {
console.log('done copying');
});
readStream.pipe(fs.createWriteStream(pathToNewDestination));
}
Please help me to solve this issue.

Node.js writing data to file throws an error

Got this error when I run my program
TypeError: Callback is not a function
// Update data from a new file
lib.update = function(dir,file,callback){
//Open the file for writing
fs.open(lib.baseDir+dir+'/'+'.json','r+',function(err,fileDescriptor){
if(!err && fileDescriptor){
var stringData= JSON.stringify(data);
//Truncate the file before writing
fs.truncate(fileDescriptor,function(err){
if(!err){
//Write to the file and close it
fs.writeFile(fileDescriptor,stringData,function(err){
if(!err){
fs.close(fileDescriptor,function(err){
if(!err){
callback(false);
}else {
callback('Error closing existing file!')
}
})
}else {
callback('Error writing to existing file')
}
});
}else {
callback('Error Truncating file')
}
});
}else {
callback('Could not open file for updating! May not exist yet')
}
});
}
I propose you to refactor your code to be a little more clear about what you are doing. Something like this, it will help you find where errors come. Using promises better than use a lot of callbacks.
Just my contribution.
lib.update = function(dir, file, data) {
const updateP = new CustomPromise();
openFile()
.then(truncateFile)
.then(writeFile.bind(this, JSON.stringify(data)))
.then(closeFile)
.then(updateP.resolve) // at this point all the functions was successful
.catch((errorType) =>
errorType !== ERROR_TYPE.ERROR_CLOSING_FILE // something fail try to close the file
? closeFile().finally(() => updateP.reject(errorType))
: updateP.reject(errorType),
);
return updateP.promise;
};
// Constants
const ERROR_TYPE = Object.freeze({
ERROR_OPEN_FILE: 'error-open-file',
ERROR_TRUNCATING_FILE: 'error-truncating-file',
ERROR_WRITING_FILE: 'error-writing-file',
ERROR_CLOSING_FILE: 'error-closing-file',
});
// Private functions
function CustomPromise() {
this.promise = new Promise(function(resolve, reject) {
this.resolve = resolve;
this.reject = reject;
});
}
function openFile() {
const openP = new CustomPromise();
fs.open(lib.baseDir + dir + '/' + '.json', 'r+', function(err, fileDescriptor) {
(err || !fileDescriptor) && openP.reject(ERROR_TYPE.ERROR_OPEN_FILE);
openP.resolve(fileDescriptor);
});
return openP.promise;
}
function truncateFile(fileDescriptor) {
const truncateP = new CustomPromise();
fs.truncate(fileDescriptor, function(err) {
err && truncateP.reject(ERROR_TYPE.ERROR_TRUNCATING_FILE);
truncateP.resolve(fileDescriptor);
});
return truncateP.promise;
}
function writeFile(data, fileDescriptor) {
const writeFileP = new CustomPromise();
fs.writeFile(fileDescriptor, data, function(err) {
err && writeFileP.reject(ERROR_TYPE.ERROR_WRITING_FILE);
writeFileP.resolve(fileDescriptor);
});
return writeFileP.promise;
}
function closeFile(fileDescriptor) {
const closeP = new CustomPromise();
fs.close(fileDescriptor, function(err) {
err && closeP.reject(ERROR_TYPE.ERROR_CLOSING_FILE);
closeP.resolve();
});
return closeP.promise;
}

Node js Promises with recursive function

I want to read the all (text) files from a specific directory and it's all subdirecoty recursively.. I am able to read the file and append the result to a global variable. but i want to access the variable at the end of all operation. I am trying with promises but i am unable to access it. please help
var file_path = `C:\\Users\\HP\\Desktop\\test_folder`;
const fs = require('fs');
var final_array = [];
let getFolderTree = function(file_path) {
return new Promise(function(resolve, reject) {
fs.readdir(file_path, function(err, folders) {
if (err) {
console.log("error reading folder :: " + err);
} else {
if (folders.length !== 0) {
for (let i = 0; i < folders.length; i++) {
if (folders[i].endsWith("txt")) {
let text_file_path = file_path + `\\` + folders[i];
fs.readFile(text_file_path, function(error_read, data) {
if (error_read) {
console.log("error reading " + error_read);
} else {
return resolve(final_array.push(data));// want to access final_array at the end of all operations
}
});
} else {
let current_path = file_path + `\\` + folders[i];
getFolderTree(current_path);
}
}
}
}
});
});
}
getFolderTree(file_path).then(function() {
console.log(final_array); // this is not working
});
I think i have found the solution but I am still confused about how it works.
I took reference from another code and able to figure out some how.
var fs = require('fs');
var path = require('path');
let root_path = "C:\\Users\\HP\\Desktop\\test_folder";
function getAllDirectoriesPath(current_path) {
var results = [];
return new Promise(function (resolve, reject) {
fs.readdir(current_path, function (erro, sub_dirs) {
if (erro) {
console.log(error);
} else {
let no_of_subdir = sub_dirs.length;
if (!no_of_subdir) {
return resolve(results);
} else {
sub_dirs.forEach(function (dir) {
dir = path.resolve(current_path, dir);
fs.stat(dir, function (err, stat) {
if (stat && stat.isDirectory()) {
getAllDirectoriesPath(dir).then(function (res) {
results = results.concat(res);
if (!--no_of_subdir) {
resolve(results);
}
});
} else {
fs.readFile(dir, function (err, data) {
results.push(data.toString());
if (!--no_of_subdir) {
resolve(results);
}
});
}
});
});
}
}
});
});
}
getAllDirectoriesPath(root_path).then(function (results) {
console.log(results);
});

Why node js express route never stops running

I have made a route in express and it works below is the code snippet:
In app.js I have added:
var generateOrders = require('./routes/generateOrders');
app.use('/generate-orders', generateOrders);
In generateOrders.js:
var express = require('express');
var router = express.Router();
var generateOrdersServices = require('../services/generateOrders')
router.get('/get-orders', function (req, res) {
generateOrdersServices.getOrders(function (errGenerateOrders, responseGenerateOrders) {
if(errGenerateOrders){
console.log(errGenerateOrders);
res.render(errGenerateOrders);
return;
}
else{
console.log(responseGenerateOrders);
res.render(responseGenerateOrders);
return;
}
})
})
Now when I hit http://localhost:4000/generate-orders/get-orders It does not stop ever.
My code generate a log file, where I add all the input and when I hit this route it keeps on adding output to the log file again and again.Also in terminal everything keeps on printing again and again.
below is my code which I am using to generate a log file for my console output:
/*************************Log Console **********************/
var log_file = fs.createWriteStream(base_log_path+'/getorders/getorders'+year+'-'+month+'-'+day+'-'+hours+'-'+minutes+'-'+seconds+'.log', {flags : 'w'});
var log_stdout = process.stdout;
console.log = function(d) { //
log_file.write(util.format(d) + '\n');
log_stdout.write(util.format(d) + '\n');
};
/*************************Log Console **********************/
Please suggest some solution.
Update
generateOrdersServices.getOrders:
var getOrders = function(callback){
exportedTradeIds(storename,function(err_processedList, tradeIds){
if(err_processedList){
console.log("Error in getting Processed trade ids from exported table error is: "+err);
return callback('Error in getting Processed trade ids from exported table error is: '+err);
}
else if(tradeIds == 0)
{
console.log('No exported trade Ids');
return callback('No exported trade Ids');
}
else
{
jdp_tb_trade.getJstTrades('WAIT_BUYER_CONFIRM_GOODS', 900, 30, 'tmall_i18n', tradeIds, function(err_jdp, jdp_tb_trades){
if(err_jdp)
{
callback('Error in jdp_trade_response. Error is: '+err_jdp);
return;
}
if(!jdp_tb_trades)
{
callback(null, 'No result found from remote database.');
return;
}
else
{
for ( i in jdp_tb_trades) {
if(i == 1)
{
processConsign(jdp_tb_trades[i].tid,
function (err_process_consign, lpnumber) {
if (err_process_consign) {
console.log("Some error occurred in processConsign. Error is:" + err_process_consign);
var jdp_response_json_parsed = JSON.parse(jdp_tb_trades[i].jdp_response);
//Check if generate XML is enabled from admin end.
configuration.getOneByKey('generateXml', function (err_configuration, result_configuration) {
if (err_configuration) {
console.log('[generateXml]: generate xml enabled/disabled - No response.')
}
else
{
if (result_configuration.value == 'true') {
console.log('[generateXml]: generate xml enabled.')
generateXml(jdp_tb_trades[i].tid, jdp_response_json_parsed, lpnumber, function (err_generate_xml, success_generate_xml) {
if (err_generate_xml) {
return callback(err_generate_xml);
}
else {
return callback(null, success_generate_xml);
}
});
}
else{
console.log('[generateXml]: generate xml disabled.');
}
}
});
}
else
{
console.log("response from processConsign:Lpnumber" + lpnumber);
var jdp_response_json_parsed = JSON.parse(jdp_tb_trades[i].jdp_response);
//Check if generate XML is enabled.
configuration.getOneByKey(
'generateXml',
function (err_configuration, result_configuration) {
if (err_configuration) {
console.log('[generateXml]: generate xml enabled/disabled - No response.')
}
else {
if (result_configuration.value == 'true')
{
console.log('[generateXml]: generate xml enabled.')
generateXml(jdp_tb_trades[i].tid, jdp_response_json_parsed, lpnumber, function (err_generate_xml, success_generate_xml) {
if (err_generate_xml)
{
return callback(err_generate_xml);
}
else
{
return callback(null, success_generate_xml);
}
});
}
else
{
console.log('[generateXml]: generate xml disabled.');
}
}
});
}
});
}
}
}
});
}
})
};
module.exports = {
getOrders: function (callback) {
getOrders(function(err_getOrders, getOrdersResponse){
if(err_getOrders){
console.log("generate Order process error:"+err_getOrders);
return callback(err_getOrders);
}
else{
console.log("generate Order process success:"+getOrdersResponse);
return callback(null, getOrdersResponse);
}
});
},
}
I have omitted some code from generateOrdersServices.getOrders as it is not allowed to post that much code here.

Coffeescript --watch switch not working with node 0.4.7

The --watch directive to the coffeescript compiler doesn't work with node.js 0.4.7. How to fix?
(Node 0.4.7 is currently required for deployment on Heroku.)
There is no fs.watch method in node.js 0.4.7. Replace the watch function at line 198 in command.js with the following:
watch = function(source, base) {
return fs.stat(source, function(err, prevStats) {
if (err) throw err;
return fs.watchFile(source, function(curr, prev) {
return fs.stat(source, function(err, stats) {
if (err) throw err;
if (stats.size === prevStats.size && stats.mtime.getTime() === prevStats.mtime.getTime()) {
return;
}
prevStats = stats;
return fs.readFile(source, function(err, code) {
if (err) throw err;
return compileScript(source, code.toString(), base);
});
});
});
});
};
If you can't upgrade to Node 0.6, try Jitter. It's a single-purpose command line utility that does what coffee -cw did before CoffeeScript 1.1.3. Plus it has a few little extras, like Growl notifications when compilation fails.
Here's a watchscript I wrote after having same experience with coffeescript -w option not working. It finds and watches .coffee, .less, .jade files for change and triggers their corresponding compilers in response. Generates colored output for easy detection of when the compilation failed.
/*
* Tested on node 0.6, Ubuntu
*/
#!/usr/bin/node
COLOR_GREEN="\033[01;32m"
COLOR_RED="\033[01;31m"
COLOR_YELLOW="\033[01;33m"
COLOR_BLUE="\033[01;34m"
COLOR_NORMAL="\033[00m"
var fs = require('fs');
var coffeescript, jade, less;
try {
coffeescript = require('/usr/lib/node_modules/coffee-script'); // after npm -g install coffee-script
} catch(e) {
console.log(COLOR_RED,String(e),COLOR_NORMAL);
}
try {
jade = require('/usr/lib/node_modules/jade'); // after npm -g install jade
} catch(e) {
console.log(COLOR_RED,String(e),COLOR_NORMAL);
}
try {
less = require('/usr/lib/node_modules/less'); // after npm -g install less
} catch(e) {
console.log(COLOR_RED,String(e),COLOR_NORMAL);
}
console.log(
'Watch Support',
(coffeescript ? COLOR_GREEN : COLOR_RED),' coffeescript ',COLOR_NORMAL,
(jade ? COLOR_GREEN : COLOR_RED),' jade ',COLOR_NORMAL,
(less ? COLOR_GREEN : COLOR_RED),' less ',COLOR_NORMAL
);
var compile = function (fname) {
var source = fs.readFileSync(fname).toString();
try {
if(/\.jade$/.test(fname)) {
var html = jade.compile(source,{pretty:true})();
var htmlfname = fname.replace(/\.jade$/,'.html');
fs.writeFileSync(htmlfname, html);
console.log(COLOR_GREEN,'Success ',fname,new Date(),COLOR_NORMAL)
} else if(/\.coffee$/.test(fname)) {
var js = coffeescript.compile(source);
var jsfname = fname.replace(/\.coffee$/,'.js');
fs.writeFileSync(jsfname, js);
console.log(COLOR_GREEN,'Success ',fname,new Date(),COLOR_NORMAL)
} else if(/\.less$/.test(fname)) {
var lessparser = new(less.Parser);
var cssfname = fname.replace(/\.less$/,'.css');
lessparser.parse(source, function (e, tree) {
if(e) {
console.log(COLOR_RED,'Failed ',fname,String(e),COLOR_NORMAL);
} else {
fs.writeFileSync(cssfname, tree.toCSS());
console.log(COLOR_GREEN,'Success ',fname,new Date(),COLOR_NORMAL)
}
});
}
} catch(e) {
console.log(COLOR_RED,'Failed ',fname,String(e),COLOR_NORMAL)
}
}
var walk = function(dir, done) {
var results = [];
fs.readdir(dir, function(err, list) {
if (err) return done(err);
var pending = list.length;
if (!pending) return done(null, results);
list.forEach(function(file) {
file = dir + '/' + file;
fs.stat(file, function(err, stat) {
if (stat && stat.isDirectory()) {
walk(file, function(err, res) {
results = results.concat(res);
if (!--pending) done(null, results);
});
} else {
results.push(file);
if (!--pending) done(null, results);
}
});
});
});
};
function main() {
if(process.argv.length > 2) {
dirName = process.argv[2];
} else {
dirName = '.';
}
stats = fs.statSync(dirName);
if(stats.isDirectory()) {
walk(dirName,function (err, files) {
var watching = 0;
files.forEach(function (fname) {
if(/\.coffee$|\.less$|\.jade$/.test(fname)) {
fs.watchFile(fname,{persistent:true,interval:500},
function (cur,prev) {
if(cur.mtime.getTime() != prev.mtime.getTime()) {
compile(fname);
}
}
);
watching++;
}
});
if(watching) {
console.log('Watching '+watching+' file(s) in '+
COLOR_YELLOW+dirName+COLOR_NORMAL);
} else {
console.log('No coffee/jade/less files found to watch');
}
});
} else {
// It's a file
var fname = dirName;
if(/\.coffee$|\.less$|\.jade$/.test(fname)) {
fs.watchFile(fname,{persistent:true,interval:500},
function (cur,prev) {
if(cur.mtime.getTime() != prev.mtime.getTime()) {
compile(fname);
}
}
);
console.log('Watching '+COLOR_YELLOW+fname+COLOR_NORMAL);
} else {
console.log('No coffee/jade/less files found to watch');
}
}
}
main();
If you prefer gist: https://gist.github.com/3083080

Resources