How can I list all files inside a folder with Meteor
.I have FS
collection and cfs:filesystem
installed on my app. I didn't find it in the doc.
2 Answers
Another way of doing this is by adding the shelljs npm module.
To add npm modules see: https://github.com/meteorhacks/npm
Then you just need to do something like:
var shell = Meteor.npmRequire('shelljs');
var list = shell.ls('/yourfolder');
Shelljs docs: https://github.com/arturadib/shelljs

- 182
- 1
- 15
The short answer is that FS.Collection creates a Mongo collection that you can treat like any other, i.e., you can list entries using find()
.
The long answer...
Using cfs:filesystem, you can create a mongo database that mirrors a given folder on the server, like so:
// in lib/files.js
files = new FS.Collection("my_files", {
stores: [new FS.Store.FileSystem("my_files", {"~/test"})] // creates a ~/test folder at the home directory of your server and will put files there on insert
});
You can then access this collection on the client to upload files to the server to the ~test/ directory:
files.insert(new File(['Test file contents'], 'my_test_file'));
And then you can list the files on the server like so:
files.find(); // returns [ { createdByTransform: true,
_id: 't6NoXZZdx6hmJDEQh',
original:
{ name: 'my_test_file',
updatedAt: (Date)
size: (N),
type: '' },
uploadedAt: (Date),
copies: { my_files: [Object] },
collectionName: 'my_files'
}
The copies
object appears to contain the actual names of the files created, e.g.,
files.findOne().copies
{
"my_files" : {
"name" : "testy1",
"type" : "",
"size" : 6,
"key" : "my_files-t6NoXZZdx6hmJDEQh-my_test_file", // This is the name of the file on the server at ~/test/
"updatedAt" : ISODate("2015-03-29T16:53:33Z"),
"createdAt" : ISODate("2015-03-29T16:53:33Z")
}
}
The problem with this approach is that it only tracks the changes made through the Collection; if you add something manually to the ~/test directory, it won't get mirrored into the Collection. For instance, if on the server I run something like...
mkfile 1k ~/test/my_files-aaaaaaaaaa-manually-created
Then I look for it in the collection, it won't be there:
files.findOne({"original.name": {$regex: ".*manually.*"}}) // returns undefined
If you just want a straightforward list of files on the server, you might consider just running an ls
. From https://gentlenode.com/journal/meteor-14-execute-a-unix-command/33 you can execute any arbitrary UNIX command using Node's child_process.exec()
. You can access the app root directory with process.env.PWD
(from this question). So in the end if you wanted to list all the files in your public directory, for instance, you might do something like this:
exec = Npm.require('child_process').exec;
console.log("This is the root dir:");
console.log(process.env.PWD); // running from localhost returns: /Users/me/meteor_apps/test
child = exec('ls -la ' + process.env.PWD + '/public', function(error, stdout, stderr) {
// Fill in this callback with whatever you actually want to do with the information
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if(error !== null) {
console.log('exec error: ' + error);
}
});
This will have to run on the server, so if you want the information on the client, you'll have to put it in a method. This is also pretty insecure, depending on how you structure it, so you'd want to think about how to stop people from listing all the files and folders on your server, or worse -- running arbitrary execs.
Which method you choose probably depends on what you're really trying to accomplish.

- 1
- 1

- 1,287
- 1
- 8
- 9
-
@michelk I'd see if you can use `process.env.PWD` to get the app directory. It should work on the server, but it will fail on the client. I'm not sure if you have to specify the path on the client when you set up the store, so you may be able to do something like `path: Meteor.isServer ? process.env.PWD + '/uploads' : '/uploads'` – Carson Moore Mar 30 '15 at 11:59
-
Many thanks, it works now. Except for the file's storage. My files are stored in a folder created in my user's folder, not in my project. Without mentioning a store path, files are stored in myProject//.meteor/local/cfs/files/myFS_collection_name. – michelk Mar 30 '15 at 12:05
-
EDIT: Sorry, I made a mistake.I tried it again: `path: Meteor.isServer ? process.env.PWD + '/uploads' : '/uploads'`WORKS PERFECTLY! Many thanks again! – michelk Mar 30 '15 at 12:19