开发者

How to get a list of files in a directory with Ruby?

Here's what I have so far:

class FileRenamer
    def RenameFiles(folder_path)
        baseDirectory = folder_path

        files = Dir.glob("*")
    end
end

puts "Renaming files..."

renamer = FileRenamer.new()
files = renam开发者_JAVA百科er.RenameFiles("/home/papuccino1/Desktop/Test")
puts files

puts "Renaming complete."

The problem is that it seems to be getting files on the directory where my .rb file is running from.

How can I set the directory to where I want it to be? Notice I have the baseDirectory variable there.


files = Dir.glob(File.join(folder_path, "*"))


files = Dir.glob(folder_path + '/*')


...
Dir.chdir(baseDirectory)
files = Dir.glob("*")
...

Btw, using CamelCase for variables and methods in ruby is not good (it's only for modules and classes). Use snake_case for it.


If you just want files

class FileRenamer
    def RenameFiles(folder_path)
        files = Dir.glob( File.join(".","*")).select{|x| test(?f,x)}
    end
end


    Find.find(@path) do |path|
      if FileTest.directory?(path)
        @dirs.push(path)
      else
        @files.push(path)
      end
    end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜