fs

Node.js check exist file

纵饮孤独 提交于 2019-11-28 03:10:20
How do i check the existence of a file ? In the documentation for the module fs there's a description of the method fs.exists(path, callback) . But, as I understand, it checks for the existence of only directories. And I need to check the file ! How can this be done? Fox Why not just try opening the file ? fs.open('YourFile', 'a', function (err, fd) { ... }) anyway after a minute search try this : var path = require('path'); path.exists('foo.txt', function(exists) { if (exists) { // do something } }); // or if (path.existsSync('foo.txt')) { // do something } For Node.js v0.12.x and higher Both

Get all files recursively in directories NodejS

此生再无相见时 提交于 2019-11-28 03:01:36
问题 I have a little problem with my function. I would like to get all files in many directories. Currently, I can retrieve the files in the file passed in parameters. I would like to retrieve the html files of each folder in the folder passed as a parameter. I will explain if I put in parameter "test" I retrieve the files in "test" but I would like to retrieve "test / 1 / *. Html", "test / 2 / . / .html ": var srcpath2 = path.join('.', 'diapo', result); function getDirectories(srcpath2) { return

「学习笔记」杂项算法大礼包

纵饮孤独 提交于 2019-11-28 01:15:22
平面最近点对。 #include <algorithm> #include <cstdio> #include <vector> #include <cmath> using namespace std; #define fs first #define sc second typedef double db; typedef pair<db, db> pa; const int N = 2e5 + 10; const db EPS = 1e-10; int cmp1(pa a, pa b) { return a.fs - b.fs < -EPS; } int cmp2(pa a, pa b) { return a.sc - b.sc < -EPS; } int n, m; pa a[N], arr[N]; db p(db x) { return x * x; } db d(pa x, pa y) { return sqrt(p(x.fs - y.fs) + p(x.sc - y.sc)); } db divide(int l, int r) { if(r - l <= 2) { db ans = 3e18; for(int i = l; i < r; i ++) for(int j = i + 1; j <= r; j ++) ans = min(ans, d(a[i], a[j

chaining `fs.readdir` with a `.then` to return an array

a 夏天 提交于 2019-11-27 18:55:16
问题 I am trying to create an array of specific files in a directory; which will go through a few test cases to make sure it fits a given criteria. I'm using the fs.readdir method, but it doesn't return a promise meaning I cannot push to an array . My idea was to populate an array ( arr ) with the files I actually want to output and then do something with that array. But because readdir is asynchronous and I can't chain a .then() onto it, my plans are quashed. I've also tried the same thing with

Find absolute base path of the project directory

Deadly 提交于 2019-11-27 18:31:40
Until now we could get the absolute path of a file to open later as readStream with this code snippet: var base = path.resolve('.'); var file = base + '/data/test.csv'; fs.createReadStream(file) Since Meteor 0.6.5 the base path is pointing to .meteor/local/build/programs/... There is also the Assets API, which but can not give us back a path but only the read document. We but need a stream to process some bigger data files? Another way to find your project's root directory now is this: var base = process.env.PWD Note that this is not the same as process.cwd() . Instead it is the directory

Asynchronously reading and caching multiple files in nodejs

别等时光非礼了梦想. 提交于 2019-11-27 14:14:18
问题 I have an array which keeps URL of several files. For example: var files = ['1.html', '2.html', '3.html']; I need to read them asynchronously and save them in an object named cache (cache = {}). To do this I used the code: for(var i = 0; i < files.length; i++){ require('fs').readFile(files[i], 'utf8', function (error,data) { cache[files[i]]=data; }); } In the end I have the result: cache = { undefined : 'File 3 content' } I do understand that the "readFile" acts after the loop is ended and it

How to create full path with node's fs.mkdirSync?

此生再无相见时 提交于 2019-11-27 11:16:06
I'm trying to create a full path if it doesn't exist. The code looks like this: var fs = require('fs'); if (!fs.existsSync(newDest)) fs.mkdirSync(newDest); This code works great as long as there is only one subdirectory (a newDest like 'dir1') however when there is a directory path like ('dir1/dir2') it fails with Error: ENOENT, no such file or directory I'd like to be able to create the full path with as few lines of code as necessary. I read there is a recursive option on fs and tried it like this var fs = require('fs'); if (!fs.existsSync(newDest)) fs.mkdirSync(newDest,'0777', true); I feel

Node.js check if path is file or directory

一个人想着一个人 提交于 2019-11-27 08:59:26
问题 I can't seem to get any search results that explain how to do this. All I want to do is be able to know if a given path is a file or a directory (folder). 回答1: fs.lstatSync(path_string).isDirectory() should tell you. From the docs: Objects returned from fs.stat() and fs.lstat() are of this type. stats.isFile() stats.isDirectory() stats.isBlockDevice() stats.isCharacterDevice() stats.isSymbolicLink() (only valid with fs.lstat()) stats.isFIFO() stats.isSocket() NOTE: The above solution will

wav文件与byte互转 C#

牧云@^-^@ 提交于 2019-11-27 02:32:11
//wav转byte public void WavToByte() { Byte[] bs; FileStream fs = new FileStream(@"C:\1.wav", FileMode.Open, FileAccess.Read); bs = new Byte[fs.Length]; fs.Read(bs, 0, (int)fs.Length); fs.Close(); } //byte转wav public void ByteToWav(Byte[] bs) { FileStream fs = new FileStream(@"C:\2.wav", FileMode.OpenOrCreate, FileAccess.Write); fs.Write(bs, 0, bs.Length); fs.Close(); } 来源: https://www.cnblogs.com/macT/p/11338514.html

How to close a readable stream (before end)?

纵然是瞬间 提交于 2019-11-27 00:59:46
How to close a readable stream in Node.js? var input = fs.createReadStream('lines.txt'); input.on('data', function(data) { // after closing the stream, this will not // be called again if (gotFirstLine) { // close this stream and continue the // instructions from this if console.log("Closed."); } }); This would be better than: input.on('data', function(data) { if (isEnded) { return; } if (gotFirstLine) { isEnded = true; console.log("Closed."); } }); But this would not stop the reading process... Nitzan Shaked Invoke input.close() . It's not in the docs, but https://github.com/joyent/node/blob