Click here to Skip to main content
15,888,733 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am new to C# and coding in general and have come across a problem that I could use some help with. There is a project I am working on where I have to loop through a folder structure that contains many subfolders and files in them. This is the easy part. Where I am having the issue is that I need to pull some information out of the current or previous folder name that the file resides in. I am not sure how to filter out and verify the information I need in these folder names.

ex
C:\Project\2011\43\1111$11$1 Word Document\687\filename.pdf
C:\Project\2011\55\1234-54-5\1234-54-5 Excel Document\filename.pdf
C:\Project\2011\23\5555$44$5\5555$44$5 A333XP Another Type of Document\filename.pdf

The information I need to gather from these is the 5555$44$5 (can also be in 1234-54-5 format) and the information after such as word document, excel document, etc. However I do not need the A333XP information that appears in some of the folder names. There is at least some standardization in in that the elements that I need have a structure.

5555$44$5 (4 numbers, $ or -, 2 numbers, $ or -, 1 number)
A333XP (1 letter, 3 numbers, 2 letters)

Once I pull out those I need to gather the rest of the name for the last field of information I need.

My question is how would I go about taking this on to get the fields that I need separated?
Posted
Comments
ridoy 7-Nov-13 9:50am    
use regex.

I would go for a regular expression to filter out the directory-names parts:
C#
using System.Text.RegularExpressions;

Regex r = new Regex(@"^(?<Part1>[\d]{4}[-$][\d]{2}[-$][\d]{1})\s(?<Part2>[\w][\d]{3}[\w]{2})\.*$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
string example = "5555$44$5 A333XP Another Type of Document";
Match m = r.Match(example);
string part1 = m.Groups["Part1"]; // part1 == "5555$44$5"
string part2 = m.Groups["Part2"]; // part2 == "A333XP"


You can use Expresso[^] if you want to learn/build complex regular expressions.

Hope this helps.
 
Share this answer
 
v5
Comments
trkghost 7-Nov-13 15:42pm    
Thanks for the help. I had contemplated regular expressions but I was not sure on how to approach it. I have played with Expresso and was having issues building the expression needed for this. Also was not sure if that was the best approach.
phil.o 12-Nov-13 5:44am    
For this kind of requirement (fixed string of which you know the syntax in the first place), regular expressions are THE way to go.
Cheers.
If the element you need is always the first word of the folder name then you can get the string that represents that text and split it by the empty character, that will give you an array with every word separated. If you want to recognize an specific format then you can use regular expressions, C# implements this in the namespace RegExp.
 
Share this answer
 
Comments
trkghost 7-Nov-13 15:45pm    
Think Regular expressions is going to be the best way to do this since the information I am needing to gather is in a few different formats. Thanks for the help!
Arnaldo P. Castaño 11-Nov-13 13:40pm    
ok, anything I can do to help.
trkghost 11-Nov-13 14:14pm    
Thanks. I have a couple of the expressions built so far to find the information I am looking for.

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