Datetime VBScript-运行文件的日期和时间限制
好的,我对写VBScript还不熟悉,我想写一个字符串,只在某一天和特定时间之间播放一个文件(WAV格式)。将我在互联网上找到的多段代码拼凑在一起后,我只剩下以下内容:Datetime VBScript-运行文件的日期和时间限制,datetime,audio,if-statement,vbscript,runtime-error,Datetime,Audio,If Statement,Vbscript,Runtime Error,好的,我对写VBScript还不熟悉,我想写一个字符串,只在某一天和特定时间之间播放一个文件(WAV格式)。将我在互联网上找到的多段代码拼凑在一起后,我只剩下以下内容: Dim myDateString Dim thing1 thing1 = 0 myDateString = Date() If myDateString < "13/08/13" Then thing1 = 1 end if if thing1 = 1 then If myDateString >
Dim myDateString
Dim thing1
thing1 = 0
myDateString = Date()
If myDateString < "13/08/13" Then
thing1 = 1
end if
if thing1 = 1 then
If myDateString > "15/08/13" Then
thing1 = 2
end if
end if
if thing1 = 2 then
hournow = hour(Time())
If hour(Time()) >= 9 And Hour(Now()) < 22 Then
set WshShell = CreateObject("WScript.Shell")
music = "C:\Users\MYUSERNAME\Desktop\MYSOUND.wav"
WshShell.Run "wmplayer """ & music & """",0,True
Else
wscript.quit 1
End If
Else
wscript.quit 1
End If
Dim myDateString
暗淡的东西
thing1=0
myDateString=日期()
如果myDateString<“13/08/13”,则
事物1=1
如果结束
如果thing1=1,那么
如果myDateString>“15/08/13”,则
事物1=2
如果结束
如果结束
如果thing1=2,则
hournow=小时(Time())
如果小时(Time())>=9,小时(Now())<22,那么
设置WshShell=CreateObject(“WScript.Shell”)
music=“C:\Users\MYUSERNAME\Desktop\MYSOUND.wav”
WshShell.Run“wmplayer”“”和music&”“,0,True
其他的
wscript.1退出
如果结束
其他的
wscript.1退出
如果结束
好的,所以我已经为我运行这个的日期设置了这个,在我进去的一个小时内。但是
它不起作用。我希望VBS开始播放MYSOUND.wav,但它没有。运行文件时
但是没有错误,所以我想知道我做错了什么
我正在运行Windows7
如果有人能告诉我我做错了什么,以及如何改正,那就太好了
如果有人可以发布代码的更正版本,则得双倍积分
谢谢你的回答 首先,缩进您的代码,并为变量指定有意义的名称 然后,日期比较不起作用,因为您试图将字符串当作日期进行比较。这通常不起作用(取决于您的“系统区域设置”):您需要使用日期类型变量和实际的日期比较函数(VBScript中的DateDiff)
(编辑:正如Ansgar Wiechers指出的那样,您不需要使用DateDiff来比较VBScript中的日期,“DateStart您把事情复杂化了。使用普通的比较运算符(
DateStart
)可以很好地比较日期。”。无需使用DateDiff
。非常感谢!这完美地解决了我的问题!令人痛苦的是,有没有办法等到脚本播放声音文件之前的特定时间(在正确的一天)?如果没有,感谢您迄今为止的努力!如果我有足够的声誉,我会投票支持您的答案!
Dim DateStart, DateEnd, WshShell, music
DateStart = DateSerial(2013, 8, 13)
DateEnd = DateSerial(2013, 8, 15)
If DateDiff("D", DateStart, Now) >= 0 And DateDiff("D", Now, DateEnd) >= 0 Then
If Hour(Now) >= 9 And Hour(Now) < 22 Then
'*** delete after debugging ***
MsgBox "play sound"
Set WshShell = CreateObject("WScript.Shell")
music = "C:\Users\MYUSERNAME\Desktop\MYSOUND.wav"
'*** 2nd parameter : 0 hides wmplayer, 1 shows it ***
WshShell.Run "wmplayer """ & music & """", 1, True
Else
'*** delete after debugging ***
MsgBox "Not the right time"
End If
Else
'*** delete after debugging ***
MsgBox "Not the right day"
End If