我已从here复制了旧的VBS代码,但似乎无法正常工作。 我对VBS一无所知,所以我在这里迷路了。
这似乎很简单,它打开一个文本文件,根据之前编写的内容更改值(只是“略微翻转”),删除旧文件,然后写入新文件。 关闭它创建的文件似乎有问题,并且从未创建
第42行出现错误,代码为
800A01A8,“所需对象:”“。
Const ForAppending = 8
Const ForReading = 1
Dim outPutFile
Dim objFSO, objFile, objOutFile, strLine
dim sleepLine1, sleepLine2, doNothingLine1, doNothingLine2
sleepLine1 = "powercfg -SETACVALUEINDEX 381b4222-f694-41f0-9685-ff5bb260df2e 4f971e89-eebd-4455-a8de-9e59040e7347 5ca83367-6e45-459f-a27b-476b1d01c936 1"
sleepLine2 = "powercfg -SETDCVALUEINDEX 381b4222-f694-41f0-9685-ff5bb260df2e 4f971e89-eebd-4455-a8de-9e59040e7347 5ca83367-6e45-459f-a27b-476b1d01c936 1"
doNothingLine1 = "powercfg -SETACVALUEINDEX 381b4222-f694-41f0-9685-ff5bb260df2e 4f971e89-eebd-4455-a8de-9e59040e7347 5ca83367-6e45-459f-a27b-476b1d01c936 0"
doNothingLine2 = "powercfg -SETDCVALUEINDEX 381b4222-f694-41f0-9685-ff5bb260df2e 4f971e89- eebd-4455-a8de-9e59040e7347 5ca83367-6e45-459f-a27b-476b1d01c936 0"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("ToggleLidAction.bat", ForReading)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
strDoNothing = InStr(strLine, "powercfg -SETDCVALUEINDEX 381b4222-f694-41f0-9685- ff5bb260df2e 4f971e89-eebd-4455-a8de-9e59040e7347 5ca83367-6e45-459f-a27b-476b1d01c936 0")
strSleep = InStr(strLine, "powercfg -SETDCVALUEINDEX 381b4222-f694-41f0-9685- ff5bb260df2e 4f971e89-eebd-4455-a8de-9e59040e7347 5ca83367-6e45-459f-a27b-476b1d01c936 1")
Loop
objFile.Close
Set obj = CreateObject("Scripting.FileSystemObject")
obj.DeleteFile("ToggleLidAction.bat")
If strDoNothing Then
outPutFile = "ToggleLidAction2.bat"
Set objOutFile = objFSO.CreateTextFile(outPutFile, ForAppending)
objOutFile.WriteLine "" & sleepLine1 & ""
objOutFile.WriteLine "" & sleepLine2 & ""
MsgBox ("The System will now Sleep when the lid is closed.")
Else
If strSleep Then
outPutFile = "ToggleLidAction2.bat"
Set objOutFile = objFSO.CreateTextFile(outPutFile,ForAppending)
objOutFile.WriteLine "" & doNothingLine1 & ""
objOutFile.WriteLine "" & doNothingLine2 & ""
MsgBox ("The System will now continue running when the lid is closed.")
End If
End If
objOutFile.Close
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "ToggleLidAction2.bat C:\WINDOWS\system32\cmd.exe", 0
答案 0 :(得分:1)
如果该行上方的部分中的任何一个条件均不满足,则objOutFile不会被实例化,因此无法关闭。您将在不存在的对象上调用Close。我的VBScript有点生疏,但我相信对其进行检查似乎像If Not objOutFile Is Nothing Then ObjOutFile.Close