树状结构输出文件目录结构

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>


require 'find'

class Recorder
	attr_accessor :fileSteam
	
	def initialize(outputFilePath)
		@fileSteam = File.open(outputFilePath, 'w')
	end
	
	def appendLineToFile(depth, text)
		#计算深度,写入tab,写入文件名
		str = "\t" * depth + text
		puts str
		@fileSteam.puts str
	end
	
	def closing
		@fileSteam.close
		puts "Save File #{File.absolute_path(@fileSteam)} Finished."
	end
end

class Finder
	attr_accessor :fileCount, :directoryCount, :fileNameRecorder, :originDepth
	
	def initialize(recorder)
		@fileCount = 0
		@directoryCount = 0
		@fileNameRecorder = recorder
		@originDepth = 0
	end
	
	def findInDirectory(directoryPath)
		@fileNameRecorder.appendLineToFile(0, "findInDirectory: #{directoryPath}\n")
		
		@originDepth = directoryPath.count "/" 
		
    Find.find(directoryPath) do |file|
      @fileCount += 1 if File.file?(file)
      @directoryCount += 1 if File.directory?(file)
  		@fileNameRecorder.appendLineToFile(file.count("/") - @originDepth,
  		 	File.directory?(file) ? "<" + File.basename(file) + ">" : File.basename(file))
    end
    
    @fileNameRecorder.appendLineToFile(0, "directoryCount: #{@directoryCount} filesCount: #{@fileCount}")
    @fileNameRecorder.closing
	end
end

recorder = Recorder.new(ARGV[1])
finder = Finder.new(recorder)
finder.findInDirectory(ARGV[0])