Search code examples
dosbatch-file

how to set date to current date using dos batch file command


how to set date to current date using dos batch file command.


Solution

  • If you need to use the current date in a batch file, the variable %date% has the current date:

    echo %date%
    23/02/2010
    

    It uses the format of the regional setting of your computer. In my computer it's dd/mm/yyyy.

    Since the / can't be part of a file name, they must be replaced with a safe character or nothing:

    echo %date:/=-%
    23-02-2010
    
    echo %date:/=%
    23022010
    

    If you want to create a backup copy of a file, you can do something like:

    copy file.txt file-%date:/=%.txt
    dir /b file*.*
    file-23022010.txt
    file.txt
    

    Or first set it to a variable and then use it:

    set currdate=%date:/=%
    copy file.txt file-%currdate%.txt