为什么%date%在作为计划任务执行的批处理文件中产生不同的结果? [英] Why does %date% produce a different result in batch file executed as scheduled task?

查看:149
本文介绍了为什么%date%在作为计划任务执行的批处理文件中产生不同的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

批处理文件正在运行robocopy,并正在创建名称为robocopyServer_%date%.txt的日志文件.

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

手动运行批处理文件时,结果为robocopyServer_yyyy-mm-dd.txt

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

当批处理文件从Windows Server 2016上的计划任务运行时,结果为robocopyServer_Tue(或星期几),并且没有文件扩展名.

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.

此批处理文件在Windows Server 2012 R2上完美运行(手动或作为计划任务).

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

推荐答案

%date%或date /t并不总是可靠的,因为它取决于区域和用户设置.这是获取通用日期的方法:

%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%

这将以yyyy-mm-dd格式回显当前日期.

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

这篇关于为什么%date%在作为计划任务执行的批处理文件中产生不同的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆