0

I have a directory with release note files, I need to show a link to their pdf in website:

File names look like this:

Appower Release Notes 2.104.0_August102021.pdf
Appower Release Notes 2.107.0_November092021.pdf
Appower Release Notes 2.108.0_December072021.pdf

HTML markup:

<ol>
    <li>
        <a download="" href="~/Content/ReleaseNotes/ Appower Release Notes 2.104.0_August102021.pdf">August</a>
    </li>
    <li>
        <a download="" href="~/Content/ReleaseNotes/Appower Release Notes 2.107.0_November092021.pdf">November</a>
    </li>
    <li>
        <a download="" href="~/Content/ReleaseNotes/Appower Release Notes 2.108.0_December072021.pdf">December</a>
    </li>
</ol>

Instead of hardcoding August, November and December, I need to get the month from File name, so Every time that I upload files I can get the name dynamically instead of code change,

I have tried to use this code:

var enumDir = Directory.GetFiles(Server.MapPath("~/Content/ReleaseNotes"))
                       .Where(a => Path.GetFileName(a).Length > 12);

foreach (var item in enumDir)
{
    var alt_right6 = new string(item.Reverse().Take(10).Reverse().ToArray()); 
}
       

This code will return this: 102021.pdf - how get December from this?

Also open to better approach.

1
  • 2
    string month = new System.Text.RegularExpressions.Regex("([^A-Z]*)([a-zA-Z]*)").Match(Path.GetFileNameWithoutExtension(item).Split('.').Last()).Groups[2].Value; Commented Dec 11, 2021 at 21:49

1 Answer 1

2

It's possible to use regular expressions here to find month's name in the file name:

string pattern = @"([a-zA-Z]+)\d+.pdf";  // finds a word with digits and .pdf in the end

  RegexOptions options = RegexOptions.Multiline;
 foreach (var item in enumDir)
        {
            var matches = Regex.Matches(item, pattern, options);
            var month = matches.FirstOrDefault()?.Groups[1].Value;
        }
       
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.