Adding Date and Timestamp to a Filename

Sometimes you have to create output files and you want to dynamically create specific values as part of the output file. Sometimes it is important to append time and date to the name of a file. I was recently involved in  project to output a specific file before and after a specific event happened on a server. Not only did i want to control the text added to the file name, but I wanted to include the date and time in the file name of each log file.

Generally, the solution is quite simple — we should use the %date% and %time% built-in variables, that display date and time based on the regional coding. In my case, the variables returned the following values:

To include date:

command > "C:\textfile_%date%.log”

To include time:

command > "C:\textfile_%time%.log”

To include date and time:

command > "C:\textfile_%date%_%time%.log”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.