Radek
Radek

Reputation: 11121

How "not to change" file attributes when editing via ruby?

I use ruby -pi~ -e \"gsub(/\\\"/, \\\"'\\\")\" \"#{dir}\\*.csv\" to replace double quotes by single quote in all files in a directory #{dir}

It works well but it changes the date/time of the file to current one.

How can I preserve the time/date properties of the file(s)?

Upvotes: 1

Views: 170

Answers (1)

peter
peter

Reputation: 42207

You can't but you can set the modification time back to before the change like this

original_time= File.mtime('myfile')
p original_time

date = Time.now - 86400
File.utime(date, date, 'myfile')
p File.mtime('myfile')

File.utime(original_time, original_time, 'myfile')
p File.mtime('myfile')

#2012-10-04 02:28:25 +0200
#2012-10-03 02:28:25 +0200
#2012-10-04 02:28:25 +0200

utime(atime, mtime, file_name,...) → integer click to toggle source Sets the access and modification times of each named file to the first two arguments. Returns the number of file names in the argument list.

Upvotes: 2

Related Questions