user7776690
user7776690

Reputation:

Javascript Electron remove/ignore a certain item from a list

I am getting the filenames from a folder using the following code:

getFilenameList = () => {

    let select = document.getElementById("filenameSelect");

    let options = [];

    fs.readdirSync(folderUrl).forEach(file => { options.push(file) })

    for (let i = 0; i < options.length; i++) {

      let opt = options[i];
      let el = document.createElement("option");

      el.textContent = opt.replace(/\.[^/.]+$/, "");

      el.value = opt;

      console.log(opt);

      select.appendChild(el);

    }

  }

This is returning a list like this:

.DS_Store
filename1.html
filenameother.js
filenames.txt

The problem is that its also picking up unwanted files like: .DS_Store.

How can I fix this?

Upvotes: 0

Views: 346

Answers (1)

Daniel Vest&#248;l
Daniel Vest&#248;l

Reputation: 489

Your function returns all files in the folder, including the hidden .DS_Store file. You can choose to ignore just that file

fs.readdirSync(folderUrl).forEach(file => { if(file != ".DS_Store"){options.push(file)}})

All files that start with . (usually hidden files)

fs.readdirSync(folderUrl).forEach(file => { if(file[0] != "."){options.push(file)}})

Ignore all files that don't have the a specific extension

fs.readdirSync(folderUrl).forEach(file => { if(file.includes(".html") || file.includes(".js") || file.includes(".txt")){options.push(file)}})

Upvotes: 1

Related Questions