Windows 7 and Server 2008 DOS Batch File Date Tip….

May 31st, 2012 by Jeremy Pavlov | Filed under Microsoft, Scripting, Windows 7.

No matter what people think, DOS batch scripts are just as alive and needed as ever.  And for all the tasks we create on Windows servers, we still commonly need to gather application output, rotate logs, etc.

So when running a script that exports results to a log/results file, I always prefer to date my file names for easy tracking and history.  My preferred date arrangement for filenames is the date in reverse format: YYYYMMDD, or 20120531.  And since the DOS date command isn’t as friendly or flexible as the Linux/Unix date command (with which you may easily format the output in myriad ways), it’s best to do the next-best thing: use the date *variable*.

Most folks don’t even know that your Windows system is keeping a real-time variable for the date, but it certainly makes sense that it would be necessary.  Go ahead, pull up a command prompt and type: echo %date%

…and on Windows 7 and Server 2008 (for XP, please see this post), you’ll get a result like this:  Wed 05/31/2012

 So what we need to do now is to utilize string manipulation, grab the date elements we want, and flip the order around to get the arrangement we need:
set todaysdate=%date:~10,4%%date:~4,2%%date:~7,2%

 The structure above is this:  There are 3 sections, all of which use the date variable.  The first section moves in 10 characters, and grabs 4.  The second moves in 4 characters and grabs 2, and the third moves in 7 and grabs 2.  As a result, we have reverse-date!

Then set a variable to use the combined date variable in the filename.

set exportfile=C:\temp\export\myexport-%todaysdate%.txt

…now just call the %exportfile% in your batch file when you redirect your output, and viola!!  Here’s what it looks like when you echo %exportfile%:

C:\temp\export\myexport-20120531.txt

Enjoy…

8-)

(Updated 20120809 for OS version clarification)

Did you like this? Share it:

Tags: , , ,


2 Responses to “Windows 7 and Server 2008 DOS Batch File Date Tip….”

  1. Paul says:

    Hi, I am trying to create a batch job that copies a set of files into a new folder each day however if I run the batch on windows 2008 server it creates sub folders for the dd-mm-yy values but on xp it creates it correct 20120801
    D:\tempbackup\3 incorrect

    @echo off
    set date=%date:~10,4%%date:~4,2%%date:~7,2%
    md D:\tempbackup\%date%
    xcopy /e \\SADISTIX\BATCH\DATA\ETL\*.* D:\tempbackup\%date% /Y

    END

    • Jeremy Pavlov says:

      @ Paul –

      A couple thoughts here…
      1.) I wouldn’t use “date” as the variable name. Call me superstitious, but I never like to use system names (variables, executables, etc.) as my variable names. That’s why I chose “todaysdate” in my exmaples.
      2.) I’m sorry to say, I simply don’t have a Server 2008 instance anywhere with which to test. However, I tested my examples above on Server 2008 R2, and they work perfectly. I’d suggest you “echo %date%” in your example after the “set date=” command to see what you have at that point.
      3.) For pete’s sake, use robocopy… ;)

      I hope that helps a little bit, or at least gets you started… 8-)

Leave a Reply