Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// create a dir with a bunch of files in it, then read them.
var fs = require("fs")
var path = require("path")
var util = require("util")
var fileCount = 1024
var x = "x".charCodeAt(0)
var testFolder = path.resolve("test-many-files-"+process.pid)
var eol = process.platform === "win32" ? "\r\n" : "\n"
console.error("many file test")
console.error(testFolder)
function numName (num) {
return "test-file-" +
new Buffer([Math.floor(num / 256), num % 256]).toString("hex") +
"-" + num +
// make the length of files variable
new Array((num % 10) + 1).join(".x")
}
console.error("making files")
console.error(testFolder)
fs.mkdirSync(testFolder, 0777)
var test = []
for (var i = 0; i < fileCount; i ++) {
test[i] = numName(i)
var data = new Buffer(i)
for (var d = 0; d < i; d ++) {
data[d] = x
}
fs.writeFileSync(path.resolve(testFolder, test[i]), data)
}
test.sort(alphasort)
console.error("test files written, reading directory")
fs.readdir(testFolder, function (er, files) {
if (er) throw er
files = files.filter(function (f) {
return f !== "." && f !== ".."
}).sort(alphasort)
fs.writeFileSync(testFolder + ".txt",
util.inspect(files).split("\n").join(eol))
if (files.length !== fileCount) {
throw new Error("wrong file count")
}
for (var i = 0; i < files.length; i ++) {
if (files[i] !== test[i]) throw new Error("invalid data at file #"+i)
}
console.log("ok")
})
// sort alphabetically.
// fs.readdir results are arbitrarily sorted, so we need to put them in
// order to compare them properly.
function alphasort (a, b) {
return a === b ? 0 : a > b ? 1 : -1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.