windows %date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2% 是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36147552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does %date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2% mean?
提问by LightTechnician
I have this part of script that create a variable called fileName
used later to name a file.
我有这部分脚本,它创建了一个名为fileName
稍后用于命名文件的变量。
set fileName=db_%date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2%.bak
What does %date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2%
mean?
什么%date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2%
意思?
回答by Mofi
Opening in a command prompt window and running there set /?
outputs the help for command SET.
在命令提示符窗口中打开并在那里运行会set /?
输出命令SET的帮助。
There is explained on last help page that %DATE%
expands to the current locale dateand %TIME%
to the current locale timeon parsing the line containing those environment variable references.
最后还有帮助页面上解释说,%DATE%
扩展到目前的区域设置日期和%TIME%
到当前区域的时间上分析包含了这些环境变量引用的行。
The format of the date and the time depends on Windows region and language settings. Therefore it is necessary to execute in a command prompt window
日期和时间的格式取决于 Windows 区域和语言设置。因此有必要在命令提示符窗口中执行
echo Date is: %DATE%
echo Time is: %TIME%
or alternatively
或者
echo %DATE% %TIME%
best executed before 10:00 AM to know the locale date and time string formats. Is the date with weekday? What is the order of day, month and year in date string? Is the time in 12 or 24 hours format? Is the time always with a two digit hour?
最好在上午 10:00 之前执行以了解区域设置日期和时间字符串格式。日期是工作日吗?日期字符串中日、月、年的顺序是什么?时间是 12 小时制还是 24 小时制?时间总是用两位数的小时吗?
And in output help of command SETthere is explained also %Variable:~X,Y%
to get a substring of a string of an environment variable from position X
in string with Y
characters length.
在命令SET 的输出帮助中,还解释%Variable:~X,Y%
了从字符串中的位置获取环境变量字符串的子字符串,X
并具有Y
字符长度。
The first character in a string has position number (character index) 0
.
A negative value for X
means X
characters from end of string (from right side).
字符串中的第一个字符具有位置编号(字符索引)0
。
负值X
表示X
从字符串末尾(从右侧)开始的字符。
set fileName=db_%date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2%.bak
defines a variable with name fileName
定义一个变量名 fileName
- starting with fixed string
db_
, - appending with
%date:~-4,4%
the last 4 characters of the current locale date which is obviously the year, - appending with
%date:~-10,2%
the tenth and ninth characters from right side of the current locale date which is most likely the month, - appending with
%date:~-7,2%
the seventh and sixth characters from right side of the current locale date which is most likely the day, - appending an underscore as separator between date in format
YYYYMMDD
and time in formatHHmm
, - appending with
%time:~0,2%
the first 2 characters of the current locale time which is obviously the hour, - appending with
%time:~3,2%
the fourth and fifth character of the current locale time which is obviously the minute and - appending the file extension
.bak
.
- 从固定字符串开始
db_
, - 附加
%date:~-4,4%
当前语言环境日期的最后 4 个字符,这显然是年份, - 附加
%date:~-10,2%
当前语言环境日期右侧的第十个和第九个字符,这很可能是月份, - 附加
%date:~-7,2%
当前语言环境日期右侧的第七个和第六个字符,这很可能是当天, - 在格式中的日期和格式中的
YYYYMMDD
时间之间附加下划线作为分隔符HHmm
, - 附加
%time:~0,2%
当前语言环境时间的前 2 个字符,这显然是小时, - 附加
%time:~3,2%
当前语言环境时间的第四个和第五个字符,显然是分钟和 - 附加文件扩展名
.bak
.
The result of this variable definition with several substrings can be verified by executing in a command prompt window:
可以通过在命令提示符窗口中执行来验证具有多个子字符串的此变量定义的结果:
echo db_%date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2%.bak
The substring references from date string are done from right side (end of string) instead of left side (beginning of string) to make the definition independent on weekday present or missing on beginning of date string.
来自日期字符串的子字符串引用是从右侧(字符串结尾)而不是左侧(字符串开头)完成的,以使定义独立于工作日出现或在日期字符串开头丢失。
回答by Admiral Noisey Bottom
Using SET in DOS sets an environment variable that can be later retrieved by other DOS Commands, either directly or via Batch Files.
在 DOS 中使用 SET 设置一个环境变量,以后可以由其他 DOS 命令直接或通过批处理文件检索该变量。
For example;
例如;
SET PATH=%PATH%;C:\MyStuff
SET PATH=%PATH%;C:\MyStuff
The above adds C:\MyStuff to the Path.
以上将 C:\MyStuff 添加到路径中。
SET FRIEDRICE=YES
设置炸薯条=是
This creates an environment variable called FRIEDRICE and sets the value to YES.
这将创建一个名为 FRIEDRICE 的环境变量并将值设置为 YES。
The value set is only valid for the current command prompt session unless it's set more permanently using other methods.
设置的值仅对当前命令提示符会话有效,除非使用其他方法对其进行更永久的设置。
In the good old days AUTOEXEC.BAT was often used to set up the basic operating environment.
在过去,AUTOEXEC.BAT 经常被用来设置基本的操作环境。
EDIT
编辑
Mofi is quite right in his answer. The right side of the SET command, in your case, is setting a Filename (for later use somewhere) into the current environment variables. Sadly I misunderstood the question and thought you were asking about SET rather than what was being set. You can test your SET commands by typing SET (with no paremeters) in your command prompt. The current environment variables will be echoed.
莫菲的回答说得很对。在您的情况下,SET 命令的右侧是将文件名(供以后在某处使用)设置到当前环境变量中。遗憾的是,我误解了这个问题,并认为您是在询问 SET 而不是正在设置的内容。您可以通过在命令提示符中键入 SET(不带参数)来测试 SET 命令。当前环境变量将被回显。