0

I have a folder with this structure:

folder
  |--file1.csv
  |--file2.csv
  |--file3.csv
  |--file4.csv

Is there a way to programmatically write a script to go into a folder and open one file at a time to do something and then open the next until all files in that folder has been inspected?

Val
  • 1,140
  • 4
  • 21
  • 35

1 Answers1

0

You can use node fs.*Sync() methods which will block until the operation is done.

The readdirSync function will give you a list of files (and subdirectories, which are also files) in a directory and statSync will tell you, if it's a directory or a file.

You should be aware that your script can do nothing else while doing the blocking operations though. So do not export and use that code as a function in a bigger application.

If you want to reuse it, you should write your code in an asynchronous fashion (i.e. use callbacks and handle your state / values accordingly). Or use one of the npm modules that implement the necessary functionality ("walk" seems to be a good search keyword for your use-case).

Narigo
  • 2,719
  • 3
  • 18
  • 29
  • using Sync methods can be a really bad idea because it doesnt just block the current request on the server, it blocks all the request to the server – Gurbakhshish Singh Mar 20 '17 at 14:59