Categories
Productivity Technology

Format Date in DOS Command Line

For any dos batch files that I run on a regular basis I commonly want to create a folder or a text file with the current date in the file name. I’m a purist and I like my dates formatted as YYYYMMDD so that an alphabetic sort is also chronological.

The following script will place the current date into a variable in the required format.

@echo off
for /F "tokens=1-4 delims=/ " %%A in ('date /t') do (
 set DateDay=%%A
 set DateMonth=%%B
 set DateYear=%%C
)
set CurrentDate=%DateYear%%DateMonth%%DateDay%
rem echo %DateDay%
rem echo %DateMonth%
rem echo %DateYear%
rem echo %CurrentDate%

https://github.com/RossGoodman/DOS/blob/master/Dates.cmd

You can then use the “CurrentDate variable within a folder or file name!

See also this script for a way to delete older folders created with these names.

Leave a Reply

Your email address will not be published. Required fields are marked *