DateTime stamp in output .zip file

1.9k views Asked by At

I have a String Path to output a .ZIP file String path = @"C:\TEMP\test.zip"; and I am looking to five the file name a date stamp. Example, test_TodayDate.ZIP. There's a way to achieve this?

Thanks

2

There are 2 answers

3
CriketerOnSO On BEST ANSWER

You can do:

string filePath = @"C:\TEMP\test.zip";
string finalPath = Path.Combine(Path.GetDirectoryName(filePath),
                            Path.GetFileNameWithoutExtension(filePath) 
                                  + DateTime.Now.ToString("yyyyMMddHHmmss") 
                                  + Path.GetExtension(filePath));
  • First Get File name without extension and add your Time Stamp, Then concatenate file extension,
  • Then get Current directory for the file path
  • Use Path.Combine to combine directory and new file name
1
Afzaal Ahmad Zeeshan On

You can create your own variable, like this,

// gets the file name without extension
var fileName = Path.GetFileNameWithoutExtension(path); 
// create the new file name
var newFileName = fileName + "_" + DateTime.Now + ".zip"; 

Now save the new file generated, and name this file as the newFileName it will have the DateTime in the name.