Link to home
Start Free TrialLog in
Avatar of Member_2_7967119
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.pdf

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
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Gustav Brock
You can use Path for this:
string fullPath = @"C:\Files\10\24\36\4\111.pdf";

string root = Path.GetPathRoot(fullPath);
string[] folders = Path.GetDirectoryName(fullPath).Substring(root.Length).Split(Path.DirectorySeparatorChar);
string file = Path.GetFileName(fullPath);

for (int i = 0; i < folders.Length; i++)
{
	Console.WriteLine("Folder {0}: {1}", i, folders[i]);
}
Console.WriteLine("File: " + file);

Output:

Folder 0: Files
Folder 1: 10
Folder 2: 24
Folder 3: 36
Folder 4: 4
File: 111.pdf

Open in new window

/gustav
Avatar of Roshan Jagtap
Roshan Jagtap

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.CultureInvariant).Matches(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.
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
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:
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);
	}
}

Open in new window


Output:
C:\Files\10
C:\Files\10\24
C:\Files\10\24\36
C:\Files\10\24\36\4

Open in new window