批处理脚本日起变数 [英] Batch script date into variable

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

问题描述

for /F "tokens=1-4 delims=/ " %%i in ('date /t') do (
set Day=%%k
set Month=%%j
set Year=%%l
set DATE=%%k/%%j/%%l)

我试图让日起在批处理脚本上述变量,但当前的日期出来为

I am try to get the date into the above variables in a batch script, but currently the date comes out as

2011/04/

这是如何解决这一问题有什么建议?

Any suggestions on how to fix this?

推荐答案

您没有得到您所期望的,因为%DATE%使用Windows设置返回当前日期对于短日期格式。此设置完全(不休)定制的。

You don't get what you expected because %DATE% returns the current date using the windows settings for the "short date format". This setting is fully (endlessly) customizable.

一个用户可配置系统显示短日期Fri040811;而另一用户(甚至在同一系统)可以选择08/04/2011。这是一个为BAT程序员一个完整的噩梦。

One user may configure its system to show the short date as Fri040811; while another user (even in the same system) may choose 08/04/2011. It's a complete nightmare for a BAT programmer.

一个可能的解决方案是使用 WMIC 代替。 WMIC是WMI命令行界面WMI。 WMI Windows管理规范的是<一个href=\"http://en.wikipedia.org/wiki/Windows_Management_Instrumentation\">http://en.wikipedia.org/wiki/Windows_Management_Instrumentation

One possible solution is to use WMIC, instead. WMIC is the WMI command line interface to WMI. WMI Windows Management Instrumentation is the http://en.wikipedia.org/wiki/Windows_Management_Instrumentation

WMIC Path Win32_LocalTime Get Day,Hour,Minute,Month,Second,Year /Format:table

返回一个便捷的方式的日期直接与解析一个

完成解析,并把拼在一起

Completing the parse and putting the pieces together

 FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
    SET /A TODAY=%%F*10000+%%D*100+%%A
 )

这篇关于批处理脚本日起变数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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