1

I'm currently trying to add a DateTime stamp, a prefix and a unique number to a file name. My desired output is:

\ParentDirectory\Sub Directory\Another Sub Directory\Prefix- Unique Number - 11 29 2016 2 07 30 PM.xlsx

Prefix and Unique Number above will be passed into the function. I'm using the following method to achieve this:

public static string AppendDateTimeToFileName(this string fileName, string prefix, string uniqueNumber)
{
    return string.Concat(
        Path.GetFullPath(fileName),
        Path.Combine(prefix + " - " + uniqueNumber + " - "),
        Path.GetFileNameWithoutExtension(fileName),
        DateTime.Now.ToString()
        .Replace("/", " ")
        .Replace(":", " ")
        .Trim(),
        Path.GetExtension(fileName)
        );
}

I call the above method as:

string fileName = @"\\ParentDirectory\Sub Directory\Another Sub Directory\MyFile.xlsx";
string adjustedFileName = fileName.AppendDateTimeToFileName("Shipping Note", "0254900");

The output I receive is as follows:

\ParentDirectory\Sub Directory\Another Sub Directory\Shipping Note -\0254900 - 11 29 2016 2 08 10 PM

As you can see in the above output the string is incorrect, firstly I get an extra -\ and the file extension isn't coming through either. Can someone tell me where I'm going wrong please.

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
Izzy
  • 6,740
  • 7
  • 40
  • 84
  • 3
    Path.GetFullPath(fileName) returns the full path not the filename. – AnthonyLambert Nov 29 '16 at 14:29
  • @AnthonyLambert oh ok, but how comes I get an extra `-\` in my returned value? Also I want to return the full path along with the appended file because later on I save the file to the path with the new name – Izzy Nov 29 '16 at 14:31
  • You don't need `Path.Combine` just to concatenate the prefix and number together. `Path.Combine` is for combining parts of a path with the appropriate path separator. – juharr Nov 29 '16 at 14:36
  • Your method has an argument called `uniqueNumber`, but you use something called `rmaNumber` instead. When I fix that my result is "\\ParentDirectory\Sub Directory\Another Sub Directory\MyFile.xlsxShipping Note - 0254900 - MyFile11 29 2016 9 38 04 AM.xlsx" – juharr Nov 29 '16 at 14:37
  • @juharr Sorry that was a typo from when I was trying out a different param. – Izzy Nov 29 '16 at 14:39
  • Path.Combine(prefix + " *-* " + rmaNumber + " - "), I think because you have the `-` there? You ` \ ` : Here is your answer: It is not realy a \ which is added but a null character http://stackoverflow.com/questions/2292850/c-sharp-what-does-0-equate-to – TripleEEE Nov 29 '16 at 14:41
  • @juharr How come `MyFile.xlsx` isn't being appended? It'll work if it's just the file name but I need to return the entire directory path with it too – Izzy Nov 29 '16 at 14:42
  • You really should be able to pin the problem down to the individual part(s) that go into the `string.Concat` yourself... – C.Evenhuis Nov 29 '16 at 14:45
  • @Code Not sure why you got a result that was different, but as Anthony said `GetFullPath` will include the file name. – juharr Nov 29 '16 at 14:55

2 Answers2

2

Here's how I'd do it

public static string AppendDateTimeToFileName(this string fileName, string prefix, string uniqueNumber)
{
    return Path.Combine(
        Path.GetDirectoryName(fileName),
        string.Concat(
            prefix,
            " - ",
            uniqueNumber,
            " - ",
            Path.GetFileNameWithoutExtension(fileName),
            DateTime.Now.ToString("MM dd yyyy h mm ss tt"),
            Path.GetExtension(fileName)));
}

This correctly uses Path.Combine to combine the directory from Path.GetDirectoryName and you're new concatenated file name. Note I also used a date format string instead of the replacements. You might want to consider changing that format and putting a separator between the file name and the date.

juharr
  • 31,741
  • 4
  • 58
  • 93
1

The posted version of your code give the following output:

\ParentDirectory\Sub Directory\Another Sub Directory\MyFile.xlsxShipping Note - 0254900 - MyFile29 11 2016 15 46 48.xlsx

and NOT as you posted:

\ParentDirectory\Sub Directory\Another Sub Directory\Shipping Note -\0254900 - 11 29 2016 2 08 10 PM

if your desired output is:

\ParentDirectory\Sub Directory\Another Sub Directory\Prefix- Unique Number - 11 29 2016 2 07 30 PM.xlsx

You need to move the directory into the Path.Combine and use GetDirectoryName. Also remove the line:

Path.GetFileNameWithoutExtension(fileName)

since in your desired output I don't see the old file name "MyFile".
This code:

public static string AppendDateTimeToFileName(this string fileName, string prefix, string uniqueNumber)
{
    return string.Concat(               
        Path.Combine(Path.GetDirectoryName(fileName), prefix + " - " + uniqueNumber + " - "),
        DateTime.Now.ToString()
        .Replace(".", " ")
        .Replace(":", " ")
        .Trim(),
        Path.GetExtension(fileName)
        );
}

will yield the following output:

\ParentDirectory\Sub Directory\Another Sub Directory\Shipping Note - 0254900 - 29 11 2016 15 39 37.xlsx

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • I've just tried this and it works perfectly! Thank you. just a quick note I updated this line `.Replace(".", " ")` to `.Replace("/", " ")` – Izzy Nov 29 '16 at 14:46