Scripting: How to Programmatically Add Current Date/Time to a Filename

When writing scripts such as batch files you may want to auto-create a file (like a text file) to save the log of a certain command (such as DCDiag or NetDiag). If your script is going to run on a routine interval, and you want to maintain a history of logs, it may be helpful to incorporate the date/time into the filename.

Fortunately, Microsoft has given us some syntax to be able to do this from the command line.

Before we get to the actual commands, let’s review the pertinent information. If you go to a command line and type “Echo %date%” you’ll get back a response such as:

FRI 01/01/2010

That is a collection of fourteen characters, represented as such:

CHAR F R I _ 0 1 / 0 1 / 2 0 1 0

POS 00 01 02 03 04 05 06 07 08 09 10 11 12 13

In addition, if you go to a command line and type “Echo %time%” you’ll get back a response such as:

01:00:00.00

That is a collection of eleven characters, represented as such:

CHAR 0 1 : 0 0 : 0 0 . 0 0

POS 00 01 02 03 04 05 06 07 08 09 10

Now that we know what character appears at what position, we are ready to augment our file name with the proper syntax to incorporate the date and time.

For the date, we’re going to use “%date:~x,y%” where “x” equals the position and “y” equals the substring length.

So, if I wanted my file name to be “test-01-01-2010” I would use:

Test-%date:~4,2%-%date:~7,2%-%date:~10,4%.txt

Or, if I just wanted it to be “test-2010” I would use:

Test-%date:~10,4%.txt

Saying it another way, that “10” represents position 10 (the start of the year) and that “4” tells it to use four characters from that spot, so I get the entire four digit year (or 2010). If I just wanted a two digit year, I would change the “10” to “12” (to start at position 12) and the “4” to a “2” resulting in “10” instead of “2010.”

Time works much the same way, but with the syntax being “ %time:~x,y%” instead.

In case you have a need, you can use a minus sign to the right of the tilde (i.e. %date:~-x,y%) to count the x backwards. Don’t forget though, that the spaces, colons, dots and slashes will all be included in whichever direction you initiate your count.

About Rebecca Harness

Rebecca Harness is a Business Information Security Officer (BISO) for a publicly-traded, global information solutions company. As BISO, she champions security initiatives and recommends strategies to mitigate risk, facilitating innovation and new product development. She’s also responsible for representing the business unit’s security program in client facing engagements, conferences, and industry forums. Prior to her current role, she was an influential cybersecurity leader for one of the world’s largest transportation providers, known for transforming information security efforts into well-orchestrated programs. There, she developed an innovative methodology for delivering key information security priorities as a service model, leading to quicker adoption enterprise-wide while significantly reducing operational costs. She also led and modernized their global, multi-brand PCI Assessment and other compliance initiatives. In the early 2000’s, Rebecca developed one of St. Louis' leading Managed Services Providers from a startup in a spare bedroom into a mature consulting company with 30+ employees and 150+ clients in the Greater St. Louis Area. Rebecca holds many certifications, including; ISACA Certified Information Systems Auditor (CISA); ISC2 Certified Information Systems Security Professional (CISSP); and GIAC Security Leadership Certified (GSLC). She’s also a proud alumni of Hastings College and a longtime member of the Society of American Magicians.

No comments yet... Be the first to leave a reply!

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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