Trouble with basic VBScript that writes on a text file
Trouble with basic VBScript that writes on a text file
I've copied an old VBS code from here, but it seems to be not working.
I know nothing of VBS so I'm lost here.
It seems to be very simple, it opens a text file, changes the values according to what was written before (it just "flips a bit"), removes the old file then writes the new one.
It seems to be having issues with closing the file it created, and it never creates
It's giving an error on Line 42, with the code
800A01A8, "Object Required: '' ".
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:WINDOWSsystem32cmd.exe", 0
That's the copy from the source, verbatim. I know nothing of VBS, but I think the first end if is from "if strDoNothing", and the second is from "else if"
– Gabriel Torreiro de Moraes
Jun 29 at 22:44
@GabrielTorreirodeMoraes if you are trying to learn VBScript this isn't a good place to start. On Stack Overflow the OP is expected to atleast know the topic they are posting about, you have said you don't know VBScript which raises a red flag instantly. How can we help you through a problem if you don't understand the source material?
– Lankymart
Jun 30 at 7:20
I'm not trying to learn VBScript right now. This was just a simple script I've found in an old StackOverflow post (which the OP said it worked) and I thought I could get it working by asking what's wrong with it here.
– Gabriel Torreiro de Moraes
2 days ago
@GabrielTorreirodeMoraes You've just made my point for me.
– Lankymart
yesterday
1 Answer
1
If neither of the conditions in the section above that line are met, objOutFile is never instantiated and can therefore not be closed. You'd be calling Close on a non-existing object. My VBScript is a little rusty but I believe a check for it would look something like If Not objOutFile Is Nothing Then ObjOutFile.Close
If Not objOutFile Is Nothing Then ObjOutFile.Close
a non-existing object like what? The "ToggleLidAction.bat" file? Through the code, it deletes the file, in order to replace it with its new written file. Even if I disable the file delete, it still fails with the same error code
– Gabriel Torreiro de Moraes
Jun 29 at 22:46
@GabrielTorreirodeMoraes A non-existing object like the
objOutFile
that your code didn't open. objOutFile
is only opened if strDoNothing
or strSleep
evaluate to True
. When both variables evaluate to False
the file is never opened, so you're getting an error when trying to close a file that you didn't open before.– Ansgar Wiechers
Jun 29 at 23:23
objOutFile
objOutFile
strDoNothing
strSleep
True
False
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
I see two end if, but only one if, are there code parts missing?
– LotPings
Jun 29 at 22:25