Skip to content

Commit d264586

Browse files
author
LoneWandererProductions
committed
Cleanup a bit more.
1 parent 4f94d4e commit d264586

3 files changed

Lines changed: 79 additions & 147 deletions

File tree

FileHandler/DirectoryInformation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* PROJECT: FileHandler
44
* FILE: FileHandler/DirectoryInformation.cs
55
* PURPOSE: Generic System Functions for Directories
6-
* PROGRAMER: Peter Geinitz (Wayfarer) – Refactor by ChatGPT
6+
* PROGRAMER: Peter Geinitz (Wayfarer)
77
*/
88

99
using System;

FileHandler/FileHandleRenameExtension.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* PROJECT: FileHandler
44
* FILE: FileHandler/FileHandleRenameExtension.cs
55
* PURPOSE: Unified safe rename helpers (append/remove/replace/reorder)
6-
* PROGRAMER: Peter Geinitz (Wayfarer) – Refactor by ChatGPT
6+
* PROGRAMER: Peter Geinitz (Wayfarer)
77
*/
88

99
using System;

FileHandler/FileHandleSearch.cs

Lines changed: 77 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -2,113 +2,103 @@
22
* COPYRIGHT: See COPYING in the top level directory
33
* PROJECT: FileHandler
44
* FILE: FileHandler/FileHandleSearch.cs
5-
* PURPOSE: Does all types of File Operations, search Files
6-
* PROGRAMER: Peter Geinitz (Wayfarer)
5+
* PURPOSE: Handles all types of file searches (refactored for clarity and safety)
6+
* PROGRAMER: Peter Geinitz (Wayfarer)
77
*/
88

9-
// ReSharper disable MemberCanBeInternal, we use it external
10-
// ReSharper disable UnusedMember.Global, it is a library
11-
129
using System.Collections.Generic;
1310
using System.Diagnostics;
1411
using System.IO;
1512
using System.Linq;
16-
using System.Threading.Tasks;
1713

1814
namespace FileHandler;
1915

2016
/// <summary>
21-
/// Handles most File Searches
17+
/// Handles most file searches.
2218
/// </summary>
2319
public static class FileHandleSearch
2420
{
2521
/// <summary>
26-
/// Collects all files with a specific Extension
22+
/// Collects all files with specific extensions (full path).
2723
/// </summary>
28-
/// <param name="path">Target Folder</param>
29-
/// <param name="appendix">List of File Extension</param>
30-
/// <param name="subdirectories">Include Sub-folders</param>
31-
/// <returns>List of Files with Path and extensions<see cref="T:List{string}" />.</returns>
32-
public static List<string>? GetFilesByExtensionFullPath(string path, IEnumerable<string> appendix,
33-
bool subdirectories)
24+
/// <param name="path">The path.</param>
25+
/// <param name="appendix">The appendix.</param>
26+
/// <param name="subdirectories">if set to <c>true</c> [subdirectories].</param>
27+
/// <returns>File by criteria</returns>
28+
public static List<string> GetFilesByExtensionFullPath(string path, IEnumerable<string> appendix, bool subdirectories)
3429
{
35-
var lst = new List<string>();
36-
3730
if (string.IsNullOrEmpty(path) || !Directory.Exists(path))
38-
{
39-
return null;
40-
}
31+
return new List<string>();
4132

42-
foreach (var file in appendix.Select(app =>
43-
FileHandlerProcessing.GetFilesByExtension(path, app, subdirectories)))
44-
{
45-
if (file == null)
46-
{
47-
return null;
48-
}
33+
var lst = new List<string>();
4934

50-
lst.AddRange(file);
35+
foreach (var app in appendix)
36+
{
37+
var files = FileHandlerProcessing.GetFilesByExtension(path, app, subdirectories) ?? new List<string>();
38+
lst.AddRange(files);
5139
}
5240

5341
return lst;
5442
}
5543

5644
/// <summary>
57-
/// Collects all files with a specific Extension
45+
/// Collects all files with a specific extension (full path).
5846
/// </summary>
59-
/// <param name="path">Target Folder</param>
60-
/// <param name="appendix">File Extension</param>
61-
/// <param name="subdirectories">Include Sub-folders</param>
62-
/// <returns>List of Files with Path and extension<see cref="T:List{string}" />.</returns>
63-
public static List<string>? GetFilesByExtensionFullPath(string path, string appendix, bool subdirectories)
47+
/// <param name="path">The path.</param>
48+
/// <param name="appendix">The appendix.</param>
49+
/// <param name="subdirectories">if set to <c>true</c> [subdirectories].</param>
50+
/// <returns>File by criteria</returns>
51+
public static List<string> GetFilesByExtensionFullPath(string path, string appendix, bool subdirectories)
6452
{
65-
return FileHandlerProcessing.GetFilesByExtension(path, appendix, subdirectories);
53+
return FileHandlerProcessing.GetFilesByExtension(path, appendix, subdirectories) ?? new List<string>();
6654
}
6755

6856
/// <summary>
69-
/// Collects all files with a specific Extension
57+
/// Collects file names with extension only.
7058
/// </summary>
71-
/// <param name="path">Target Folder</param>
72-
/// <param name="appendix">File Extension</param>
73-
/// <param name="subdirectories">Include Sub-folders</param>
74-
/// <returns>List of Name of Files, with extension<see cref="T:List{string}" />.</returns>
75-
public static List<string>? GetFileByExtensionWithExtension(string path, string appendix, bool subdirectories)
59+
/// <param name="path">The path.</param>
60+
/// <param name="appendix">The appendix.</param>
61+
/// <param name="subdirectories">if set to <c>true</c> [subdirectories].</param>
62+
/// <returns>File by criteria</returns>
63+
public static List<string> GetFileByExtensionWithExtension(string path, string appendix, bool subdirectories)
7664
{
77-
var files = FileHandlerProcessing.GetFilesByExtension(path, appendix, subdirectories);
78-
79-
if (files == null)
80-
{
81-
return null;
82-
}
83-
84-
var lst = new List<string>();
85-
lst.AddRange(files.Select(Path.GetFileName));
65+
var files = FileHandlerProcessing.GetFilesByExtension(path, appendix, subdirectories) ?? new List<string>();
66+
return files.Select(Path.GetFileName).ToList();
67+
}
8668

87-
return lst;
69+
/// <summary>
70+
/// Collects file names without path, with extension.
71+
/// </summary>
72+
/// <param name="path">The path.</param>
73+
/// <param name="appendix">The appendix.</param>
74+
/// <param name="subdirectories">if set to <c>true</c> [subdirectories].</param>
75+
/// <returns>File by criteria</returns>
76+
public static List<string> GetFileByExtensionWithoutExtension(string path, string appendix, bool subdirectories)
77+
{
78+
var files = FileHandlerProcessing.GetFilesByExtension(path, appendix, subdirectories) ?? new List<string>();
79+
return files.Select(Path.GetFileNameWithoutExtension).ToList();
8880
}
8981

9082
/// <summary>
91-
/// Get the all files.
83+
/// Get all files in a folder (optionally including subfolders).
9284
/// </summary>
9385
/// <param name="path">The path.</param>
94-
/// <param name="subdirectories">The subdirectories.</param>
95-
/// <returns>List of Name of Files, with extension and Path<see cref="T:List{string}" />.</returns>
96-
public static List<string>? GetAllFiles(string path, bool subdirectories)
86+
/// <param name="subdirectories">if set to <c>true</c> [subdirectories].</param>
87+
/// <returns>File by criteria</returns>
88+
public static List<string> GetAllFiles(string path, bool subdirectories)
9789
{
98-
return FileHandlerProcessing.GetFilesByExtension(path, null, subdirectories);
90+
return FileHandlerProcessing.GetFilesByExtension(path, null, subdirectories) ?? new List<string>();
9991
}
10092

10193
/// <summary>
102-
/// Gets the file detail.
94+
/// Get details of a single file.
10395
/// </summary>
10496
/// <param name="path">The path.</param>
105-
/// <returns>The Details of a File, to be extended<see cref="T:List{FileDetails}" /> can return null.</returns>
97+
/// <returns>File Details</returns>
10698
public static FileDetails? GetFileDetails(string path)
10799
{
108100
if (!File.Exists(path))
109-
{
110101
return null;
111-
}
112102

113103
var fileInfo = FileVersionInfo.GetVersionInfo(path);
114104
var fi = new FileInfo(path);
@@ -129,131 +119,73 @@ public static class FileHandleSearch
129119
}
130120

131121
/// <summary>
132-
/// Get the file details.
122+
/// Get details of multiple files.
133123
/// </summary>
134124
/// <param name="files">The files.</param>
135-
/// <returns>The Details of the File, to be extended<see cref="T:List{FileDetails}" />.</returns>
136-
public static List<FileDetails>? GetFilesDetails(List<string> files)
125+
/// <returns>File Details</returns>
126+
public static List<FileDetails> GetFilesDetails(List<string> files)
137127
{
138128
if (files == null || files.Count == 0)
139-
{
140-
return null;
141-
}
129+
return new List<FileDetails>();
142130

143-
var data = new List<FileDetails>(files.Count);
144-
145-
data.AddRange(from file in files where File.Exists(file) select GetFileDetails(file));
146-
147-
return data;
131+
return files.Select(GetFileDetails).Where(f => f != null)!.ToList()!;
148132
}
149133

150134
/// <summary>
151-
/// Collects all files with a specific Extension
135+
/// Get immediate subfolders.
152136
/// </summary>
153-
/// <param name="path">Target Folder</param>
154-
/// <param name="appendix">File Extension</param>
155-
/// <param name="subdirectories">Include Sub-folders</param>
156-
/// <returns>List of Name of Files, without extension and Path</returns>
157-
public static List<string>? GetFileByExtensionWithoutExtension(string path, string appendix, bool subdirectories)
158-
{
159-
var files = FileHandlerProcessing.GetFilesByExtension(path, appendix, subdirectories);
160-
161-
if (files == null)
162-
{
163-
return null;
164-
}
165-
166-
var lst = new List<string>();
167-
lst.AddRange(files.Select(Path.GetFileNameWithoutExtension));
168-
169-
return lst;
170-
}
171-
172-
/// <summary>
173-
/// Mostly used for Save Game Operations
174-
/// </summary>
175-
/// <param name="path">Target Folder</param>
176-
/// <returns>Returns all Subfolders</returns>
177-
/// <exception cref="FileHandlerException">No Correct Path was provided</exception>
178-
public static List<string>? GetAllSubfolders(string path)
137+
/// <param name="path">The path.</param>
138+
/// <returns>Subfolders</returns>
139+
/// <exception cref="FileHandler.FileHandlerException"></exception>
140+
public static List<string> GetAllSubfolders(string path)
179141
{
180142
if (string.IsNullOrEmpty(path))
181-
{
182143
throw new FileHandlerException(FileHandlerResources.ErrorEmptyString);
183-
}
184144

185145
if (!Directory.Exists(path))
186-
{
187-
return null;
188-
}
189-
190-
var list = Directory.GetDirectories(path).ToList();
146+
return new List<string>();
191147

192-
return list.ConvertAll(Path.GetFileName);
148+
return Directory.GetDirectories(path).Select(Path.GetFileName).ToList();
193149
}
194150

195151
/// <summary>
196-
/// Simple Check if Folder Contains something
152+
/// Check if a folder contains any file or folder.
197153
/// </summary>
198-
/// <param name="path">Target Path</param>
199-
/// <returns>True if we find Something</returns>
200-
/// <exception cref="FileHandlerException">No Correct Path was provided</exception>
201-
public static async Task<bool> CheckIfFolderContainsElement(string path)
154+
/// <param name="path">The path.</param>
155+
/// <returns>Check if folder is empty.</returns>
156+
/// <exception cref="FileHandler.FileHandlerException"></exception>
157+
public static bool CheckIfFolderContainsElement(string path)
202158
{
203159
if (string.IsNullOrEmpty(path))
204-
{
205160
throw new FileHandlerException(FileHandlerResources.ErrorEmptyString);
206-
}
207161

208162
if (!Directory.Exists(path))
209-
{
210163
return false;
211-
}
212164

213-
return await Task.Run(() =>
214-
{
215-
var fileCheck = Directory.GetFiles(path).FirstOrDefault();
216-
return fileCheck != null;
217-
});
165+
return Directory.EnumerateFileSystemEntries(path).Any();
218166
}
219167

220-
221168
/// <summary>
222-
/// Get the files that contain this sub string. If Invert is true, files that don't contain it.
169+
/// Get files containing a specific substring (or inverse).
223170
/// </summary>
224171
/// <param name="path">The path.</param>
225172
/// <param name="appendix">The appendix.</param>
226173
/// <param name="subdirectories">if set to <c>true</c> [subdirectories].</param>
227174
/// <param name="subString">The sub string.</param>
228-
/// <param name="invert">if set to <c>true</c> [invert], does not contain [subString].</param>
229-
/// <returns>List of files with Extension and Path that contain this string</returns>
230-
public static List<string>? GetFilesWithSubString(string path, IEnumerable<string> appendix, bool subdirectories,
175+
/// <param name="invert">if set to <c>true</c> [invert].</param>
176+
/// <returns>All files with a specific substring.</returns>
177+
public static List<string> GetFilesWithSubString(string path, IEnumerable<string> appendix, bool subdirectories,
231178
string subString, bool invert)
232179
{
233180
var lst = GetFilesByExtensionFullPath(path, appendix, subdirectories);
234181

235-
if (lst == null || lst.Count == 0)
236-
{
237-
return null;
238-
}
239-
240-
var list = new List<string>();
182+
if (lst.Count == 0)
183+
return new List<string>();
241184

242-
if (invert)
185+
return lst.Where(element =>
243186
{
244-
list.AddRange(from element in lst
245-
let file = Path.GetFileName(element)
246-
where !file.Contains(subString)
247-
select element);
248-
249-
return list;
250-
}
251-
252-
list.AddRange(from element in lst
253-
let file = Path.GetFileName(element)
254-
where file.Contains(subString)
255-
select element);
256-
257-
return list;
187+
var file = Path.GetFileName(element);
188+
return invert ? !file.Contains(subString) : file.Contains(subString);
189+
}).ToList();
258190
}
259191
}

0 commit comments

Comments
 (0)