本文介绍了如何创建一个用户环境变量,*每次调用它的调用时间*%日期%或%时间%?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建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%

这样每次我打电话:

so that every time I call:

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.

推荐答案

或者使用延迟扩展,那么你就能够用一行扩大两倍。

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%

它的作品,因为,第一批次行分析器扩张 DATEL%%日期:〜-4 !!日期:3,2〜 !日期:〜!0,2 毕竟%的扩张完成。

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.

在<一个解释href=\"http://stackoverflow.com/questions/4094699/how-does-the-windows-command-inter$p$pter-cmd-exe-parse-scripts/4095133#4095133\">how CMD.EXE解析脚本

这篇关于如何创建一个用户环境变量,*每次调用它的调用时间*%日期%或%时间%?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 13:05