Ruby List Files

學習Ruby 隨手寫的一個小例子。

#羅列目錄下所有的文件
class FolderList

	#初始化
	def initialize()  
		@itemCount = 0;
	end

	#羅列
	def startList()
		p = File.dirname(__FILE__);
		puts "dir path: " + p;
		puts "==========start list================="
		Dir.foreach(p) do |item|
			filename = item.class;
			 if item.index(".png") || item.index(".plist") || item.index(".tps")
			 	puts item;
			 	@itemCount = @itemCount + 1;
			 end
		end
		puts "============end list================="
	end

	#獲取目錄下所有文件數組
	def getFoldFiles()
		p = File.dirname(__FILE__);
		Dir.entries(p);
	end

	#count
	def getFoldedsCount
		return @itemCount;
	end

end

#根據文件類型羅列文件
class SubFolderList < FolderList

	#初始化
	def initialize()
		super;
		@logger = RecordLogs.new();
	end

	#根據類型羅列文件
	def listFileByType(fType)
		files = getFoldFiles();
		files.each{|item| if item.index(fType)
			puts item;
			@logger.addLog(item, "info");
		end}
	end
end

class RecordLogs

	def initialize()
		@logInfo = "";
	end

	def addLog(info, infoType)
		@logInfo ="[" + infoType + "]" + "@"  + Time.now.inspect + ":  " + @logInfo + info + "\n";
		f = File.new("log.txt","a+");
		f.syswrite(@logInfo);
		f.close();
		@logInfo = "";
	end
end


d = SubFolderList.new();
d.listFileByType(".png");



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章