Skip to content

Instantly share code, notes, and snippets.

@semicolonsnet
Created July 26, 2021 04:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save semicolonsnet/412cd5e89a559630159cfa1d05ab3ed5 to your computer and use it in GitHub Desktop.
Save semicolonsnet/412cd5e89a559630159cfa1d05ab3ed5 to your computer and use it in GitHub Desktop.
Concatenates text files in a directory with daily date stamps into a single file for last month. The script is designed to be run on the first of the month to concatenate text files (e.g. journal entries) from the previous month.
#!/usr/bin/env ruby
$textdir = ""
$fileprefix = ""
$deletefiles = "false"
require 'time'
today = Time.now
thisyear = today.strftime("%Y")
thismonth = today.strftime("%m")
month = ((thismonth.to_i - 1).to_s)
if thismonth == "01"
newyear = ((thisyear.to_i - 1).to_s)
year = newyear
month = "12"
else
year = thisyear
end
if month.size == 1
month.insert 0, "0"
end
day = "01"
$output = ""
begin
filename = ( $notebookdir + $fileprefix + year + "-" + month + "-" + day + ".txt" )
if File.file?(filename)
newtext = ""
openfile = File.open filename
$output.concat "\n" + "## " + year + "-" + month + "-" + day + "\n"
newtext = openfile.read
$output.concat newtext
$output.concat "\n"
openfile.close
if $deletefiles == "true"
File.delete filename
end
end
#
#
# else end
#
day.next!
end until day == "32"
if $output ==""
puts "Error! No relevant files to concatenate!"
else
writefilename = $notebookdir + $fileprefix + year + "-" + month + ".txt"
writefile = File.new(writefilename, "a")
writefile.puts $output
writefile.close
puts "Created new file " + writefilename
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment