Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to see file list in Rails from any folder, outer Rails.root - Windows

I have rails application, I need see files in any folder. example I have application on c:\rails_app and in controller write this code:

@files = Dir.glob("Z:/*")

and don't see file entire directory in rails console work perfectly. I have question: How to see files from non rails directory? Thanks
--- after some answer
Problem not in use Dir class. Problem that Rails see only own root directory and i can`t change dir to other disk or folder in rails controller.

like image 233
Vladimir Avatar asked Oct 18 '25 17:10

Vladimir


2 Answers

You can use Dir#entries, Dir#glob or Dir#[] to get a listing in any folder.

Dir.entries('/Users/ccashwell/.vim/')
=> [".",
 "..",
 ".git",
 ".gitignore",
 ".gitmodules",
 ".netrwhist",
 "ackrc",
 "after",
 "autoload",
 "bundle",
 "init",
 "LICENSE",
 "README.md",
 "snippets",
 "syntax",
 "vimrc"]

Dir.glob('/Users/ccashwell/.vim/*')
=> ["/Users/ccashwell/.vim/ackrc",
 "/Users/ccashwell/.vim/after",
 "/Users/ccashwell/.vim/autoload",
 "/Users/ccashwell/.vim/bundle",
 "/Users/ccashwell/.vim/init",
 "/Users/ccashwell/.vim/LICENSE",
 "/Users/ccashwell/.vim/README.md",
 "/Users/ccashwell/.vim/snippets",
 "/Users/ccashwell/.vim/syntax",
 "/Users/ccashwell/.vim/vimrc"]

Dir['/Users/ccashwell/.vim/*']
=> ["/Users/ccashwell/.vim/ackrc",
 "/Users/ccashwell/.vim/after",
 "/Users/ccashwell/.vim/autoload",
 "/Users/ccashwell/.vim/bundle",
 "/Users/ccashwell/.vim/init",
 "/Users/ccashwell/.vim/LICENSE",
 "/Users/ccashwell/.vim/README.md",
 "/Users/ccashwell/.vim/snippets",
 "/Users/ccashwell/.vim/syntax",
 "/Users/ccashwell/.vim/vimrc"]
like image 78
Chris Cashwell Avatar answered Oct 20 '25 09:10

Chris Cashwell


The equivalent of ls (list files) from the rails console is

Dir.entries(Dir.pwd)

e.g.

Dir.entries(Dir.pwd)

# Returns this:

=> [".", "..", ".DS_Store", "app", ".ruby-version", "test", "bin", "config", "config.ru", 
 "storage", "README.md", "Rakefile", "public", ".gitignore", "package.json", "lib", "db",
 "Gemfile", "log", "Gemfile.lock", "init.R", ".git", "tmp", "vendor"]
like image 43
stevec Avatar answered Oct 20 '25 08:10

stevec



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!