批处理文件日/月/年语法?
我找不到用于提取当前日/月/年的批处理文件语法的简单细分。
我有以下语法来声明用作目录名称的变量;
set folder=%date:~10,4%%date:~7,2%%date:~4,2%
任何人都可以阐明(或发布链接)波浪号、双百分比的含义吗?我似乎无法仅凭直觉完全解读它。
I can't find a simple breakdown of the batch file syntax for extracting the current day/month/year.
I have the following syntax for declaring a variable used as a directory name;
set folder=%date:~10,4%%date:~7,2%%date:~4,2%
Can anyone shed some light (or post a link) on what the tilde, the double percentage means? I can't seem to fully decipher it from intuition alone.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
双百分比绝对没有任何意义。它只是并排进行两个变量扩展的结果,例如:
波浪号为您提供一个子字符串。在您的情况下,
%date:~10,4%
在date
环境变量的偏移量 10 处为您提供四个字符(在本例中为年份,因为格式可能为 < code>Thu 29/12/2011,偏移量从零开始)。如果您在 Windows 命令提示符处输入
set /?
,它会为您解释所有选项,包括使用负偏移量从末尾提取内容的巧妙技巧。细绳。但是,您应该记住,
date
环境变量格式取决于区域设置,因此这种简单的字符串提取不太可能在所有国际版本的 Windows 上工作(这让我在几年前感到困惑)。更好的解决方案是使用 WMI 获取日期组件,例如 Rob van der Woude 出色的脚本页面< /a>,为了完整性而复制到此处:
The double percentage means absolutely nothing. It is simply a result of having two variable expansions side-by-side such as:
The tilde gives you a substring. In your case,
%date:~10,4%
gives you four characters at offset ten of thedate
environment variable (the year in this case since the format is likelyThu 29/12/2011
, with offsets starting at zero).If you enter
set /?
at a Windows command pronpt, it will explain all the options for you, including the nifty trick of using negative offsets to extract from the end of the string.However, you should keep in mind that the
date
environment variable format depends on the locale so this simplistic string extraction is unlikely to work across all international versions of Windows (this bit me a couple of years back).A better solution is to use WMI to get the date components such as on Rob van der Woude's excellent scripting pages, copied here for completeness:
~ 和 %%s 正在分割字符串(如果您只是在命令中键入日期,它会显示完整的字符串)。
%date:~10,4% 表示从第 10 个字符开始获取接下来的 4 个字符。
还要注意使用不同区域设置的不同电脑,因为它们会更改字符串中这些字符的顺序。
The ~ and %%s are splitting the string (if you just type date into command it shows you the full string).
%date:~10,4% means get next 4 characters from the 10th character along.
Also watch out for different PCs using different Regional Settings, as they change the order of those characters in the string.
@paxdiablo 打败了我。这里有一个解释其工作原理的网站链接,其中有大量示例。
@paxdiablo beat me to it. Here's a link to a site that explains how it works, with plenty of examples.