I had a directory with a lot of PGN chess files, from which I wanted to remove the move times (written as [%emt {a_number}]
. I wrote this script:
regex = /\[.emt[^\]]+\]/
directory = "path/to/files"
extension = ".pgn"
Dir.chdir(directory)
Dir.foreach(directory) do |file_name|
file_object = File.open(file_name, "r+")
contents = file_object.read
new_contents = contents.gsub(regex, "")
File.truncate(directory + "/" + file_name, 0)
file_object.puts(new_contents)
file_object.close
end
This removed all the move times, but curiously it appended a large number of null characters to the beginning of the files (I suspect this number is equal to the number of bytes in the file). So I replaced the line new_contents = contents.gsub(regex, "")
with contents.delete("\0")
, but this only made it worse, appending even more null characters to the beginning of the files. How can I remove them?