Ryan Leavengood
6/22/2005 4:38:00 PM
Elliott said:
> Hello,
>
> I'd like to recursively go through a bunch of directories and for text
> file - convert any linefeed (LF) endings to carriage return line feeds
> (CRLF). Ideally, I'd like to be able to do the conversion either way
> and to distinguish from binary files (so that I don't bother doing any
> changes to these files). Lastly, I'd like to package this utility as
> a standalone .exe for windows for systems that don't have ruby on it.
>
[snip]
> Anything info would be appreciated.
Hi Elliott,
Firstly, welcome to Ruby! :)
For the recursive directory search you will need to use the Find module.
For the conversion you will need to learn about reading and writing files
with either the IO or File classes, as well as regular expressions. Also
take a look at the FileUtils module.
As Mark Hubbart says binary files can usually be determined by looking for
control characters. Unfortunately [[:cntrl:]] matches newlines, so that
isn't the character class to use. This should work fairly well:
class File
def binary?
(self.read(100) =~ /[\x00-\x06]/) != nil
end
end
Here is a test for the above (for Windows systems with the default install
location):
['C:\ruby\bin\ruby.exe', 'C:\ruby\readme.txt'].each do |filename|
File.open(filename) do |file|
if file.binary?
puts "#{filename} is binary"
else
puts "#{filename} is NOT binary"
end
end
end
Also as Mark said, you can package up your ruby script using rubyscript2exe.
Ryan