Wednesday, 12 April 2017

How to get specific files using fs.readFileSync()

I am trying to get only the audio files from a folder that has different file types. Like images, videos etc..

I made a function to filter the results but it only returns me the first audio file I don't know what I'm doing wrong.

coffeescript

@audioFiles = @getAudioFiles()

getAudioFiles: ->

  allFiles = fs.readdirSync(@pathtoMusic.toString())
  musicFiles = Array
  file = 0
  cont = 0

  #-- I also tried with a for loop --#
  #for file of allFiles
  #  fileName = allFiles[file]

  while(file < allFiles.length)
    fileName = allFiles[file++]
    fileExtension = fileName.split('.').pop();

    if(fileExtension is "mp3") or (fileExtension is "MP3")
      musicFiles[cont++] = fileName
    if(fileExtension is "wav") or (fileExtension is "WAV")
      musicFiles[cont++] = fileName
    if(fileExtension is "3gp") or (fileExtension is "3GP")
      musicFiles[cont++] = fileName
    if(fileExtension is "m4a") or (fileExtension is "M4A")
      musicFiles[cont++] = fileName
    if(fileExtension is "webm") or (fileExtension is "WEBM")
      musicFiles[cont++] = fileName

  return musicFiles

Is there a way to do it directly?, without needing to create a function or a loop. It would be fantastic. Something like this.

fileExtensions = ["mp3","m4a","3gp","wav","webm","etc"...]
@audioFiles = fs.readdirSync(pathToMusic,"fileExtencions")



via Jesse R. Jose

No comments:

Post a Comment