Skip to content

Instantly share code, notes, and snippets.

@rjhancock
Created October 6, 2020 13:56
Show Gist options
  • Save rjhancock/eccbee2203148b1b1954cc5b168ddc1b to your computer and use it in GitHub Desktop.
Save rjhancock/eccbee2203148b1b1954cc5b168ddc1b to your computer and use it in GitHub Desktop.
Convert CRLF -> LF and Enforce UTF-8 File Format
files = Dir['**/*.blk']
files += Dir['**/*.mtf']
files.each do |file|
puts "Checking file: #{file}"
raw = File.read(file)
# New Line Conversion from CRLF -> LF
data = raw.encode(raw.encoding, universal_newline: true)
# Convert to valid UTF-8
data = data.unpack('C*').pack('U*') if !(data.encoding.name == 'UTF-8' && data.valid_encoding?)
File.write(file, data)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment