3

I have a file name dayhappy_02_02345.csv

How do I get the 02 part out to be used in a variable and also how do I get the 02345 part so that I can pass these 2 values into a variable for a function.

Using c#.

I have looked at GetFileName but this gets either the filename, the extention or the full file name only.

Thanks

Ste

0

3 Answers 3

5

For that specific file name,

string sData = "dayhappy_02_02345.csv";
string[] sArr = sData.split('_');

string sPart1 = sArr[1];
string sPart2 = sArr[2];

Will do, but that's a special case, will work only on file names of this type

Sign up to request clarification or add additional context in comments.

4 Comments

I think your answer might work, but im using a Dts.Variable and the filename with its extension is located in the variable. Will this work? Can you simply replace string sData = Dts.Variables["filenameVariable"].Value. thanks for quick response
Ofcourse you can. Furthermore, you have to insert some validations in this code, which I haven't included in the example above
I have this at the moment: string fileName = Dts.Variables["csvFileNames"].Value; string[] sArray = fileName.Split('_'); string 02345Number = sArray[1]; string 02Number = sArray[2]; MessageBox.Show(02345Number); saying can not convert object to string. Im being really silly here I reckon and im missing syntax, but i have this: using Microsoft.SqlServer.Dts.Runtime; in the namespace so I dont know what would be wrong with this. Thanks for your help
Sorry I was being very stupid. I was missing a (). lol thanks
4

Get the file name as you've already figured out, then use String.Split() to get the individual pieces.

6 Comments

I would advise against using string methods, since change of format will force you to change the code, whereas with Regex you can make the string a configuration setting, and it's more flexible and powerful anyways.
@Mr.TA - Fair enough. I agree in general, but in this specific example, String.Split is sufficient. This is subjective, but I like cleaner simpler code, where possible. Also, this is obviously a very new developer, so I gave the simplest answer possible. Learning Regular Expressions is something that needs to be done, but does he need it right now, for this case? I was thinking baby steps before running. But again, I DO agree with what you say in general.
The filename keeps changing but the extension doesn't, also I have the file in a dts variable. is the Regex way the best way forward?
Judging by the other answers, the splitting is more popular, who am I to argue with the PEOPLE's choice :)
@Steven The "filename keeps changing" - meaning the format of it keeps changing?
|
3

You have to use Regex:

var match = new Regex(@".*_(\d+)_(\d+)").Match(Path.GetFileNameWithoutExtension(fileNAme));
var v02 = match.Groups[0].Value;
var v02345 = match.Groups[1].Value;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.