遍历文件夹中的特定文件 [ruby 遍历 文件夹 traverse]

今天客户要求把所有的页面和JavaScript中的提示行信息按
模块,中文,英文的格式
全部摘出来写个excel,为了检查英文是否“地道”

我设计的那部分系统的所有字符串资源都统一放在一个文件中,比较好处理
但是同事写代码时,所有字符串都写在出现的地方
手工一个一个来那可费死劲了,还容易遗漏

于是写了一个脚本 extract_alert.rb

 

require "find"
Find.find('./') do |path|
        if not File.directory?path and path[/\.jsp$/] then
                found_line = []
                File.open("#{Dir.pwd}#{path[1..path.size]}","r").each_with_index do|line,lno|
                        if line=~/[^\/]alert/ then
                                found_line<<"#{lno}:\t#{line.strip}"
                        end
                end
                if found_line.size!=0 then
                        puts path
                        puts found_line
                        puts "\n"
                end
        end
end

 本来说写一个遍历文件夹的函数,查了下Ruby Cookbook,原来Ruby早就写好了一个Find模块,使用起来很方便

extract_alert.rb>>a.txt

 

你可能感兴趣的:(JavaScript,jsp,Excel,脚本,Ruby)