Matthew Verstraete
Matthew Verstraete

Reputation: 6781

Adding DateTime to filename during rename

I have found lots of examples of people creating new files and adding the current Datetime then the file extension but what I want to do is look to see if a file currently exists and if it does simply add the current DateTime to the file name but I can't figure out how to maintain the file extension. My current code so far:

public class FileUploadHelper
{
    private CoreSiteContext db = new CoreSiteContext();

    public Int64 UploadSiteImage(string ContainerName, string NewFileName, HttpPostedFile UploadedFile)
    {
        string SavePath = @"F:\FFInfoImages\" + ContainerName + @"\";

        if (System.IO.File.Exists(SavePath + NewFileName))
        {
            System.IO.File.Move(SavePath + NewFileName, SavePath + NewFileName + DateTime.Now.ToString("MM_dd_yyyy_hh_mm_ss"));
            UploadedFile.SaveAs(SavePath + NewFileName);
        }
        else
        {
            UploadedFile.SaveAs(SavePath + NewFileName);
        }

        using (db)
        {
            File NewFile = new File()
            {
                FileName = NewFileName,
                ContentType = UploadedFile.ContentType
            };

            db.Files.Add(NewFile);
            db.SaveChanges();
            return NewFile.ID;
        }
    }
}

Upvotes: 1

Views: 6486

Answers (4)

Zec
Zec

Reputation: 843

Appears as if the NewFileName string variable does not get passed in with the filename extension, otherwise most of this should work. Why not get the extension from UploadedFile?

string strNewPath = SavePath + NewFileName + Path.GetExtension(UploadedFile.FileName);

if (System.IO.File.Exists(strNewPath)) {
  System.IO.File.Move(strNewPath, SavePath + NewFileName + DateTime.Now.ToString("MM_dd_yyyy_hh_mm_ss") + Path.GetExtension(UploadedFile.FileName));
  UploadedFile.SaveAs(strNewPath);
}
else {
  UploadedFile.SaveAs(strNewPath);
}

using (db) {
  File NewFile = new File() {
    FileName = NewFileName + Path.GetExtension(UploadedFile.FileName),
    ContentType = UploadedFile.ContentType
  };

  db.Files.Add(NewFile);
  db.SaveChanges();

  return NewFile.ID;
}

Upvotes: 4

Transcendent
Transcendent

Reputation: 5755

The following method totally solves your problem

System.IO.Path.GetExtension("Path");

You'd better get the current file name without extension first using System.IO.Path.GetFileNameWithoutExtension("Path") then add the Date Time and then add up the extension anyway.

Upvotes: 2

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

you can use Path.GetExtension() method to identify the file extension.

Try This:

   String strExtension=IO.Path.GetExtension(SavePath + NewFileName);  
   if (System.IO.File.Exists(SavePath + NewFileName))
    {
        System.IO.File.Move(SavePath + NewFileName, SavePath + NewFileName +  DateTime.Now.ToString("MM_dd_yyyy_hh_mm_ss")+strExtension);
        UploadedFile.SaveAs(SavePath + NewFileName);
    }
    else
    {
        UploadedFile.SaveAs(SavePath + NewFileName);
    }

Upvotes: 1

Grant Winney
Grant Winney

Reputation: 66449

.NET has built-in methods for safely extracting the different portions of a file name (the file's name and it's extension, respectively). Path exists in the System.IO namespace.

Assuming NewFileName is something like myfilename.txt, you could use it like this (untested):

if (File.Exists(SavePath + NewFileName))
{
    var name = Path.GetFileNameWithoutExtension(NewFileName);
    var ext = Path.GetExtension(NewFileName);

    File.Move(SavePath + NewFileName,
        SavePath + name + DateTime.Now.ToString("MM_dd_yyyy_hh_mm_ss") + ext);
}
UploadedFile.SaveAs(SavePath + NewFileName);

Upvotes: 4

Related Questions