Question

I am new in programming and not so good about regex. I wish to load / read a csv File and then save in a txt File using the same name of csv File. I will give an example.

D:\Project\File\xxx.csv

After I load this file, I want to get the name "xxx" and save it in a txt file:

D:\Project\File\xxx.txt

Or maybe in another folder, for example:

D:\Project\Specifications\PersonInfo.csv

save to

D:\Project\DataBank\PersonInfo.txt
Was it helpful?

Solution

This can be accomplished in many ways.

Maybe what you're lacking is knowledge of the System.IO.Path class (MSDN article here).

For instance changing the extension could be accomplished like so:

string originalFilePath = @"D:\Project\File\xxx.csv";
string newFilePath = Path.ChangeExtension(originalFilePath, ".txt");

Note: You need to explicitate the leading dot (".") for the extension.

Here's some "Path algebra" fun you could combine to create your desired effects:

string originalFilePath = @"D:\Project\File\xxx.csv";
string thePath = Path.GetDirectoryName(originalFilePath);
// will be @"D:\Project\File"

string filename = Path.GetFileName(originalFilePath);
// will be "xxx.csv"

string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(originalFilePath);
// will be "xxx"

string recombinedFilePath = Path.Combine( @"D:\OtherFolder", "somethingElse.txt" );
// will be @"D:\OtherFolder\somethingElse.txt"

Note: Path.Combine knows how to handle extra/missing leading/trailing backslashes.

For example:

  • Path.Combine(@"D:\MyFolder1", @"MyFolder2\MyFile.txt")
  • Path.Combine(@"D:\MyFolder1\", @"MyFolder2\MyFile.txt")
  • Path.Combine(@"D:\MyFolder1", @"\MyFolder2\MyFile.txt")
  • Path.Combine(@"D:\MyFolder1\", @"\MyFolder2\MyFile.txt")

will all yield the same result: @"D:\MyFolder1\MyFolder2\MyFile.txt"

OTHER TIPS

You do not need regex for that, because .NET provides a System.IO.Path class for dealing specifically with file name manipulations.

For example, to replace .csv with .txt you can use this call:

var csvPath = @"D:\Project\File\xxx.csv";
var txtPath = Path.Combine(
    Path.GetDirectoryName(csvPath)
,   Path.GetFileNameWithoutExtension(csvPath)+".txt"
);

You use a similar trick to replace other parts of the file path. Here is how you change the name of the top directory:

var csvPath = @"D:\Project\Specifications\xxx.csv";
var txtPath = Path.Combine(
    Path.GetDirectoryName(Path.GetDirectoryName(csvPath))
,   "DataBank"
,   Path.GetFileNameWithoutExtension(csvPath)+".txt"
);

You don't need Regex.

You can use Path.GetFileName or Path.GetFileNameWithoutExtension:

string fileName = Path.GetFileNameWithoutExtension("D:\Project\Specifications\PersonInfo.csv");

If you want to use regex for this, this regex will get the part you want:

([^\\]+)\.[^.\\]+$

The first group (in the parentheses) matches one or more characters (as many as possible) which is not a backslash. Then there need to be a literal dot. Then one or more characters (as many as possible) that are not a dot or backslash, then the end of the string. The group captures the wanted part.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top