Using the Current Date in Windows Batch Files


Problem: We want to be able to archive files by date created, for example

  ren somefile.txt somefile.20050223.txt

Solution: use the %DATE% variable and extract the interesting bits

    c:\> echo %DATE%
    Wed 02/23/2005
    c:\> echo %TIME%
     8:03:48.74

Examples: from my workstation, using Win2K and whatever my regional settings currently are (note: mileage may vary w/ OS and settings, so probably a good place for some testing before productive use :-)

        mm:  SET CURDATE=%DATE:~4,2%
        dd:  SET CURDATE=%DATE:~7,2%
      mmdd:  SET CURDATE=%DATE:~4,2%%DATE:~7,2%
        yy:  SET CURDATE=%DATE:~10,2%
      yyyy:  SET CURDATE=%DATE:~10,4%
    mmddyy:  SET CURDATE=%DATE:~4,2%%DATE:~7,2%%DATE:~10,2%
  mmddyyyy:  SET CURDATE=%DATE:~4,2%%DATE:~7,2%%DATE:~10,4%

  then, just ren somefile.txt somefile.%CURDATE%.txt


keywords: windows, command line, command.com, cmd.exe, batch file, rename, copy, move, save
date: 02/23/2005 (revised 05/03/2005)