批处理文件日/月/年语法?

发布于 2024-12-23 14:55:46 字数 188 浏览 1 评论 0原文

我找不到用于提取当前日/月/年的批处理文件语法的简单细分。

我有以下语法来声明用作目录名称的变量;

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

近箐 2024-12-30 14:55:46

双百分比绝对没有任何意义。它只是并排进行两个变量扩展的结果,例如:

echo %firstname%%lastname%
     \_________/\________/
      Two separate expansions.

波浪号为您提供一个子字符串。在您的情况下, %date:~10,4%date 环境变量的偏移量 10 处为您提供四个字符(在本例中为年份,因为格式可能为 < code>Thu 29/12/2011,偏移量从零开始)。

如果您在 Windows 命令提示符处输入 set /?,它会为您解释所有选项,包括使用负偏移量从末尾提取内容的巧妙技巧。细绳。

但是,您应该记住,date 环境变量格式取决于区域设置,因此这种简单的字符串提取不太可能在所有国际版本的 Windows 上工作(这让我在几年前感到困惑)。

更好的解决方案是使用 WMI 获取日期组件,例如 Rob van der Woude 出色的脚本页面< /a>,为了完整性而复制到此处:

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

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

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:

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!
    )
)
梦与时光遇 2024-12-30 14:55:46

~ 和 %%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.

凌乱心跳 2024-12-30 14:55:46

@paxdiablo 打败了我。这里有一个解释其工作原理的网站链接,其中有大量示例。

@paxdiablo beat me to it. Here's a link to a site that explains how it works, with plenty of examples.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文