How to add the date to a file name

I would like to know how I can add the date and time to a file name when it is moved from one path to another.

Example:
Previous file name: “Name.xlsx”
File name when moving: “Name_2020_01_16__12_35.xlsx”

Thank you in advance.

3 Likes

name: “Name_” +DateTime.Now.ToString(“yyyy-MM-dd”)+“.xlsx”
or name = “Name” +DateTime.Now.ToString(“yyyy-MM-dd_HH-mm-ss”)+“.xlsx”

Regards, and welcome to our community!

7 Likes

Thank you for your answer. I forgot to clarify that the file can have different names and is not always called “Name”. How can I do it in that case?

store the name in a string variable, and just replace it in the expression like that:
FileName = YourVariableName + “_” +DateTime.Now.ToString(“yyyy_MM_dd”)+“.xlsx”

1 Like

@danielrtovar

with Path.GetFileNameWithoutExtension Method you will get the filename only

So give a try on:
RetrievedFileNameVar +“_”+DateTime.Now.ToString(“yyyy_MM_dd__hh_mm”)+“.xlsx”

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.