Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Updated filename script to work with or without Zettelkasten ID
#!/usr/bin/ruby
#
# Inserts the file's name as the first header in the file, if it isn't already there.
# Original file saved as filename.bak.
#
# As always, caveat emptor. This code could harm your files, so try it on a backup.
#
Dir.glob('*.md') do |filename|
puts "working on: #{filename}"
id_text = nil
line_cnt = 0
should_add = false
title = filename.chomp('.md')
File.open('temp.txt', 'w') do |temp|
File.foreach(filename) do |line|
line_cnt += 1 #increment line count
if line_cnt == 1 #if we're evaluating first line
if line !~ /^# #{title}[ ]*$/ #if first line does NOT match title
temp.puts("# " + title + "\n\n") #then put it there
end
end
temp.puts(line)
end
end
File.rename(filename, filename + '.bak')
File.rename('temp.txt', filename)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment