Programing

Ruby에서 파일 이름 및 확장자 가져 오기

lottogame 2020. 8. 13. 07:39
반응형

Ruby에서 파일 이름 및 확장자 가져 오기


YouTube에서 비디오를 다운로드하고 MP3로 변환하고 파일에 대한 디렉토리 구조를 만드는 프로그램을 작업 중입니다.

내 코드는 다음과 같습니다.

FileUtils.cd("#{$musicdir}/#{$folder}") do
  YoutubeDlhelperLibs::Downloader.get($url)
  if File.exists?('*.mp4')
    puts 'Remove unneeded tempfile'
    Dir['*.mp4'].each do |waste|
      File.delete(waste)
    end
  else
    puts 'Temporary file already deleted'
  end

  Dir['*.m4a'].each do |rip|
    rip.to_s
    rip.split
    puts 'Inside the function'
    puts rip
  end

end

첫 번째는 이미 생성 된 음악 폴더로 이동합니다. 그 안에서 나는 실행하고 get있습니다. 그 후 디렉토리에 "xyz.mp4"와 "xyz.m4a"라는 두 개의 파일이 있습니다.

두 파일을 다르게 처리 할 수 ​​있도록 확장자없이 파일 이름을 가져오고 싶습니다.

나는 배열을 사용하고 있지만, 하나의 일치에 대한 배열은 나에게 미친 소리로 들립니다.

다른 생각이 있습니까?


목적에 따라 다음 기능을 사용할 수 있습니다.

path = "/path/to/xyz.mp4"

File.basename(path)         # => "xyz.mp4"
File.extname(path)          # => ".mp4"
File.basename(path, ".mp4") # => "xyz"
File.basename(path, ".*")   # => "xyz"
File.dirname(path)          # => "/path/to"

참고 URL : https://stackoverflow.com/questions/20793180/get-file-name-and-extension-in-ruby

반응형