如何创建一个用户环境变量,每次调用时会调用*%日期%或%时间%? [英] How to create a user Environment variable that *calls* %date% or %time% each time it's invoked?
问题描述
我试图用以下定义创建2个用户环境变量:
I'm trying to create 2 user environment variables with the following defintion:
datel=%date:~-4%%date:~3,2%%date:~0,2%
datetime=%date:~-4%%date:~3,2%%date:~0,2%-%time:~0,2%_%time:~3,2%_%time:~6,2%
,以便每次我打电话:
echo %datel%
echo %datetime%
我得到:
20110407
20110407-11_45_45
我可以在GUI中定义用户环境变量,没有问题(计算机 - >(右键单击)属性 - >高级系统设置 - >环境变量),当我在新的cmd窗口中进行设置时,我得到以下内容:
I can define the user environment variables without problems in the GUI (Computer->(Right Click)Properties->Advanced System Settings->Environment Variables) and when I do a "set" in a new cmd window I get the following:
>set da
datel=%date:~-4%%date:~3,2%%date:~0,2%
datetime=%date:~-4%%date:~3,2%%date:~0,2%-%time:~0,2%_%time:~3,2%_%time:~6,2%
但是,回音他们不是我预期的:
But then "echoing" them is not what I expected:
C:\Users\jaravj
>echo %datel%
%date:~-4%%date:~3,2%%date:~0,2%
C:\Users\jaravj
>echo %datetime%
%date:~-4%%date:~3,2%%date:~0,2%-%time:~0,2%_%time:~3,2%_%time:~6,2%
谢谢或者使用延迟扩展,那么您可以在一行中扩展两次。
Thanks a huge lot in advance.
推荐答案
p>
Or use the delayed expansion, then you are able to expands two times in one line.
setlocal
set "datel=!date:~-4!!date:~3,2!!date:~0,2!"
setlocal EnableDelayedExpansion
echo %datel%
它的作品是因为,首先批处理日期:〜4!日期:〜3,2!日期:〜0,2! c $ c> / code>,所有百分比扩展完成后。
It's works because, first the batch line parser expands %datel%
to !date:~-4!!date:~3,2!!date:~0,2!
and after all percent expansions are done.
然后转义字符被处理,然后作为最后一个阶段,解析器扩展感叹号将被扩展。
Then the escape characters are handled, and then as the last phase the parser expands the exclamations are expanded.
这篇关于如何创建一个用户环境变量,每次调用时会调用*%日期%或%时间%?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!