1

I have a folder that contain some text documents,

I want to create an array that contain the names of the text documents.

the result must be like this:

array=[firstfile.txt],[secondfile.txt],[thirdfile.txt],ecc.

i know that i have to do something like this:

fs.readFile(foldername, function (err, content) {

var array = content.toString().split("...");
}

But i don't know what should I write in the .split().

Some idea? thanks.

1

1 Answer 1

1

You should probably use fs.readdir instead of fs.readFile.

fs.readFile is for getting the contents of a particular file, and the path parameter that you pass to it should be the path to a particular file, NOT to a folder.

fs.readdir is for getting the contents a particular folder/directory. Here the path parameter should be the path to a folder

The Node docs say:

Reads the contents of a directory. The callback gets two arguments (err, files) where files is an array of the names of the files in the directory excluding '.' and '..'.

Read more here. You'll have to see if the file names include the file extensions like you seem to want.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.