Replace specific text in file with it's own Filename using linqpad, C#, and regex

110 views Asked by At

Operating on windows 10 32bit at the moment unfortunately. I'm editing cuesheets to match back up with mp3s and m4bs whose file names have changed. I'm looking for a method to easily automate the process. Because I generally name cuesheets to match the name of the paired audio file, I thought the simplest method would just be to grab it's own file name (hopefully without it's file extension). I have the regex to select the text I want to replace working just fine in notepad++ and several other apps as well. Sadly, I have yet to find a prebuilt find and replace solution that has the file's own name that is being edited as a variable. So, I spent some time searching and the most promising thing I found was in another post (Use filename as variable when replacing text in files).

Example line in cue file:

FILE "Alfred Minoa.1998 - Animals of The Sahara - Documentary.m4b" MP4

Portion to be replaced:

Alfred Minoa.1988 - Animals of The Sahara - Documentary

New names of m4b & cue file:

Alfred Minoa - Animals of The Sahara - Documentary.cue

Alfred Minoa - Animals of The Sahara - Documentary.m4b

Goal is to replace text "Alfred Minoa.1998 - Animals of The Sahara - Documentary" inside the cue file with name of the cue file minus the ".cue" file Extension.

Updated and now working code thanks to input from Fildor

Update: Figured out the last tidbit Solved

Here's the code for anyone that might be looking to acheive the same thing or something similar.

    var files = new DirectoryInfo(@"D:\TestFolder\).GetFiles("*.cue", SearchOption.AllDirectories);
    foreach(var fileInfo in files)
    {
        var text = File.ReadAllText(fileInfo.FullName);
        string TrimmedText = fileInfo.Name; // Get Filename into String
        TrimmedText = TrimmedText.Substring(0, TrimmedText.Length - 4); // Trim File Extension from filename
        text = Regex.Replace(text, @"(?<=FILE "").+?(?=\...."")", TrimmedText); // Replace "pattern" with filename
        File.WriteAllText(fileInfo.FullName, text);
    }

Thank you to those who took the time to contribute.

2

There are 2 answers

0
sgmoore On

Don't know what your search string is trying to do, but you could try this.

string searchStr   = @"FILE \"".+?\""" ;
string replacement = @$"FILE ""{System.IO.Path.GetFileNameWithoutExtension(fileInfo.Name)}""" ;
text = Regex.Replace(text, searchStr, replacement);

Regex syntax sometimes varies between languages but seeing you are using LinqPad, you can use the Ctrl-Shift-F1 to test the search part of the regex in C# (but not the replace)

4
Bagus Tesa On

You could find enclosing FILE and MP4 using the following regex:

(?:(FILE "").*("" (MP4|M4B)))

Fiddle: https://regex101.com/r/RU70xd/2

Then you can use it this way:

string t = "FILE \"Alfred Minoa.1998 - Animals of The Sahara - Documentary.m4b\" MP4";
string pat = "(?:(FILE \").*(\" (MP4|M4B)))";
Regex rx = new Regex(pat, RegexOptions.Compiled
   | RegexOptions.IgnoreCase | RegexOptions.Multiline);
string nt = rx.Replace(t, "$1TEST$2");
Console.WriteLine(nt);

It will prints

FILE "TEST" MP4

You can replace the TEST with whatever string you wanted, but keep both enclosure (the $1 and $2).

Fiddle: https://dotnetfiddle.net/vjY7qZ