bat文件 日期返回获取 另附if和for的运用

来源:互联网 发布:重庆中联软件 编辑:程序博客网 时间:2024/06/05 21:05

当前版本使用截止时间:2016-06-01
输入新时间:
tm1=23
tm2=06:
tm3=25

如下:

@echo off
time
set tm1=%time:~0,2%
set tm2=%time:~3,3%
set tm3=%time:~6,2%

set tm1
set tm2
set tm3

%time:~0,2% 表示从字符串的"23:06:25.17"第0个开始,去取2个长度的字符为"23"了。

%time:~3,3% 表示从字符串的"23:06:25.17"第三个开始,去取三个长度的字符为"06:"

%...%表示内部作为一个值返回给cmd。

例:

@echo off
:start
:: ************ SAVE OLD DATE *********************
SET OldDate=%DATE:~0,10%
ECHO ********** now date is: %OldDate% *************

ECHO ********** Reset the Rtool's not overdue date."Q" is exit *****
:doIn
SET /P Input=NewDate.Format:YYYY-MM-DD:
IF "%Input%" == "" GOTO doIn
IF "%Input%" == "Q" GOTO exit
IF "%Input%" == "q" GOTO exit
SET Input=%Input:~0,10%
ECHO ********** Input OK.***************************
SET NewDate=%Input%
DATE %NewDate%
ECHO ********** Set newDate OK *********************
ECHO ********** you set the date is: %NewDate% *****

ECHO ********** Recover System Dat *****************
PAUSE
DATE %OldDate%
ECHO ********** now date is: %OldDate% *************

ECHO ********** Exit the "DateControl" system [y/n]?

SET /P exitFlag=Exit[n]?
IF "%exitFlag%" == "n" GOTO start
IF "%exitFlag%" == "N" GOTO start
:exit


执行效果:

********** now date is: 2009-06-18 *************
********** Reset the Rtool's not overdue date."Q" is exit *****
NewDate.Format:YYYY-MM-DD:2009-09-09
********** Input OK.***************************
********** Set newDate OK *********************
********** you set the date is: 2009-09-09 *****
********** Recover System Dat *****************
请按任意键继续. . .
********** now date is: 2009-06-18 *************
********** Exit the "DateControl" system [y/n]?
Exit[n]?

另附:FOR命令

FOR %variable IN (set) DO command [command-parameters]

  %variable 指定一个单一字母可替换的参数。

  (set) 指定一个或一组文件。可以使用通配符。

  command 指定对每个文件执行的命令。

  command-parameters

  为特定命令指定参数或命令行开关。

在xp底下,for命令扩展名被起用因此for的功能变得更加强大.下面讲一个真正的循环.

  FOR /L %variable IN (start,step,end) DO command [command-parameters]

  该集表示以增量形式从开始到结束的一个数字序列。

  因此,(1,1,5) 将产生序列 1 2 3 4 5,(5,-1,1) 将产生

  序列 (5 4 3 2 1)

  里面第一个1在start的位置,意思是起始位置,第2个1在step位置,英文意思是跨步,在这里面的意思是每次增量为1.后面的5在end的位置,意思是结束时的大小.

  这句话的意思就是从1(start)开始增加,每次增加1(step),一直到变到5(end)为止

参考:SET /?

原创粉丝点击