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

2015-07-12 0 696
树状结构输出文件目录结构

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])

遇见资源网 ruby 树状结构输出文件目录结构 http://www.ox520.com/16412.html

下一篇: ruby unit
常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务