Batch Files – Create New Folder Using Current Date and Time in Folder Name

Share on LinkedIn

This would be great for backup jobs or copying files where you want to keep track of when it was done.

In my simple batch file, I’m copying contents from a mapped network drive to my local machine.

Originally, I was using the following command:

xcopy S: C:\Users\james\Documents\Backup_WIN_Server\SysAdmin /c /v /f /i /s /e /h /y

If using this for a simple form of backup, the data will be overwritten each time I run the batch file.

In order to create a new directory each time with a time/date stamp, all I need to do isĀ use a substring and the built-in %DATE% and %TIME% variables. In this example, I’m just adding the substring before my xcopy command.

@echo OFF
:: Use date /t and time /t from the command line to get the format of your date and
:: time; change the substring below as needed.
:: This will create a timestamp like yyyy-mm-dd-hh-mm-ss.
set TIMESTAMP=%DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2%-%TIME:~0,2%-%TIME:~3,2%-%TIME:~6,2%


Finally, I just need to add that timestamp to my destination folder as such:

xcopy S: C:\Users\james\Documents\Backup_WIN_Server\"%1\%TIMESTAMP%-SysAdmin" /c /v /f /i /s /e /h /y
Posted in Windows SysAdmin and tagged , , , , , , .