JavaScript Retrieve multiple properties from multiple files simultaneously

Currently, I have a folder containing 50 mp3 files. My goal is to create an object for each file that includes its name and source.

However, the challenge lies in figuring out how to select and retrieve properties from all of the files. Is this even possible? I am aware that you can extract information from a text file using JS.

A possible approach could be:

for (var i = 0; i < musicFolder.length; i++) {
 var object = new Object (
 musicFolder[i].title,
 musicFolder[i].path/src
 );
 objectArray.push(object);
}

I might need to somehow select an entire folder, but I am unsure how to achieve this in JS.

Answer №1

If you are using Node.js and only want to extract the name and path of a file, you can achieve it like this:

const fs = require('fs');
let filePath = 'enter your file path here';
const fileList = [];
fs.readdir(filePath, (error, files) => {
  for (let i=0; i<files.length; i++) {
    fileList.push({
      fileName: files[i],
      filePath: `${filePath}/${files[i]}`
    });


  }
  console.log(fileList)
});

This code snippet will loop through the specified folder, display all the files it contains, and store each file's name and path in an object before adding it to an array.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Here are the steps to pass the selected dropdown value to ng-repeat for ordering:

I have a dropdown box with options for filtering data in an ng-repeat by different criteria. How can I pass the selected value from the dropdown to the ng-repeat orderby? Here is my dropdown: <select name='filter_range' id='filter_range ...

Tips for validating multiple forms on a single page without altering the JavaScript validation structure

My JSP page currently consists of two forms: <body> <form id="1"></form> <form id="2"></form> </body> Only one form is visible at a time when the page is viewed. A JavaScript validation file is used to check the ...

Leveraging nodemailer for automated email delivery

Hey there! I'm looking for some advice on how to set up scheduling with emailing using nodemailer. Ideally, I'd love to be able to schedule emails to send every day at 9am within a web app using nodemailer. However, I'm not sure where to st ...

Creating a script within a newly generated division element

function addNewChild() { if (childCount <= 2) { childCount++; var newDiv = document.createElement('div'); newDiv.innerHTML = '<br> Prescription '+childCount+':<br><input id="uploadFile" class="disabl ...

What could be causing the "Undefned Jquery-ajax" error to pop up

I am struggling with the following code. My goal is to populate values into a dropdown <select> box, but I keep receiving an error of undefined. I have tried switching between Object.keys and Object.values, however, I am still facing the same issue. ...

Transform a JavaScript object array into a collection of individual values

Given a list of objects structured like this, I am looking to transform it into a list of values based on their corresponding IDs. Original = [ {id: 1, value: 12.2494, time: "00:00:00.14"}, {id: 1, value: 4.5141, time: "00:00:01.138"}, {id: 1, ...

Tips for effectively managing errors in Next.js getStaticProps

Currently, I am immersed in the development of my inaugural Next.JS application (Next and Strapi). All functionalities are operational, but I am interested in discovering the optimal approach to integrate error handling within getStaticProps. I have exper ...

"Exploring the Wonders of Regular Expressions in JavaScript: Embracing the Truth and All

Hey there! I've been working on a JavaScript script to test password field validation. As of now, I have successfully made the script display an alert when the requirements are not met. However, I am now facing an issue regarding what action to take o ...

Tips on importing a .json document from an external source

I am attempting to retrieve a json file from a specific website: "" However, this website does not support JSONP callback function, and I have encountered multiple errors while trying to access the data. I have experimented with making a JSON call but kee ...

Is there a way to enable drag and drop functionality on a dynamically created table using AJAX?

I have been successfully using the TableDnD plugin for drag and drop functionality on table rows. However, I am now facing an issue with dynamically generated tables via AJAX. The code doesn't seem to work as expected when it comes to drag and droppin ...

Is there a faster way to sort this data with Javascript (or JQuery for extra speed)?

Recently, I've encountered a challenge with sorting an HTML table using JavaScript. The table can potentially contain over a thousand rows. This is what my current setup looks like in terms of HTML: <div id="mycollection" class="table"> &l ...

The act of delegating ngshow and nghide functionality

check out this demo: http://plnkr.co/edit/EWOvKsTEutiveMEAGTKf?p=preview <li ng-show="showList" ng-repeat="task in tasks" ng-hide="task.checked=1"> {{task.name}} </li> Is it possible to use ng-show and ng-hide together on the same element? ...

Customizing hyperlink styles with JavaScript on click

Hey there! I'm experimenting with something new. I've managed to change the background color of each link after it's clicked, but now I'm facing a challenge: How can I revert the original style when another link is clicked? Here's ...

Having trouble loading the linked CSS file in the Jade template

My directory structure is organized as follows: --votingApp app.js node_modules public css mystyle.css views test.jade mixins.jade In the file mixins.jade, I have created some general purpose blocks like 'bo ...

Displaying a div when an ng-repeat directive is devoid of content, incorporating filters in AngularJS

Currently, I am in need of a solution to display a specific div when my ng-repeat list is empty. The scenario involves a list containing various types of ice cream (with search filter functionality). What I aim to achieve is showing a designated div when t ...

Transforming a JavaScript variable into a PHP function call

Looking to transfer a Javascript variable into PHP by replacing the const 4 with var i var ntime = '<?php echo count($czasAr);?>'; for (var i = 0; i < ntime; i++) { ...

React/Next Component Changes State and Clears it in useEffect

Currently, I am faced with an issue while attempting to fetch data from an API. The problem arises during the rendering process since the useEffect function is being called multiple times. Surprisingly, everything works perfectly fine during the initial pa ...

The React Bit Dev module is showing a 404 error

Attempting to incorporate the semantic-ui-react table reusable component from the Bit.dev community into my application. The link I am using is: To add this component to your application, use: npm i @bit/semantic-org.semantic-ui-react.table However, when ...

When the script is placed at the end of the file, document.getElementById() is still returning null

I need assistance with running a JavaScript function that is located at the end of my .aspx file. This is the div tag in question: <div id="div_NRContainer" oninit="div_NRContainer_Init" class="panelContainer" runat="server"> Following this, with ...

Add a CSS file to the browser-sync with proxy functionality

Currently, I have a script that I utilize to proxy a live website in order to make CSS modifications. Instead of replacing the online CSS file with a local one using a rewrite, I am exploring the possibility of injecting an entirely new file below the exis ...