Reputation: 135
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.
Upvotes: 4
Views: 4076
Reputation: 52268
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"]
Upvotes: 3
Reputation: 27553
As juanpastas mentions, this might be an escape problem. The underlying problem, however, is that a path is not a string! A path is a path(object) and could best be treated as such:
path = File.join("Z:", "*")
puts path
@files = Dir.glob(path)
Or, shorthand:
@files = Dir.glob(File.join("Z:", "*"))
Additionally, Rails (and other apps) have wrapped this Pathname in e.g. Rails.root
, use like so:
@files = Rails.root.join("lib") #=> Z:\path\to\rails\app\lib.
Upvotes: 0
Reputation: 22859
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"]
Upvotes: 5
Reputation: 21795
I think what can be happening to you is that \
escapes next character. Try:
@files = Dir.glob("Z:\\*")
or:
@files = Dir.glob("Z:/*")
Upvotes: 0