Thursday, February 4, 2010

DOS and DATES

Have you ever needed a date at the command prompt but need it in a different format? Sometimes the date a file was created or modified just isn't what you need. SQL, for instance, names its files with yyyymmdd date format in the name. Well, here's how to take a date and reformat it for use in your DOS batch files:


FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B 
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %CDATE%') DO SET dd=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('echo %CDATE%') DO SET yyyy=%%B
SET DATE=%yyyy%%mm%%dd%


Of course you can set the DATE environment variable we created in any format you need.  Here's a practical application to copy SQL's dated backup files to a DRP location:

robocopy \\Server1\SAN_DBData d:\nightlyBACKUPS *%DATE%*.BAK /s /r:0 /w:0 /nfl /ndl /np /log+:CopyNightlyDBBackups.LOG


No comments:

Post a Comment