Why does %date% produce a different result in batch file executed as scheduled task?

The batch file is running robocopy and creating a log file of name robocopyServer_%date%.txt.

When the batch file is run manually, the result is robocopyServer_yyyy-mm-dd.txt

When the batch file runs from a scheduled task on Windows Server 2016, the result is robocopyServer_Tue (or whatever the day of the week is) and no file extension.

This batch file ran flawlessly on Windows Server 2012 R2 (manually or as scheduled task).


%date% or date /t isn't always reliable as it depends on region and user settings. Here's how you can get a universally valid date:

for /f %%# in ('wmic os get localdatetime^|findstr .') do if "%%#" neq "" set date=%%#
set date=%date:~,4%-%date:~4,2%-%date:~6,2%
echo %date%

This will echo the current date in the yyyy-mm-dd format.


The format of the strings of the environment variables DATE and TIME depends on Windows region settings as defined for used account, i.e. which country is configured for the account used on running the batch file because the country determines the date and time format.

A region independent date/time string can be get using Windows Management Instrumentation Command line tool WMIC.

The command line

wmic OS GET LocalDateTime /VALUE

outputs UTF-16 Little Endian encoded for example:

 
 
LocalDateTime=20170621095402.953000+120
 
 

There are two empty lines, then the line with the current local date/time in format yyyyMMddHHmmss.microsecond±UTC offset in minutes and two more empty lines.

These data can be used with a batch code like this:

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

Of interest for file and folder names is only the date/time string between the equal sign and the decimal point which is the reason for using for /F options tokens=2 delims==. to get just 20170621095402 assigned to loop variable I whose value is assigned next to environment variable FileNameDate.

The environment variable FileNameDate is reformatted using string substitutions to get just the date string in format yyyy-MM-dd which is output for verification resulting in the output 2017-06-21.

The advantage of using WMIC to get local date/time is being independent on Windows region settings. The disadvantage is that the command execution takes a quite long time (1 to 2 seconds) in comparison to the usage of the DATE and TIME environment variables which are accessed in a few microseconds.

The command FOR has problems parsing Unicode output correct. It interprets the byte sequence 0D 00 0A 00 0D 00 0A 00 at end of the WMIC output because of the two empty lines as 0D 0D 0A, i.e. as two carriage returns and one line-feed. This results in interpreting the last two empty lines at end of WMIC output as a single line with a single carriage return as string.

That is very often a problem because the result of set "EnvironmentVariable=%%I" is with %%I expanding to just carriage return in deleting the environment variable already defined before with the correct value.

There are multiple solutions to work around this Unicode parsing error of command FOR. It is possible to append & goto Label to exit the loop with a jump to :Label below the FOR loop once the value is assigned to the environment variable to avoid running into this problem at all.

Another solution is the one used in this code. Because of using WMIC option /VALUE the name of the property and its value are output on same line. The command FOR runs the command SET because of tokens=2 only when it could split up the current line into at least 2 substrings (tokens) using equal sign and dot as delimiters because of delims==.. But the wrong parsed end of WMIC output is for FOR a line containing only a carriage return and therefore has no second token. For that reason the wrongly parsed empty lines are also ignored here by the command FOR.

See How to correct variable overwriting misbehavior when parsing output? and cmd is somehow writing Chinese text as output for details on parsing problem of FOR on UTF-16 LE encoded output.

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 /?
  • set /?
  • wmic /?
  • wmic os /?
  • wmic os get /?
  • wmic os get localdatetime /?

PS: There are lots of other solutions to get current date/time in a specific format independent on country configured for used account. Some of them are faster than the usage of wmic. All those alternative solutions can be found in the answers on:

How do I get current date/time on the Windows command line in a suitable format for usage in a file/folder name?


You have to create a Windows profile for the user being used to run the Task and configure the Regional Settings and Date format under that profile.