|
@@ -7,34 +7,25 @@ namespace PicView.Core.FileHandling;
|
|
|
public static partial class FileHelper
|
|
|
{
|
|
|
/// <summary>
|
|
|
- /// RenameFile method renames a file.
|
|
|
+ /// Renames a file by moving it to a new path. Creates the destination directory if it does not exist.
|
|
|
/// </summary>
|
|
|
- /// <param name="path">The original path of the file.</param>
|
|
|
- /// <param name="newPath">The new path of the file.</param>
|
|
|
+ /// <param name="path">The current path of the file.</param>
|
|
|
+ /// <param name="newPath">The new path to which the file will be moved.</param>
|
|
|
/// <returns>
|
|
|
- /// A boolean indicating whether the file was successfully renamed or not.
|
|
|
+ /// <c>true</c> if the file is successfully renamed; otherwise, <c>false</c>.
|
|
|
/// </returns>
|
|
|
public static bool RenameFile(string path, string newPath)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
new FileInfo(newPath).Directory.Create(); // create directory if not exists
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
-#if DEBUG
|
|
|
- Trace.WriteLine(e.Message);
|
|
|
-#endif
|
|
|
- }
|
|
|
|
|
|
- try
|
|
|
- {
|
|
|
File.Move(path, newPath, true);
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
#if DEBUG
|
|
|
- Trace.WriteLine(e.Message);
|
|
|
+ Trace.WriteLine($"{nameof(RenameFile)} {path}, {newPath} exception: \n{e.Message}\n");
|
|
|
#endif
|
|
|
return false;
|
|
|
}
|
|
@@ -132,4 +123,46 @@ public static partial class FileHelper
|
|
|
return string.Empty;
|
|
|
return File.Exists(ArchiveHelper.TempFilePath) ? ArchiveHelper.TempFilePath : string.Empty;
|
|
|
}
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Generates a new filename with an incremented number inside parentheses to avoid duplication.
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="currentFile">The path of the current file.</param>
|
|
|
+ /// <returns>
|
|
|
+ /// The path of the new file with an incremented number inside parentheses to avoid duplication.
|
|
|
+ /// </returns>
|
|
|
+ public static string DuplicateAndReturnFileName(string currentFile)
|
|
|
+ {
|
|
|
+ string newFile;
|
|
|
+ var dir = Path.GetDirectoryName(currentFile);
|
|
|
+ var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(currentFile);
|
|
|
+ var extension = Path.GetExtension(currentFile);
|
|
|
+
|
|
|
+ var i = 1;
|
|
|
+
|
|
|
+ // Check if the original filename already contains parentheses
|
|
|
+ if (fileNameWithoutExtension.Contains("(") && fileNameWithoutExtension.EndsWith(")"))
|
|
|
+ {
|
|
|
+ // Extract the number from the existing parentheses
|
|
|
+ var lastParenIndex = fileNameWithoutExtension.LastIndexOf("(", StringComparison.Ordinal);
|
|
|
+ var numberStr = fileNameWithoutExtension.Substring(lastParenIndex + 1,
|
|
|
+ fileNameWithoutExtension.Length - lastParenIndex - 2);
|
|
|
+
|
|
|
+ if (int.TryParse(numberStr, out var existingNumber))
|
|
|
+ {
|
|
|
+ i = existingNumber + 1;
|
|
|
+ fileNameWithoutExtension = fileNameWithoutExtension[..lastParenIndex].TrimEnd();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Generate a new filename with an incremented number inside parentheses
|
|
|
+ do
|
|
|
+ {
|
|
|
+ newFile = Path.Combine(dir, $"{fileNameWithoutExtension}({i++}){extension}");
|
|
|
+ } while (File.Exists(newFile));
|
|
|
+
|
|
|
+ // Copy the file to the new location
|
|
|
+ File.Copy(currentFile, newFile);
|
|
|
+ return newFile;
|
|
|
+ }
|
|
|
}
|