Run this batch code and you can see how to get current date assigned to environment variable FolderDate
for usage in name of backup folder.
@echo off
rem Get the current local date and time in a region and language
rem independent format by using Windows Management Instrumentation.
for /F "skip=1 delims=." %%T in ('%SystemRoot%\System32\wbem\wmic.exe OS get localdatetime') do set "LocalDateTime=%%T" & goto ReformatDate
rem Convert the date and time string in format yyyymmddhhmmss like
rem 20151231150357 to a readable format with date only like 2015-12-31.
:ReformatDate
set "FolderDate=%LocalDateTime:~0,4%-%LocalDateTime:~4,2%-%LocalDateTime:~6,2%"
rem Output the date string for the backup folder and delete the 2 variables.
echo Date for backup folder is: %FolderDate%
echo.
set "LocalDateTime="
set "FolderDate="
pause
Read for example answer on Find out if file is older than 4 hours in Batch file and the answers on Batch file to delete files older than N days for details about getting and reformatting date and time string.
The usage of value of environment variable DATE reformatted for the folder name to format yyyy-mm-dd
would be much faster than using wmic
. But using DATE requires knowledge of date format on used machine because of depending on region and language settings.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
for /?
pause /?
rem /?
set /?
wmic /?
It is also advisable to run wmic OS get localdatetime
from within a command line window to see the output of this command line being processed by command for and finally resulting in the date string in format yyyy-mm-dd
for usage in name of backup folder.