-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextReplacerClass.groovy
94 lines (80 loc) · 3.29 KB
/
TextReplacerClass.groovy
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
// Groovy class to search and replace text
// within all files in a given directory
// .......................................
// Author: Maria Fitas
import groovy.io.FileType
class TextReplacerClass { // Changed class name to avoid conflict with filename
String directoryPath
String searchText
String replaceText
String logFilePath
// Constructor
TextReplacerClass(String directoryPath, String searchText, String replaceText, String logFilePath = null) {
this.directoryPath = directoryPath
this.searchText = searchText
this.replaceText = replaceText
this.logFilePath = logFilePath
}
// Main function to start the search and replace process
void processFiles() {
File dir = new File(directoryPath)
if (!dir.exists()) {
println "Directory does not exist. Please check path and try again."
return
}
dir.eachFileRecurse(FileType.FILES) { File file ->
println "Found file: ${file.path}"
println "File content: ${file.text}" // Debug to check file content
if (file.text.toLowerCase().contains(searchText.toLowerCase())) {
replaceTextInFile(file)
} else {
println "No matches found in file: ${file.path}"
}
}
}
// Function to replace text in a file
void replaceTextInFile(File file) {
println "Processing file: ${file.path}"
try {
def content = file.text
def lines = file.readLines()
int occurrences = 0
def locations = []
lines.eachWithIndex { line, index ->
if (line.toLowerCase().contains(searchText.toLowerCase())) { // Case-insensitive match
occurrences += line.count(searchText)
locations << "Line ${index + 1}: ${line}"
}
}
if (occurrences > 0) {
content = content.replaceAll("(?i)" + searchText, replaceText) // Case-insensitive replacement
createBackup(file)
file.text = content
if (logFilePath) {
logModifiedFile(file.path, occurrences, locations)
}
}
} catch (Exception e) {
println "Error processing file: ${file.path}. ${e.message}"
e.printStackTrace()
if (logFilePath) {
logModifiedFile(file.path, 0, [], true, e.message)
}
}
}
// Function to create a backup of the original file
void createBackup(File file) {
File backup = new File(file.path + ".bak")
backup.text = file.text
}
// Function to log modified files and errors
void logModifiedFile(String filePath, int replacements, List<String> locations, boolean isError = false, String errorMessage = "") {
File logFile = new File(logFilePath)
def currentTime = new Date().format("dd-MM-yyyy HH:mm:ss")
if (isError) {
logFile << "[${currentTime}] ERROR: Failed to process file: ${filePath}. Reason: ${errorMessage}\n"
} else {
logFile << "[${currentTime}] SUCCESS: Modified file: ${filePath}. Replaced ${replacements} occurrence(s) of '${searchText}' at ${locations.join(", ")}\n"
}
}
}