none
Extract Name from Path RRS feed

  • Question

  • Hi Experts, 

    I have multiple file paths that are based on the Server Name like below

    ServerName = 'ZKHG-01-2019'
    
    C:\Output\ZKHG-01-2019\Files\DBName
    
    C:\Input\ZKHG-01-2019_1\Files\DBName
    
    C:\Input\backup\processing\C:\Input\ZKHG-01-2019_02\
    
    D:\Output\Requesting\ServerFiles\ZKHG-01-2019_03\
    
    
    

    Could you please support with a method that helps me extract the Server Name or Server Name matching text. Output below

    Output
    
    ZKHG-01-2019
    ZKHG-01-2019_1
    ZKHG-01-2019_02
    ZKHG-01-2019_03

    Thanks

    Priya

    Sunday, January 19, 2020 12:51 PM

Answers

  • The following provides the token if found.

    var serverName = "ZKHG-01-2019";
    var list = new List<string>()
    {
        "C:\\Output\\ZKHG-01-\\Files\\DBName",
        "C:\\Output\\ZKHG-01-2019\\Files\\DBName",
        "C:\\Input\\ZKHG-01-2019_1\\Files\\DBName",
        "C:\\Input\\backup\\processing\\C:\\Input\\ZKHG-01-2019_02\\",
        "D:\\Output\\Requesting\\ServerFiles\\ZKHG-01-2019_03\\"
    };
    
    
    foreach (var item in list)
    {
        var results = item.Split('\\').Select((x,i) => new {Item = x})
            .FirstOrDefault(data => data.Item.Contains(serverName));
    
        Console.WriteLine(results != null ? $"{results.Item}" : $"{serverName} not found in {item}");
    }

    ZKHG-01-2019 not found in C:\Output\ZKHG-01-\Files\DBName
    ZKHG-01-2019
    ZKHG-01-2019_1
    ZKHG-01-2019_02
    ZKHG-01-2019_03


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Priya Bange Monday, January 20, 2020 3:19 AM
    Sunday, January 19, 2020 1:41 PM
    Moderator
  • If you have various server names, then check this example too:

    string path = @"C:\Input\ZKHG-01-2019_1\Files\DBName";
    string servername = Regex.Match( path, @"(?<=\\|/|:|^)\w+-\d{2}-\d{4}(?:_\d+)?(?=\\|/|$)" ).Value;
     
    Console.WriteLine( path );
    Console.WriteLine( servername );


    • Marked as answer by Priya Bange Monday, January 20, 2020 3:19 AM
    Sunday, January 19, 2020 3:01 PM

All replies

  • The following provides the token if found.

    var serverName = "ZKHG-01-2019";
    var list = new List<string>()
    {
        "C:\\Output\\ZKHG-01-\\Files\\DBName",
        "C:\\Output\\ZKHG-01-2019\\Files\\DBName",
        "C:\\Input\\ZKHG-01-2019_1\\Files\\DBName",
        "C:\\Input\\backup\\processing\\C:\\Input\\ZKHG-01-2019_02\\",
        "D:\\Output\\Requesting\\ServerFiles\\ZKHG-01-2019_03\\"
    };
    
    
    foreach (var item in list)
    {
        var results = item.Split('\\').Select((x,i) => new {Item = x})
            .FirstOrDefault(data => data.Item.Contains(serverName));
    
        Console.WriteLine(results != null ? $"{results.Item}" : $"{serverName} not found in {item}");
    }

    ZKHG-01-2019 not found in C:\Output\ZKHG-01-\Files\DBName
    ZKHG-01-2019
    ZKHG-01-2019_1
    ZKHG-01-2019_02
    ZKHG-01-2019_03


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Priya Bange Monday, January 20, 2020 3:19 AM
    Sunday, January 19, 2020 1:41 PM
    Moderator
  • If you have various server names, then check this example too:

    string path = @"C:\Input\ZKHG-01-2019_1\Files\DBName";
    string servername = Regex.Match( path, @"(?<=\\|/|:|^)\w+-\d{2}-\d{4}(?:_\d+)?(?=\\|/|$)" ).Value;
     
    Console.WriteLine( path );
    Console.WriteLine( servername );


    • Marked as answer by Priya Bange Monday, January 20, 2020 3:19 AM
    Sunday, January 19, 2020 3:01 PM