批处理文件日/月/年的语法? [英] Batch file day/month/year syntax?

查看:214
本文介绍了批处理文件日/月/年的语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

双百分比意味着绝对没有。它只是具有的结果两个可变扩展侧由端如:

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.

波浪号给你一个子。在你的情况,%日期:10.4〜%为您提供了四个大字,偏移量为之日起10 环境变量(因为在格式这种情况下,今年很可能周四29/12/2011 ,以从零开始的偏移量)。

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

如果你输入设置/?在Windows命令pronpt,它会解释给你的所有选项,包括使用负偏移从提取的漂亮的伎俩结束的字符串。

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.

然而,你应该记住,在日期环境变量格式取决于语言环境让这个简单的字符串提取是不可能跨越的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).

一个更好的解决方案是使用WMI来获取最新组件如罗布·范德Woude的优秀的脚本页面,这里复制的完整性:

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

这篇关于批处理文件日/月/年的语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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