Member_2_7967119
asked on
How to parse the string and split the sub directory names in asp.net
I have a file location provided to me which I need to process the files found in the individual sub directories.
For eg: C:\Files\1\2\3\4
The sub directories limits to 4 and the file can reside in any one of the sub directories.
What I am looking for is that I need to read the path information like the following and get the first sub directory name, followed by second, third and fourth.
C:\Files\10\24\36\4\111.pd f
First sub directory 10
Secondary is 24
Third is 36
Fourth is 4
and the file is 111.pdf.
The files name can be different and the sub directories can also be of different length.
How can i parse the string in ASP.net
For eg: C:\Files\1\2\3\4
The sub directories limits to 4 and the file can reside in any one of the sub directories.
What I am looking for is that I need to read the path information like the following and get the first sub directory name, followed by second, third and fourth.
C:\Files\10\24\36\4\111.pd
First sub directory 10
Secondary is 24
Third is 36
Fourth is 4
and the file is 111.pdf.
The files name can be different and the sub directories can also be of different length.
How can i parse the string in ASP.net
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Try this one with regular expression
string input = @"C:\files\10\24\36\4\111. pdf";
MatchCollection matches = new Regex(@"(?<=([\\]))(.*?)(? =[\\]|$)", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.CultureInvari ant).Match es(input);
string output = string.Empty;
foreach(Match match in matches )
{
output = output + " "+ match.Value;
Console.WriteLine(output);
}
Console.Read();
Try this code block and regular expression. I guess you will get your desired output.
string input = @"C:\files\10\24\36\4\111.
MatchCollection matches = new Regex(@"(?<=([\\]))(.*?)(?
string output = string.Empty;
foreach(Match match in matches )
{
output = output + " "+ match.Value;
Console.WriteLine(output);
}
Console.Read();
Try this code block and regular expression. I guess you will get your desired output.
you can achieve your purpose in foreach loop for matches found
Why to do some many complex things when simple split works.
I've made it a habit to use Path whenever dealing with paths and files. It also handles UNC paths, and is really a no-brainer making you play safe.
Also note, that the simple Split will leave exact specification of the possible folders as well as the filename. Not that it will be difficult to expand the code to return these, but quite fast the "simple" becomes "less simple" and may require a comment line or two where the few lines using Path is self-explanatory.
/gustav
Also note, that the simple Split will leave exact specification of the possible folders as well as the filename. Not that it will be difficult to expand the code to return these, but quite fast the "simple" becomes "less simple" and may require a comment line or two where the few lines using Path is self-explanatory.
/gustav
This is simply an extension of what Gustav Brock already posted. It combines the root and the first folder, then iterates over the rest of the folders to give you the search path at each level:
Output:
string fullPath = @"C:\Files\10\24\36\4\111.pdf";
string searchPath = Path.GetPathRoot(fullPath);
string[] folders = Path.GetDirectoryName(fullPath).Substring(searchPath.Length).Split(Path.DirectorySeparatorChar);
for(int i = 0; i < folders.Length; i++)
{
searchPath = Path.Combine(searchPath, folders[i]);
if (i > 0)
{
// ... do something with "searchPath" ...
Console.WriteLine(searchPath);
}
}
Output:
C:\Files\10
C:\Files\10\24
C:\Files\10\24\36
C:\Files\10\24\36\4
Open in new window
/gustav