fs

Special characters in node.js readdir()

邮差的信 提交于 2019-12-11 11:34:34
问题 I'm running this piece of code in node.js in order to see the files in a directory an to see the stats for them: var getFiles = function (dir, done) { fs.readdir(dir, function (err, files) { if (err) return done(err); var pending = files.length; files.forEach(function (file) { fullPath = dir + "/" + file; console.log(fullPath); fs.stat(fullPath, function (err, stat) { if (err) { console.log("Stat error"); } else if (stat && stat != undefined) { console.log("Success"); } }); }); }); } My

Nodejs FS module returning no such file or dir error

久未见 提交于 2019-12-11 11:23:25
问题 Code: fs.readdir('./commands/', (err, files) => { // Do something... }); Error: ENOENT: no such file or directory, scandir './commands/' The folder ./commands/ does exist. This file is src/index.js and is trying to read the directory of src/commands/ . It wouldn't be fs.readdir('/commands' because that would be referring to the root directory of my PC (Ubuntu 18.04 LTS, Node version v8.10.0). If any futher information is required, ask and I will provide. Thank you all in advance. 回答1: try

Node - fs.stat birthtimeMs and mtimeMs

喜你入骨 提交于 2019-12-11 07:58:33
问题 I have created an empty file on filesystem (NTFS) in Windows. I called fs.stat on it and realized that birthtimeMs and mtimeMs are equal. This is good for my case, because I need to detect whether someone has not changed the file somehow in the meantime. So on Windows NTFS I can do birthdateMs === mTimeMs and if it evaluates to true, I can say that file was not touched by anyone else. The question is whether I can rely on this on Windows FAT and Mac filesystem too. I know I can try it, but it

Reading Json file with RNFS on react native

风流意气都作罢 提交于 2019-12-11 04:24:52
问题 I'am trying to access the content of a JSON file i just wrote with RNFS. I tried different ways, import, json-loader etc without success. I write the file like : var obj = { date: moment().format(), counter: 0 }; var json = JSON.stringify(obj); RNFS.writeFile(path, json, 'utf8') .then((success) => { count++; console.log('FILE WRITTEN! : '); }) .catch((err) => { console.log(err.message); }); Then i proceed to try to read it var path = RNFS.DocumentDirectoryPath + '/test.json'; const cFile =

fs.writeFile callback never gets called, same for WritableStream.write, etc

送分小仙女□ 提交于 2019-12-11 01:38:02
问题 I am writing a small text file (~500B) but, strangely, I get an empty file if I write using asynchronous methods such as fs.writeFile(..) (or WriteableStream's write/end method). This works: var scanInfo = getScanInfo( core ); // returns several lines delimited by \r\n fs.writeFileSync( filename, scanInfo, 'ascii' ); This creates empty file and the callback function never produces any output: var scanInfo = getScanInfo( core ); scanInfo.push('') ; scanInfo = scanInfo.join(DOS_CRLF); fs

Node (express.js) next() is called before end of stream

主宰稳场 提交于 2019-12-11 00:24:57
问题 I have the following middleware function var bodyParser = require('body-parser'), fs = require('fs'); module.exports = function(req, res, next) { // Add paths to this array to allow binary uploads var pathsAllowingBinaryBody = [ '/api2/information/upload', '/api2/kpi/upload', ]; if (pathsAllowingBinaryBody.indexOf(req._parsedUrl.pathname) !== -1) { var date = new Date(); req.filePath = "uploads/" + date.getTime() + "_" + date.getMilliseconds() + "_" + Math.floor(Math.random() * 1000000000) +

Determining the length of a read stream in node js

久未见 提交于 2019-12-10 21:45:15
问题 I am wondering if there is an easy way to determine the length of a readStream in node js. I am writing a worker to upload PDFs to S3 and it is puking with the error: 'Cannot determine length of [object PDFDocument]'. The pdf is a readable stream (I can pipe it to a file in memory currently and view the generated pdf). Some stuff I was thinking about doing: 1. Piping the entire pdf to memory (such as to a buffer) and then call fstat on that buffer to determine the size. Creating some method

Move all files in directory to parent with node.js

和自甴很熟 提交于 2019-12-10 12:43:43
问题 Question Is there a simple way to move all the files in a directory up to its parent directory then delete the directory? Use Case I'm doing a zip extraction and the source zip contains a root folder called archive , so when I extract I get extract_path/archive/ , but I'd like to just extract the contents of archive directly to extract_path . I thought this would be simple rename, but the following is throwing a "There is a file in the way" error message. fs.renameSync(extractPath + "/archive

fs.stat birthtime/birthtimeMs in Linux and MacOS

允我心安 提交于 2019-12-10 11:37:18
问题 One of the properties of the fs.Stats object that is returned when one calls fs.stat is birthtime and birthtimeMs , which I'm assuming is when the file was created. Stats { dev: 2114, ino: 48064969, mode: 33188, nlink: 1, uid: 85, gid: 100, rdev: 0, size: 527, blksize: 4096, blocks: 8, atimeMs: 1318289051000.1, mtimeMs: 1318289051000.1, ctimeMs: 1318289051000.1, birthtimeMs: 1318289051000.1, // this value atime: Mon, 10 Oct 2011 23:24:11 GMT, mtime: Mon, 10 Oct 2011 23:24:11 GMT, ctime: Mon,

how to determine whether the directory is empty directory with nodejs

帅比萌擦擦* 提交于 2019-12-10 01:52:39
问题 I have searched the Nodejs Doc,But don't find relative API. So I write the following code to determine whether the directory is empty directory. var fs = require('fs'); function isEmptyDir(dirnane){ try{ fs.rmdirSync(dirname) } catch(err){ return false; } fs.mkdirSync(dirname); return true } QUESTION:it look like some troublesome,there is better way to do it with nodejs? 回答1: I guess I'm wondering why you don't just list the files in the directory and see if you get any files back? fs.readdir