-1
D:\> set "today=%date:~10,4%-%date:~7,2%-%date:~4,2%"

D:\> mkdir SVN_BACKUP_DUMP_TEST\%today%

D:\> CD SVN_BACKUP_DUMP_TEST\%today%

when we are make bat file this command not work

1 Answers1

0

Format of date of referenced environment variable DATE depends on region setting, i.e. which country and derived from country which date format is configured for the current account. So which string is assigned to environment variable today and if that string is valid for a directory or file name depends on account specific date format.

On my computer with German set as country for my account output of echo %DATE% is 13.11.2017. So the date format is dd.MM.yyyy with point as separator, no weekday and always two digits for date and month even for a date or month less than 10.

The command line

echo %DATE:~10,4%-%DATE:~7,2%-%DATE:~4,2%

produces on my machine with my account and my region settings the output:

-01-1.

That is definitely not the current date in format yyyy-MM-dd.

The solution is using following batch code:

@echo off
for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "Today=%%I"
set "Today=%Today:~0,4%-%Today:~4,2%-%Today:~6,2%"
mkdir "SVN_BACKUP_DUMP_TEST\%Today%" 2>nul
cd "SVN_BACKUP_DUMP_TEST\%Today%"

This is a region independent solution. The batch code is completely explained in answer on
Why does %date% produce a different result in batch file executed as scheduled task?

What does %date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2% mean? is one more question on which answer should be read to understand the string substitutions. And run in a command prompt window set /? to get displayed the help for command SET explaining string substitution, too.

Mofi
  • 38,783
  • 14
  • 62
  • 115