uwuhazelnut / DateiRenamer

0 stars 0 forks source link

DateiRenamer - move digit blocks #7

Open uwuhazelnut opened 1 year ago

uwuhazelnut commented 1 year ago

Task: add a way to move digit blocks in the file name to the start or the end of the file name.

uwuhazelnut commented 1 year ago

This was done by detecting a block of digits using a regular expression. Then a new file name string is created by cutting the digit block off the current file name string and reattaching it to either the start or end of the current file name string.

public static void moveDigits(string directoryPath)
{
Console.WriteLine("Wohin sollen die Zahlenblöcke verschoben werden?");
Console.WriteLine("[0]: an den Anfang, [1]: an das Ende");

int userOption = getUserOption(1);

moveDigitsProcessor(new DirectoryInfo(directoryPath), userOption);

Console.WriteLine("Zahlenblöcke verschoben (wenn vorhanden).");
}

private static void moveDigitsProcessor(DirectoryInfo directory, int userOption)
{
FileInfo[] files = directory.GetFiles();

foreach (FileInfo file in files)
{
    string fileName = Path.GetFileNameWithoutExtension(file.FullName); // Get file name without extension to allow for moving string to the end without modifying the file extension
    string fileExtension = Path.GetExtension(file.FullName);

    if (containsDigitBlock(fileName))
    {
        if (userOption == 0)
        {
            // Use regular expression to find the digit block and move it to the beginning:
            Match digitBlockMatch = Regex.Match(fileName, @"\d+");
            string digitBlock = digitBlockMatch.Value;
            string remainingFileName = fileName.Remove(digitBlockMatch.Index, digitBlockMatch.Length); // Removes the digit block from the file name using the Regex match information
            string newFileName = digitBlock + remainingFileName;

            string newPath = Path.Combine(directory.FullName, newFileName + fileExtension); // Reattach file extension to the file name
            file.MoveTo(newPath);
        }
        else if (userOption == 1)
        {
            //Move digit block to end: 
            Match digitBlockMatch = Regex.Match(fileName, @"\d+");
            string digitBlock = digitBlockMatch.Value;
            string remainingFileName = fileName.Remove(digitBlockMatch.Index, digitBlockMatch.Length);
            string newFileName = remainingFileName + digitBlock;

            string newPath = Path.Combine(directory.FullName, newFileName + fileExtension);
            file.MoveTo(newPath);
        }
    }
}

DirectoryInfo[] subDirectories = directory.GetDirectories();
foreach (DirectoryInfo subDirectory in subDirectories)
{
    moveDigitsProcessor(subDirectory, userOption);
}
}

private static bool containsDigitBlock(string fileName)
{
// Use regular expression to check if the filename contains a block of digits
return Regex.IsMatch(fileName, @"\d+");
}
uwuhazelnut commented 1 year ago

Optimisation of if-statement:

if (containsDigitBlock(fileName))
{
    // Use regular expression to find the digit block:
    Match digitBlockMatch = Regex.Match(fileName, @"\d+");
    string digitBlock = digitBlockMatch.Value;
    string remainingFileName = fileName.Remove(digitBlockMatch.Index, digitBlockMatch.Length); // Removes the digit block from the file name using the Regex match information
    string newFileName = string.Empty; // An empty string is necessary to remove a compiler error when creating the new file path later on

    if (userOption == 0)
    {
        newFileName = digitBlock + remainingFileName; // Move digit block to beginning
    }
    else if (userOption == 1)
    {
        newFileName = remainingFileName + digitBlock; //Move digit block to end
    }

    string newPath = Path.Combine(directory.FullName, newFileName + fileExtension); // Reattach file extension to the file name
    file.MoveTo(newPath);
}