Can I loop through a folder that contains files like music?

Assuming I have a folder named Music which contains music files, played by the same singer, but both the song name and the singer name are mixed together; example : LuckyDubeholdon, instead of
Lucky Dube - hold on.
Can I loop through the files of this folder to customize the file names in a way such that, the singer name will be separated from the song title?

I wrote the below code to illustrate how I want this to be done:

const singerNameAndSong = 'luckydubereleaseme';  //song title is 'release me'

const song = singerNameAndSong.slice(9);
const firstName = singerNameAndSong.slice(0,5);
const secondName = singerNameAndSong.slice(5,9);

const singerName = firstName +' '+ secondName;

console.log(`${singerName} - ${song}`);
// output : lucky dube - releaseme

Please I want to know if we can loop through a folder that contains files in JavaScript?
I need your help.

You probably want to use the Node fs module to work with the file system. It would make sense to go through some lesson on node and fs module in order to have a smooth experience. Fs module is covered in the codecademy Node course but relatively briefly. Look into the fs.rename method to rename and probably fs.readdir dunction to read the files in a directory. Here is a link:

Node fs rename function (Geeksforgeeks)


Thanks for your kind concern.
Please I am checking on the resource you’re suggesting,
but I’ll notify you if I should need any assistance. Thanks a lot

1 Like