-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind-replace-string.rb
More file actions
71 lines (53 loc) · 1.82 KB
/
find-replace-string.rb
File metadata and controls
71 lines (53 loc) · 1.82 KB
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
#!/usr/bin/env ruby
require 'find'
require 'fileutils'
#Module Sitemap
# variable holds page path
@@sitemap = ''
# Portion of the path you want to remove
RemoveName = "views/pages/"
# Files to ignore
NotMatch = [""]
# Folders to ignore
NoDirectory = [ ""]
def findFilePath dir, wordTosearch, replaceWordWith
Find.find(dir) do |path|
#NEW Start
if FileTest.directory?(path)
if File.basename(path)[0] == ?.
Find.prune
end
#directory to ignore
if NoDirectory.include?(path)
Find.prune
end
fileNameWithNoPages = path.sub(RemoveName, "")
fileFolderName = fileNameWithNoPages.split('/')
elsif File.file?(path)
#Find.prune
fileName = path.split('/')
#puts path
if fileName.last.match(/(sass|haml)$/)
#puts path
#@@sitemap += "FILE: (#{path}) Filename: (#{fileName.last}) \n"
#puts @@sitemap
fileNameWithNoPages = path.sub(RemoveName, "")
# if wish to remove the extention of the page
#fileNameWithNoPagesNoHaml = fileNameWithNoPages.sub('haml', "")
#Output file path
puts fileNameWithNoPages
@@FileToRead = File.read(path)
# Only edit file if their is a match
if @@FileToRead.include?(wordTosearch)
@@replacedFile = @@FileToRead.gsub(wordTosearch, replaceWordWith)
File.open(path, 'w') {|f| f.write(@@replacedFile) }
@@sitemap += "FILE: (#{path}) Filename: (#{fileName.last}) \n"
end
end
end
end
# can use to save to a file the output.
#File.open('views/pages/validation.haml', 'w') {|f| f.write(@@sitemap) }
end
## Initiate the function
findFilePath 'pathtofile', ' word-to-look-for ', ' replace-with-this-word '