-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.old
219 lines (189 loc) · 6.31 KB
/
Program.old
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using System.Text;
using System.Diagnostics;
class Program
{
static void Main()
{
Console.Write("Created by Gabrielzv1233\n");
Console.Write("Enter the folder to search: ");
string? folderToSearch = Console.ReadLine()?.Trim();
Console.Write("Enter the string to search for: ");
string? searchTerm = Console.ReadLine()?.Trim();
Console.Write("Count files before scanning? (yes/no): ");
string? countFilesInput = Console.ReadLine()?.Trim().ToLower();
bool countFiles = string.IsNullOrEmpty(countFilesInput) || countFilesInput.StartsWith('y');
Console.Write("Open location on found files? (yes/no): ");
string? openOnFoundInput = Console.ReadLine()?.Trim().ToLower();
bool openOnFound = string.IsNullOrEmpty(openOnFoundInput) || openOnFoundInput.StartsWith('y');
if (string.IsNullOrEmpty(folderToSearch) || !Directory.Exists(folderToSearch))
{
Console.WriteLine("Invalid folder path. Please try again.");
return;
}
if (string.IsNullOrEmpty(searchTerm))
{
Console.WriteLine("Search term cannot be empty. Please try again.");
return;
}
SearchStringInFiles(folderToSearch, searchTerm, countFiles, openOnFound);
}
static void SearchStringInFiles(string folder, string searchString, bool countFiles, bool openOnFound)
{
int scannedFiles = 0;
int foundFilesCount = 0;
List<string> foundFiles = new();
int? totalFiles = null;
if (countFiles)
{
try
{
totalFiles = Directory.EnumerateFiles(folder, "*.*", SearchOption.AllDirectories).Count();
Console.WriteLine($"Total files to scan: {totalFiles}");
}
catch (UnauthorizedAccessException)
{
Console.WriteLine("Skipping file count due to insufficient permissions.");
}
}
else
{
Console.WriteLine("Skipping file count. Scanning directly...");
}
foreach (var file in EnumerateFilesSafe(folder))
{
scannedFiles++;
string status = "Not Found";
try
{
if (IsUtf8File(file) && FileContainsString(file, searchString))
{
foundFiles.Add(file);
foundFilesCount++;
status = "Found";
if (openOnFound)
Process.Start("explorer.exe", $"/select,\"{file}\"");
}
}
catch (Exception e)
{
status = $"Error ({e.Message})";
}
if (totalFiles.HasValue)
Console.WriteLine($"Scanned {scannedFiles}/{totalFiles}: {file} | Status: {status}");
else
Console.WriteLine($"Scanned {scannedFiles}: {file} | Status: {status}");
}
Console.WriteLine($"\nTotal files scanned: {scannedFiles}");
Console.WriteLine($"Total files found: {foundFilesCount}");
if (foundFilesCount > 0)
{
Console.WriteLine("\nFound files:");
foreach (string foundFile in foundFiles)
{
Console.WriteLine(foundFile);
}
}
else
{
Console.WriteLine("\nNo files found containing the search term.");
}
Console.WriteLine("Press Enter to exit...");
Console.ReadLine();
}
static IEnumerable<string> EnumerateFilesSafe(string path)
{
Stack<string> dirs = new();
List<string> files = new();
dirs.Push(path);
while (dirs.Count > 0)
{
string currentDir = dirs.Pop();
try
{
foreach (var file in Directory.EnumerateFiles(currentDir))
{
files.Add(file);
}
foreach (var subDir in Directory.EnumerateDirectories(currentDir))
{
dirs.Push(subDir);
}
}
catch (UnauthorizedAccessException)
{
Console.WriteLine($"Access denied to: {currentDir}");
}
}
return files;
}
static bool FileContainsString(string filePath, string searchString)
{
try
{
using var reader = new StreamReader(filePath, Encoding.UTF8, true, bufferSize: 4096);
string line;
while ((line = reader.ReadLine()) != null)
{
if (line.Contains(searchString, StringComparison.OrdinalIgnoreCase))
return true;
}
}
catch{}
return false;
}
static bool IsUtf8File(string filePath)
{
try
{
using var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) > 0)
{
if (!IsValidUtf8(buffer, bytesRead))
return false;
}
return true;
}
catch
{
return false;
}
}
static bool IsValidUtf8(byte[] bytes, int length)
{
int i = 0;
while (i < length)
{
byte b = bytes[i];
if (b <= 0x7F)
{
i++;
continue;
}
else if ((b & 0xE0) == 0xC0)
{
if (i + 1 >= length || (bytes[i + 1] & 0xC0) != 0x80)
return false;
i += 2;
}
else if ((b & 0xF0) == 0xE0)
{
if (i + 2 >= length || (bytes[i + 1] & 0xC0) != 0x80 || (bytes[i + 2] & 0xC0) != 0x80)
return false;
i += 3;
}
else if ((b & 0xF8) == 0xF0)
{
if (i + 3 >= length || (bytes[i + 1] & 0xC0) != 0x80 || (bytes[i + 2] & 0xC0) != 0x80 || (bytes[i + 3] & 0xC0) != 0x80)
return false;
i += 4;
}
else
{
return false;
}
}
return true;
}
}