%date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2% 是什么意思? [英] What does %date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2% mean?

查看:19
本文介绍了%date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2% 是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这部分脚本创建了一个名为 fileName 的变量,稍后用于命名文件.

I have this part of script that create a variable called fileName used later to name a file.

set fileName=db_%date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2%.bak

%date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2% 是什么意思?

推荐答案

在命令提示符窗口中打开并在那里运行 set/? 输出命令 SET 的帮助.

Opening in a command prompt window and running there set /? outputs the help for command SET.

在上一个帮助页面上解释说,%DATE% 扩展为当前语言环境日期%TIME% 扩展为 解析包含这些环境变量引用的行时的当前语言环境时间.

There is explained on last help page that %DATE% expands to the current locale date and %TIME% to the current locale time on parsing the line containing those environment variable references.

日期和时间的格式取决于 Windows 区域和语言设置.因此需要在命令提示符窗口中执行

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

echo Date is: %DATE%
echo Time is: %TIME%

或者替代

echo %DATE% %TIME%

最好在上午 10:00 之前执行以了解区域设置日期和时间字符串格式.日期是工作日吗?日期字符串中日、月、年的顺序是什么?时间是 12 小时制还是 24 小时制?时间总是两位数的小时吗?

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?

并且在命令 SET 的输出帮助中,还解释了 %Variable:~X,Y% 从位置 <获取环境变量字符串的子字符串code>X 字符串,Y 字符长度.

And in output help of command SET there 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.

字符串中的第一个字符具有位置编号(字符索引)0.
X 的负值意味着 X 字符串末尾(从右侧)的字符.

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).

set fileName=db_%date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2%.bak

定义一个名为fileName

  • 以固定字符串开头 db_,
  • 附加 %date:~-4,4% 当前语言环境日期的最后 4 个字符,显然是年份,
  • 附加 %date:~-10,2% 当前语言环境日期右侧的第十个和第九个字符,最有可能是月份,
  • 附加 %date:~-7,2% 当前语言环境日期右侧的第七个和第六个字符,这很可能是当天,
  • 在格式为 YYYYMMDD 的日期和格式为 HHmm 的时间之间附加下划线作为分隔符,
  • 附加 %time:~0,2% 当前语言环境时间的前 2 个字符,显然是小时,
  • 附加 %time:~3,2% 当前语言环境时间的第四个和第五个字符,显然是分钟和
  • 附加文件扩展名 .bak.
  • 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 format HHmm,
  • 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.

可以通过在命令提示符窗口中执行来验证具有多个子字符串的此变量定义的结果:

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.

这篇关于%date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2% 是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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