-
-
Save bergantine/4487752 to your computer and use it in GitHub Desktop.
Concatenate files in a directory into a single file. #ruby #donnieclapp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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