windows 批处理文件日/月/年语法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8662432/
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
Batch file day/month/year syntax?
提问by mtallyn
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.
任何人都可以阐明(或发布链接)波浪号,双百分比的含义吗?我似乎无法仅凭直觉就完全破译它。
回答by paxdiablo
The double percentage means absolutely nothing. It is simply a result of having two variable expansions side-by-side such as:
双倍百分比绝对没有任何意义。它只是并排有两个变量扩展的结果,例如:
echo %firstname%%lastname%
\_________/\________/
Two separate expansions.
The tilde gives you a substring. In your case, %date:~10,4%
gives you four characters at offset ten of the date
environment variable (the year in this case since the format is likely Thu 29/12/2011
, with offsets starting at zero).
波浪号给你一个子字符串。在您的情况下,%date:~10,4%
在date
环境变量的偏移量十处为您提供四个字符(在这种情况下为年份,因为格式可能Thu 29/12/2011
,偏移量从零开始)。
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 endof the string.
如果您set /?
在 Windows 命令提示符处输入,它将为您解释所有选项,包括使用负偏移量从字符串末尾提取的绝妙技巧。
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).
但是,您应该记住,date
环境变量格式取决于语言环境,因此这种简单的字符串提取不太可能适用于所有国际版本的 Windows(这在几年前我有点过头了)。
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:
更好的解决方案是使用 WMI 获取日期组件,例如Rob van der Woude 的优秀脚本页面上的日期组件,为了完整起见,复制到此处:
FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Day?,Hour?,Minute?,Month?,Second?,Year /Format:table') DO (
IF NOT "%%~F"=="" (
SET /A SortDate = 10000 * %%F + 100 * %%D + %%A
SET /A SortTime = 10000 * %%B + 100 * %%C + %%E
SET SortTime=0000000!SortTime!
SET SortTime=!SortTime:~-6!
)
)
回答by daniel
The ~ and %%s are splitting the string (if you just type date into command it shows you the full string).
~ 和 %%s 正在拆分字符串(如果您只是在命令中键入 date 它会显示完整的字符串)。
%date:~10,4% means get next 4 characters from the 10th character along.
%date:~10,4% 表示从第 10 个字符开始获取接下来的 4 个字符。
Also watch out for different PCs using different Regional Settings, as they change the order of those characters in the string.
还要注意使用不同区域设置的不同 PC,因为它们会更改字符串中这些字符的顺序。
回答by Aaron
@paxdiablo beat me to it. Here's a link to a site that explains how it works, with plenty of examples.
@paxdiablo 打败了我。这是一个指向解释其工作原理的站点的链接,其中包含大量示例。