一个视窗批处理文件变量设置为一周中的天 [英] Setting a windows batch file variable to the day of the week

查看:205
本文介绍了一个视窗批处理文件变量设置为一周中的天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有每天运行Windows批处理文件。希望将数据记录到一个文件,要旋转(即具有最最近7天的数据)。

I have a windows batch file that runs daily. Wish to log data into a file and want to rotate it (i.e. having at most the last 7 days worth of data).

看着命令日期 DELIMS - 不能找出解决的办法。

Looked into the commands DATE and DELIMS - Cannot figure out a solution.

有一个简单的解决方案,以创建一个包含一周即0的一天星期一的文件名等。

Is there a simple solution to create a file name that contains the day of the week i.e. 0 for monday etc.

还是我不得不求助于一些更好的shell脚本。

Or do I have to resort to some better shell script.

推荐答案

%DATE%不是你的朋友。因为%DATE%环境变量(和日期命令)返回使用Windows的当前日期短日期格式的是完全和无休止地定制。一个用户可以配置系统,将07月06日,而另一个可能选择Fri060712。使用%DATE%是一个BAT程序员一个完整的噩梦。

%DATE% is not your friend. Because the %DATE% environment variable (and the DATE command) returns the current date using the Windows short date format that is fully and endlessly customizable. One user may configure the system to return 07/06/2012 while another might choose Fri060712. Using %DATE% is a complete nightmare for a BAT programmer.

有解决这个问题的两种可能的方法:

There are two possible approaches to solve this problem:


  1. 您可能想临时更改短日期格式,通过在注册表值 HKCU \\控制面板\\国际\\ sShortDate ,要更改区域设置您可识别的格式。然后访问%DATE%来得到你想要的格式的日期;最后恢复的格式返回到原来的用户的格式。事情是这样的。

  1. You may be tempted to temporarily change the short date format, by changing the locale settings in the registry value HKCU\Control Panel\International\sShortDate, to your recognizable format. Then access %DATE% to get the date in the format you want; and finally restore the format back to the original user format. Something like this

reg copy "HKCU\Control Panel\International" "HKCU\Control Panel\International-Temp" /f >nul
reg add "HKCU\Control Panel\International" /v sShortDate /d "ddd" /f >nul
set DOW=%DATE%
reg copy "HKCU\Control Panel\International-Temp" "HKCU\Control Panel\International" /f >nul

但这种方法有两个问题:

but this method has two problems:


  • 这与当地特殊的效果影响不大,一个全球性的注册表值篡改,从而可以用,在非常同时查询短日期格式,包括它本身如果同时运行其他进程或用户任务干扰。

  • it tampers with a global registry value for its local particular purpouses, so it may interfere with other processes or user tasks that at the very same time query the date in short date format, including itself if run simultaneously.

和它返回一周中的三个字母一天在当地语言可能在不同的系统或不同用户的不同

and it returns the three letter day of the week in the local language that may be different in different systems or different users.

使用 WMIC Win32_LocalTime ,返回的日期在方便的方式直接与命令解析它。

use WMIC Win32_LocalTime, that returns the date in a convenient way to directly parse it with a FOR command.

FOR /F "skip=1" %%A IN ('WMIC Path Win32_LocalTime Get DayOfWeek' ) DO (
  set DOW=%%A
)

这是我推荐的方法。

这篇关于一个视窗批处理文件变量设置为一周中的天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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