我有以下VBS代码......
Option Explicit
Dim objFSO, objShell, objTextFile
Dim strText
Dim prevDate
Dim prevTime
Dim timeLeft
Const strDirectory = "C:"
Const strFile = "\timelog.txt"
Const checkTimeLeftVbs = "C:\checktimeleft.vbs"
Const ForAppending = 8
Const ForReading = 1
Const ForWriting = 2
Dim usageTime 'in minutes
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = WScript.CreateObject ("WScript.Shell")
Function openLogFile(N)
Set objTextFile = objFSO.OpenTextFile(strDirectory & strFile, N, True)
End Function
Function checkPrevInst()
Call openLogFile(ForReading)
prevDate = objTextFile.ReadLine
prevTime = CInt(objTextFile.ReadLine)
objTextFile.Close
If prevDate <> "" Then
If DateDiff("d", prevDate, Date) >= 1 Then
Call logStartTime()
Else
'Continue monitoring
Call logTime()
End If
Else
'Log file compromised...
Call logStartTime()
End If
End Function
Function logStartTime()
Call openLogFile(ForWriting)
objTextFile.WriteLine(Date)
objTextFile.WriteLine("0")
objTextFile.Close
prevDate = Date
prevTime = 0
Call logTime()
End Function
Function checkforChanges()
Dim tempPrevDate
Dim tempPrevTime
Call openLogFile(ForReading)
tempPrevDate = objTextFile.ReadLine
tempPrevTime = CInt(objTextFile.ReadLine)
objTextFile.Close
If tempPrevDate = PrevDate Then
If tempPrevTime <> prevTime Then
prevTime = tempPrevTime
timeLeft = usageTime - prevTime
objShell.Run checkTimeLeftVbs
End If
End If
End Function
Function logTime()
timeLeft = usageTime - prevTime
objShell.Run checkTimeLeftVbs
Do While timeLeft > 0
Call openLogFile(ForWriting)
objTextFile.WriteLine(prevDate)
objTextFile.WriteLine(prevTime)
objTextFile.WriteLine(timeLeft)
objTextFile.Close
WScript.Sleep 60000
Call checkforChanges()
prevTime = prevTime + 1
timeLeft = usageTime - prevTime
If timeLeft <= 5 Or timeLeft = 10 Or timeLeft = 15 Then
objShell.Run checkTimeLeftVbs
End If
Loop
'Time exceeded
Call timeExceeded()
End Function
Function timeExceeded()
Do While timeLeft <= 0
Call endSession()
WScript.Sleep 10000
Loop
End Function
Function endSession()
objShell.Run "%windir%\SYSTEM32\rundll32.exe user32.dll,LockWorkStation", 0, False
End Function
Function checkDay()
If Weekday(Date, 1) <> "1" And Weekday(Date, 1) <> "6" And Weekday(Date, 1) <> "7" Then
usageTime = 60 'weekday time, in minutes
Else
usageTime = 90 'weekend time, in minutes
End If
Call checkPrevInst()
End Function
'Main program
Call checkDay()
每次我尝试运行它时,我都会收到以下错误
行:25个字符:1错误:输入文件的结尾代码:800A300E
这就是让我错误的行:prevDate = objTextFile.ReadLine
请你帮我解决这个错误吗?
P.S。该代码是从wikihow下载的。
答案 0 :(得分:2)
问题是代码试图读取文件末尾。所以你可以通过做这样的事情来确保不会发生:
Function checkPrevInst()
Call openLogFile(ForReading)
Do While Not objFile.AtEndOfStream
prevDate = objTextFile.ReadLine
prevTime = CInt(objTextFile.ReadLine)
Loop
objTextFile.Close
If prevDate <> "" Then
If DateDiff("d", prevDate, Date) >= 1 Then
Call logStartTime()
Else
'Continue monitoring
Call logTime()
End If
Else
'Log file compromised...
Call logStartTime()
End If
End Function
请注意,您只是在文件不在流的末尾时才读取文件。