如何将.sc文件重写为其他格式?

时间:2018-10-14 19:32:17

标签: ruby

我想从SpaceEngine导出sc文件,然后为每个文件创建一个名称相同但扩展名为.txt的新文件。

这是我的代码:

require 'fileutils'
require 'IO/console'

puts "Make sure your export folder is clear of everything but the files you want to turn into Object text files."
puts "Starting Process"
i = 0
Dir.foreach('C:\SpaceEngine\export') do |item|
  next if item == '.' or item == '..'
  i = i + 1
  name = File.basename(item, ".*")
  current = File.new("#{name}.txt", "w");
  current.close
end
sleep 2

我已经有了后一部分,但是我无法一一读取原始文件,然后只能将原始文件中的某些内容放入新文件中。

1 个答案:

答案 0 :(得分:0)

# test.sc
# assume this is your test data
this has foo
this does not
this also has foo
this has some other stuff
this is the last line which has foo
blah
blah blah 


# filejunk.rb
# you need to write a method that handles the data inside the file you want to
# modify, change, replace etc. but for example

def replace_file_data(filename)
  lines = File.readlines
  lines.select{|l| l.include?'foo'} #assumes you only want lines with 'foo' in them
end

Dir.glob('C:\SpaceEngine\export\*.sc').each_with_index  do |filename, i|
  i += 1
  name = File.basename(filename, ".*")
  current = File.new("#{name}.txt", "w") {|f| f.write  replace_file_data(filename) }
  current.close
end