I'm new to C# and need to loop through a directory and remove the last part of any and all files that end in _xxxxxx.xml
(an underscore followed by 6 digits).
For example, the filename filename_A_123_456789.xml
becomes filename_A_123.xml
.
What I have so far:
private static void RenameFiles(string outputPath)
{
DirectoryInfo d = new DirectoryInfo(outputPath);
FileInfo[] infos = d.GetFiles();
foreach (FileInfo f in infos)
{
string newName = Regex.Replace(f.FullName, "(\\[_\d]\\s)", temp);
File.Move(f.FullName, newName);
}
}
But that's not working. Ang CSharp Regexxers out there?
You can use the regex:
_\d{6}(\.[^.]+)$
and replace with $1
instead.
The regex is matching 6 digits, then group 1 ( (\.[^.]+)
) matches the extension, which you replace with in the replacement string. The extension is matched by "a dot followed by a bunch of non-dots". Also note that the end of string anchor $
to assert that all of this must be at the end of the string.
Change your code to:
string newName = Regex.Replace(f.FullName, @"_\d{6}(\.[^.]+)$", "$1");
A regex is fine for your need. As an alternative, here is a regex free solution if you want to use one:
public static void Main()
{
var source = @"filename_A_123_456789.xml";
var slices = Path.GetFileNameWithoutExtension(source).Split('_');
var last = slices.Last();
var isSpecific = last.Length == 6 && last.All(char.IsDigit);
var result = string.Join("_", isSpecific ? slices.Take(slices.Length - 1) : slices) + Path.GetExtension(source);
Console.WriteLine(result);
}
Your regex doesn't quantify the \d
in any way to define that you are looking for 6 digits to be exact.
Use this instead:
(\S*)_\d{6}(\.xml) /m
This regex captures all non-whitespace characters before the _
followed by six digits, and the extension .xml
into two separate capturing groups.
Then replace with $1$2
, group 1 followed by group 2.
Just try:
string newName = Regex.Replace(f.FullName, "(\\_\\d{6}\\.xml)", ".xml");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.