Hi, I’m just starting out with ruby and I’m writing a script to rename
a bunch of files in a directory to a way I want them to be (just to
get more familiar). I’ve looked at the documentation for the file
class (http://www.rubycentral.com/book/ref_c_file.html), but I can’t
seem to find the method I’d use to get all the names of files in a
directory. How can I do this?
The good thing about these questions is that they are somewhat easier
to answer. There are several ways to do what you want, but they are all
basically the same:
dir_contents = Dir.entries(Dir.pwd)
The above works if your shell is in the directory of interest to you.
You can substitute a string literal for Dir.pwd
This includes any directories (and the ‘.’ and ‘…’ directories)
You can use the File.file? class method to check the entries:
require ‘ftools’
files = Array.new
Dir.new(Dir.pwd).entries.each { |n| files.push(n) if File.file?(n) }
Regards,
Mark
···
On Wednesday, October 1, 2003, at 11:24 PM, Revision17 wrote:
Hi, I’m just starting out with ruby and I’m writing a script to rename
a bunch of files in a directory to a way I want them to be (just to
get more familiar). I’ve looked at the documentation for the file
class (http://www.rubycentral.com/book/ref_c_file.html), but I can’t
seem to find the method I’d use to get all the names of files in a
directory. How can I do this?
Hi, I’m just starting out with ruby and I’m writing a script to rename
a bunch of files in a directory to a way I want them to be (just to
get more familiar). I’ve looked at the documentation for the file
class (http://www.rubycentral.com/book/ref_c_file.html), but I can’t
seem to find the method I’d use to get all the names of files in a
directory. How can I do this?
It’s in the Dir class, not the File class, which is why you didn’t
see it.
The easiest way is probably to use foreach and do your renaming in
the block you pass to it:
Dir.foreach(pathname) do
>f>
# do whatever you want with f, which is a filename within the
# given directory (not fully-qualified)
end
···
On Wed, Oct 01, 2003 at 08:01:21PM -0700, Revision17 wrote:
for unix. It’s similar for windows, but I don’t have
a windows box to test it.
···
On Thursday, 2 October 2003 at 12:24:25 +0900, Revision17 wrote:
Hi, I’m just starting out with ruby and I’m writing a script to rename
a bunch of files in a directory to a way I want them to be (just to
get more familiar). I’ve looked at the documentation for the file
class (http://www.rubycentral.com/book/ref_c_file.html), but I can’t
seem to find the method I’d use to get all the names of files in a
directory. How can I do this?
–
Jim Freeze
Hofstadter’s Law:
It always takes longer than you expect, even when you take
Hofstadter’s Law into account.
On Thursday, 2 October 2003 at 12:24:25 +0900, Revision17 wrote:
Hi, I’m just starting out with ruby and I’m writing a script to rename
a bunch of files in a directory to a way I want them to be (just to
get more familiar). I’ve looked at the documentation for the file
class (http://www.rubycentral.com/book/ref_c_file.html), but I can’t
seem to find the method I’d use to get all the names of files in a
directory. How can I do this?
The easy way is to use the glogging feature of Dir#.
For example