Skip to content

Instantly share code, notes, and snippets.

@toddlers
Forked from bergantine/gist:4487752
Created December 12, 2017 03:56
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 toddlers/de127f24ba917028dc2aaaf5104f8622 to your computer and use it in GitHub Desktop.
Save toddlers/de127f24ba917028dc2aaaf5104f8622 to your computer and use it in GitHub Desktop.
Concatenate files in a directory into a single file. #ruby #donnieclapp
# Iterates over files and subdirectories in directorie[s] given as arguments
# and adds raw text of those files to merged.txt in the working directory
# where the script is called
# Call like this:
# ruby merge.rb {absolute path portion to delete} {directory to scan} [{directory to scan}]
# For example:
# ruby merge.rb /Users/donnieclapp/Projects/ ~/Projects/htl-website/myproject/static_media/stylesheets
# create or open the merged.txt file for writing (in working directory)
File.open('merged.txt','a') do |mergedFile|
# save first argument as portion of path to delete from output, then remove from argument array
dirTree = File.absolute_path(ARGV[0])
ARGV.shift
# For each argument given,
ARGV.each do |arg|
# find its real path (to account for users adding trailing slashes or not), and
topDir = File.absolute_path(arg)
# create an array of all the files in that directory and its subdirectories.
filesInDir = Dir["#{topDir}/**/**/*.*"]
# Then for each file in that array,
filesInDir.each do |file|
# add a header to merged.txt with the relative path of that file
# (removing first argument given to script)
unless File.basename(file) =~ /jpg|png|gif|modernizr|fancybox|jquery/
relativePath = File.absolute_path(file).gsub("#{dirTree}","..")
puts "processing: #{relativePath}"
mergedFile << "\n\n=========================================================\n"
mergedFile << "#{relativePath}\n"
mergedFile << "=========================================================\n\n"
# open the current file and add each line to merged.txt
text = File.open(file, 'r').read
text.each_line do |line|
mergedFile << line
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment