Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. I have a project and it requires me to search a number from a name and put it into my filepath. For example:

The name is LU610364G02

and the filepath should be: ..\Reports\2016\10\LU610364\

where 2016 is the 3rd number from LU610364G02, 10 is the 4th and 5th number and LU610364 is the first 8 numbers.

How do I search the values and put it into the filepath?
I am using Microsoft Visual Studio, c#.
Thank you in advance for the help

What I have tried:

Tried using Regex as an expression to match the name to the filepath.

<pre lang="c#"> string Timestamp = string.Empty;
            using (var file = System.IO.File.OpenText(datFile))
            {
                //regexs = regular expressions
                //compile regexs
                //Regex nodeRegex = new Regex("NODE      :(.*)");
                //Regex testModeRegex = new Regex("TEST MODE:(.*)");
                //Regex timestampRegex = new Regex("Timestamp:(.*)");

                Regex nodeRegex = new Regex("NODE      :(.*)", RegexOptions.IgnoreCase);
                Regex testModeRegex = new Regex("\bTest Mode\b", RegexOptions.IgnoreCase);
                Regex program = new Regex("\bPROGRAM\b", RegexOptions.IgnoreCase);
                Regex TimeStamp = new Regex("\bTIMESTAMP\b", RegexOptions.IgnoreCase);

                while (!file.EndOfStream)
                {
                    String line = file.ReadLine();

                    // check name
                    Match m = nodeRegex.Match(line);


                    //string line = "TUI Test Mode: B31P";
                    string match = string.Format(@"\b{0}\b", FAR_Yield_Analysis_Test_Tool.Properties.Settings.Default.RegexTestMode);
                    bool matchTestMode = Regex.IsMatch(line, match, RegexOptions.IgnoreCase | RegexOptions.Singleline);
                    Match matchTest = Regex.Match(line, FAR_Yield_Analysis_Test_Tool.Properties.Settings.Default.RegexTestMode);
                    Match matchProgram = Regex.Match(line, FAR_Yield_Analysis_Test_Tool.Properties.Settings.Default.RegexPROGRAM);
                    Match matchTimestamp = Regex.Match(line, FAR_Yield_Analysis_Test_Tool.Properties.Settings.Default.RegexTimestamp);


                    if (matchTest.Success)
                    {
                        int startIndex = matchTest.Index + FAR_Yield_Analysis_Test_Tool.Properties.Settings.Default.RegexTestMode.Length + 1;
                        int endIndex = line.IndexOf(' ', startIndex + 1);

                        TestMode = line.Substring(startIndex, endIndex - startIndex).Trim();
                        //break;
                    }

                    if (matchProgram.Success)
                    {
                        int startIndex = matchProgram.Index + FAR_Yield_Analysis_Test_Tool.Properties.Settings.Default.RegexTestMode.Length + 1;
                        int endIndex = line.IndexOf(' ', startIndex + 1);

                        strProgram = line.Substring(startIndex, endIndex - startIndex).Trim();
                        //break;
                    }

                    if (matchTimestamp.Success)
                    {
                        int startIndex = matchTimestamp.Index + FAR_Yield_Analysis_Test_Tool.Properties.Settings.Default.RegexTimestamp.Length + 1;
                        int endIndex = line.IndexOf(' ', startIndex + 1);

                        strTimestamp = line.Substring(startIndex, endIndex - startIndex).Trim();
                        //break;
Posted
Updated 31-Jan-18 17:43pm
v2
Comments
PIEBALDconsult 31-Jan-18 21:48pm    
Regex replace? Can't try it right now, but it should be fairly easy.
Patrice T 31-Jan-18 22:18pm    
and you have some code ?

C#
string nam = "LU610364G02" ;
string pth = System.Text.RegularExpressions.Regex.Replace ( nam , @"^(..(.)(..)...).*$" , @"..\Reports\201$2\$3\$1\" ) ;
 
Share this answer
 
Comments
Member 13650651 1-Feb-18 1:10am    
thank you so much!
try
string name = "LU610364G02";
       if (name.Length > 8) {
           string thirdChar = name.Substring(2, 1);
           string year = "201" + thirdChar;

           string fourAndfifth = name.Substring(3, 2);
           string month = fourAndfifth;

           string first8Chars = name.Substring(0, 8);

           string path = string.Format(@"..\Reports\{0}\{1}\{2}\", year, month, first8Chars); //..\Reports\2016\10\LU610364\

       }
 
Share this answer
 
Comments
Member 13650651 1-Feb-18 1:10am    
Thank you so much!
Karthik_Mahalingam 1-Feb-18 1:23am    
welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900