Steve W
Steve W

Reputation: 1128

Insert string into a filepath string before the file extension C#

I have a string that defines the path of a file:

string duplicateFilePath = D:\User\Documents\processed\duplicate_files\file1.jpg;

I am going to move a file to this location but sometimes a file with the identical name exists all ready. In this case I want to differentiate the filename. I have the crc value of each file available so I figured that may be good to use to ensure individual file names. I can create:

string duplicateFilePathWithCrc = duplicateFilePath + "(" + crcValue + ")";

But this gives:

D:\User\Documents\processed\duplicate_files\file1.jpg(crcvalue);

and I need:

D:\User\Documents\processed\duplicate_files\file1(crcvalue).jpg;

How can I put the crcvalue into the string before the file extension, bearing in mind there could be other .'s in the file path and file extensions vary?

Upvotes: 19

Views: 18002

Answers (5)

YZN3RF
YZN3RF

Reputation: 103

This can also be achieved by using Replace:

using System.IO;

string duplicateFilePathWithCrc = duplicateFilePath.Replace(
    Path.GetFileNameWithoutExtension(duplicateFilePath),
    Path.GetFileNameWithoutExtension(duplicateFilePath) + "(" + crcValue + ")");

Upvotes: 2

László Koller
László Koller

Reputation: 1159

Try using the Path class (it's in the System.IO namespace):

string duplicateFilePathWithCrc = Path.Combine(
        Path.GetDirectoryName(duplicateFilePath),
        string.Format(
            "{0}({1}){2}",
            Path.GetFileNameWithoutExtension(duplicateFilePath),
            crcValue,
            Path.GetExtension(duplicateFilePath)
        )
    );

Upvotes: 0

Parimal Raj
Parimal Raj

Reputation: 20585

Something like this

string duplicateFilePath = @"D:\User\Documents\processed\duplicate_files\file1.jpg";
string crcValue = "ABCDEF";
string folder = Path.GetDirectoryName(duplicateFilePath);
string filename = Path.GetFileNameWithoutExtension(duplicateFilePath);
string extension = Path.GetExtension(duplicateFilePath);

string newFilename = String.Format("{0}({1}){2}", filename, crcValue, extension);
string path_with_crc = Path.Combine(folder,newFilename );

Upvotes: 1

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236248

int value = 42;
var path = @"D:\User\Documents\processed\duplicate_files\file1.jpg";
var fileName = String.Format("{0}({1}){2}", 
         Path.GetFileNameWithoutExtension(path), value, Path.GetExtension(path));
var result = Path.Combine(Path.GetDirectoryName(path), fileName); 

Result:

D:\User\Documents\processed\duplicate_files\file1(42).jpg

Upvotes: 7

Rotem
Rotem

Reputation: 21927

Use the static methods in the System.IO.Path class to split the filename and add a suffix before the extension.

string AddSuffix(string filename, string suffix)
{
    string fDir = Path.GetDirectoryName(filename);
    string fName = Path.GetFileNameWithoutExtension(filename);
    string fExt = Path.GetExtension(filename);
    return Path.Combine(fDir, String.Concat(fName, suffix, fExt));
}

string newFilename = AddSuffix(filename, String.Format("({0})", crcValue));

Upvotes: 51

Related Questions