touch

B

Bret Pettichord

I need to "touch" a file from my ruby script. (This is a unix-based
command that updates the timestamp on a file, creating it, if it
doesn't already exist.) I'm running on Windows. A collegue found a port
of "touch" and installed it and called it using "system", but i would
rather see a solution that requires less installation.

I am currently using the following code:

class File
def self.touch(filename)
File.open(filename, 'a'){|f| f.puts ' '}
end
end

This implementation is good enough for my present needs (touching an
xml file), but it has obvious side effects that could be a problem in
other contexts.

I did find a touch command in Ruby in the ptools package at
http://sourceforge.net/projects/ruby-miscutils/ but it did not work.
(It zeroed the file it touched!)

Any suggestions for a better "touch" in Ruby?

Bret
 
P

Philip Hallstrom

I need to "touch" a file from my ruby script. (This is a unix-based
command that updates the timestamp on a file, creating it, if it
doesn't already exist.) I'm running on Windows. A collegue found a port
of "touch" and installed it and called it using "system", but i would
rather see a solution that requires less installation.

I am currently using the following code:

class File
def self.touch(filename)
File.open(filename, 'a'){|f| f.puts ' '}
end
end

This implementation is good enough for my present needs (touching an
xml file), but it has obvious side effects that could be a problem in
other contexts.

I did find a touch command in Ruby in the ptools package at
http://sourceforge.net/projects/ruby-miscutils/ but it did not work.
(It zeroed the file it touched!)

Any suggestions for a better "touch" in Ruby?

What's wrong with FileUtils.touch() ?

http://corelib.rubyonrails.org/classes/FileUtils.html#M001802
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,529
Members
45,001
Latest member
Kendra00E1

Latest Threads

Top