【ruby】批量刷新目录下指定文件内的字段

【功能】

1、操作目录:F:\my job\tools\2019test_file
2、查找文件:result.txt
3、需要动态获取的字段:例arr=Dev.add(“Ec_1”)之中的arr字段(不同的result.txt文件中的字段可能不同,该字段是动态的,可以使用正则来匹配)
4、原字段:self.manage
5、新字段:步骤3中获取到的字段

==result.txt文件的内容:==

1
2
3
4
5
6
7
class Test_1
arr=Dev.add("Ec_1")
self.manage.add_rule("rule")
test1 hello word
file
mk l test1
self.manage.add_rule("rule")

==刷新后的result.txt文件的内容:==

1
2
3
4
5
6
7
class Test_1
arr=Dev.add("Ec_1")
arr.add_rule("rule")
test1 hello word
file
mk l test1
arr.add_rule("rule")


==实现代码如下:==

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#encoding=UTF-8  

class ChangeData
#初始化
def initialize()
@result="There are no updated files:\n"
@dir=File.dirname(__FILE__)+'/result.txt'
puts "@dir:#{@dir}"
File.new(@dir,"w+")
end
#遍历目录下面指定的文件
def traverse_dir(file_path)
#如果 path 是一个目录,则返回 true。
if File.directory? file_path
Dir.foreach(file_path) do |file|
if file !="." and file !=".."
puts "******************************file:#{file}"
puts "******************************file_path:#{file_path}"
traverse_dir(file_path+"\\"+file)
end
end
else
puts "======"
puts "file_path:#{file_path}"
puts "File:#{File.basename(file_path)}, Size:#{File.size(file_path)}"
if file_path.include?('txt')
@result1 = read_file(file_path)
write_in(file_path,@result1)
end
end
end

#第一个 返回更新后的内容
def read_file(filename)
puts "=======================output=========================="
str = ""
re_value=[]
num=0
#获取设备个数和添加规则个数
File.open(filename,"r+"){|f|
f.each_line{|line|
if line=~/Dev.add/
re_value << line.scan(/^\s*(\S+)\s*=\s*Dev.add\(/).flatten
re_value.flatten!
end
if line=~/self.manage/
puts "====="
num +=1
end
}
}
#把self.manage替换成对应的设备变量
i=0
#新增一个txt文件用来记录未刷脚本名称
File.open(filename,"r+"){|f|
f.each_line{|line|
puts "re_value:#{re_value}"
puts "re_value.size:#{re_value.size}"
puts "num:#{num}"
#class Test_1
#获取用例名
if line.include?("class") && re_value.size>1 && num!=re_value.size
puts "class**************************88"
testcase_name=line.scan(/^\s*class\s*(\S+)/).flatten
@result =@result+testcase_name[0]+"\n"
break
end
if line.include?("self.manage")
if re_value.size==1
str += line.gsub('self.manage',re_value[0])
elsif re_value.size>1 && num==re_value.size
puts "i:#{i}"
puts "re_value[i]#{re_value[i]}"
if i<=num
puts "i:#{i},num:#{num}"
str += line.gsub("self.manage",re_value[i])
i +=1
end
end
else
str += line
end
puts "str:#{str}"
}
}
return str
end

#第二个 把返回更新后的内容重新写入即文本替换
def write_in(filename,str)
puts "=======================input=========================="
puts str
if str!=""
File.open(filename,"w"){|f|
f.write str
}
end
end
#写未刷新的用例名
def wirte_txt()
puts "@result:#{@result}&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"
File.open(@dir,"w+"){|f|
f.write @result
}

end
end


#调用事例
data=ChangeData.new()
data.traverse_dir('F:\my job\tools\2019test_file')
data.wirte_txt()

-------------本文结束感谢您的阅读-------------