001-VBScript

一、语法中文网

1.菜鸟教程

教程地址:https://www.runoob.com/vbscript/vbscript-tutorial.html

2.W3School

教程地址:http://www.twcms.com/book/w3school/vbscript/index.asp.htm

二、使用案例

001.在指定时间段内,间隔指定时间,循环执行指定操作
' 设置开始时间和结束时间
Dim startTime, endTime
startTime = "08:00:00" ' 开始时间,格式为HH:MM:SS
endTime = "19:00:00"   ' 结束时间,格式为HH:MM:SS

' 设置间隔时间(以分钟为单位)
Dim intervalMinutes
intervalMinutes = 0.1

' 获取当前时间
Dim currentTime
currentTime = Now

' 不同状态执行不同操作
Dim currentStatus
currentStatus = 0

Do
' 检查当前时间是否在指定时间段内
If IsInTimeRange(currentTime, startTime, endTime) Then
	
	IF currentStatus = 0 Then
		' 执行操作
		StartOperation
		currentStatus = 1
	Else
		EndOperation
		currentStatus = 0
	End If
    
    ' 暂停指定的时间间隔
    WScript.Sleep intervalMinutes * 60000 ' 1分钟 = 60000毫秒
Else
	EndOperation
	StartOperation
End If

Loop

' 检查当前时间是否在指定时间段内的函数
Function IsInTimeRange(time, startTime, endTime)
    
    Dim timeHour, timeMinute, startHour, startMinute, endHour, endMinute
    timeHour = Hour(time)
    timeMinute = Minute(time)
    startHour = Hour(startTime)
    startMinute = Minute(startTime)
    endHour = Hour(endTime)
    endMinute = Minute(endTime)
	
    If (timeHour > startHour) Or ((timeHour = startHour) And (timeMinute >= startMinute)) Then
        If (timeHour < endHour) Or ((timeHour = endHour) And (timeMinute <= endMinute)) Then
            IsInTimeRange = True
        Else
            IsInTimeRange = False
        End If
    Else
        IsInTimeRange = False
    End If
End Function

' 执行操作的函数
Sub StartOperation
    ' 在这里添加你的操作代码
    MsgBox "start..."
End Sub

' 执行操作的函数
Sub EndOperation
    ' 在这里添加你的操作代码
    MsgBox "end..."
End Sub

002.指定两个时间点,一个时间点执行A操作,一个时间点执行B操作,执行完B后结束
' 设置开始时间和结束时间
Dim startTime, endTime
startTime = "18:45:00" ' 开始时间,格式为HH:MM:SS
endTime = "18:46:00"   ' 结束时间,格式为HH:MM:SS

' 设置间隔时间(以分钟为单位)
Dim intervalMinutes
intervalMinutes = 1

Dim currentStatus
currentStatus = 2


Dim timeHour, timeMinute, startHour, startMinute, endHour, endMinute
startHour = Hour(startTime)
startMinute = Minute(startTime)
endHour = Hour(endTime)
endMinute = Minute(endTime)


Do
' 检查当前时间是否在指定时间段内
MsgBox currentStatus

' 获取当前时间
Dim currentTime
currentTime = Now

timeHour = Hour(currentTime)
timeMinute = Minute(currentTime)

If ((timeHour = startHour And timeMinute = startMinute) Or (timeHour = endHour And timeMinute = endMinute)) And currentStatus >= 1  Then
    
	IF currentStatus = 2 Then
		' 执行操作
		EndOperation
		StartOperation
		currentStatus = 1
	Else
		EndOperation
		currentStatus = 0
	End If
	
    ' 暂停指定的时间间隔
    WScript.Sleep intervalMinutes * 60000 ' 1分钟 = 60000毫秒
Else
	EndOperation
	IF currentStatus = 0 Then
		Exit Do
	Else
		' 暂停指定的时间间隔
		WScript.Sleep intervalMinutes * 60000 ' 1分钟 = 60000毫秒
	End If
End If

Loop


' 执行操作的函数
Sub StartOperation
    ' 在这里添加你的操作代码
    MsgBox "start..."
End Sub

' 执行操作的函数
Sub EndOperation
    ' 在这里添加你的操作代码
    MsgBox "end..."
End Sub
003.设定一个闹钟,可以指定间隔时间,也可以关闭
Dim interval
Dim ret

' set task's interval
Do
  interval = InputBox("Please input the interval:", "Interval(seconds)", 30 * 60 * 1000)

  If interval = vbEmpty Then
    ' click cancel
    MsgBox "You input empty, interval is set default half an hour."
    interval = 1800000
    Exit Do
  End If

  If Not IsNumeric(interval) Then
    ' input what is't a numeric
     MsgBox "Please input number"
   Else
     Exit Do
  End If
Loop

' execute interval task
Do
  ret = MsgBox("Drink some water", vbOKCancel, "Time up")
  If ret = 1 Then
    Wscript.sleep interval
  Else
    MsgBox "Close notification", vbOkOnly, "Close notification"
    Exit Do
  End If
Loop
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>