Introduction: Intro to VB Script: a Beginners Guide : Part 2 : Working With Files

About: im just a regular guy that like regular things

Well in my last VBScript instructable, I went over how to make a script to shut off your internet to play Xbox360. Today I have a different problem. My computer has been shutting down at random times and I want to log every time that the computer reboots into a file.

I have a feeling that the problem is the video card but I want to make sure. I leave the computer on at all times but cannot monitor it 24/7, so my easiest solution is to write a time stamp to a file every time it reboots. Because it gives me a BSOD every time, logging when it shuts down will be hard so I have to get it when it comes back on. That's the easy part.

Because of the fact that this is just temporary until I figure out what is wrong with the computer, I am not going to write to the registry or make it a service so it will start up at boot time. And to get it to reboot every time it shuts down I will have to edit the bios.

Step 1: Editing the Bios to Get It to Reboot on Power Fail.

I am not sure if my problem is with the power supply failing or with my video card or what it is. But I do know that the computer reboots every so often. So to make sure it reboots after a power fail (IE:the power supply browns out) I want to edit the bios.

Because of the fact that there are so many different bios makers out there, it would be too hard to list all the different ways to do this in all the different bios screens so I am just going to give you the basics and let you figure out the specifics on your own.

Basically to get into your bios you will need to press a button on boot. Usually it will say something like "hit Del to enter setup" or "F2". Different manufacturers give you different ways but it is usually either "F2" or "Del" key.

Either way watch for it and hit the key it tells you to hit. Now you will come to a screen that is usually either grey or blue, but again it depends on your bios make. You will be looking for a power settings option. It will either be on a window all its own like in the AMIBIOS screen shot (borrowed from toms hardware guide) or in some it may be in an advanced bios features window. Anyway you are looking for a "restore on ac/power loss" setting. It may be named something else but it will be close to that. Just look for it. It will usually have three options (last state, on after power loss, off after power loss). You want to set it to on after power loss. Make sure you save your settings and exit the bios.

Now when your power fails or you unplug your machine it will reboot as soon as it gets power back.

Yeahhhhhh!!!!!!!!!!!!!!!!

Step 2: Writing the Script Part 1: Making a Plan

First thing we want to do is create a plan.

What exactly do we want the script to do.

A.) When the power fails or the BSOD comes up we want it to reboot. (Taken care of in step 1 Editing the Bios
B.) As soon as it reboots we want to write to a file the exact date and time it rebooted.
C.) We want to be able to amend that file. If it reboots more than once we would never know.
D.) We want it to have error correction so it wont screw up and we make sure we get all of our reboots without missing any because of bad coding.
E.) We want it to pop up a message saying it rebooted.
F.) We want it to open the file we wrote to so we can see a list of all reboots when we click ok on the message pop up.

Step 3: Writing the Script Part 2: Creating Your Script an Assigning the Variables.

Ok first things first, create a text document and rename it to any_name.vbs. I used shutdown_catcher.vbs but you can use anything.

Now just right click the vbs file and hit edit.

If you use notepad++ now would be a good time to open it, if you don't but plan to do any amount of coding you should look into getting it.

Ok now first thing we want to do is create variables. Variables are basically just a place holder so we don't have to write a whole bunch of stuff a bunch of times.

example:
we have a sentence that says "the quick brown fox jumps over the lazy dogs back". but it is in our code 20 times. we can either type out the whole sentence 20 times or create a variable and assign the sentence to that variable

So in this example we create the variable strSentence then assign "the quick brown fox jumps over the lazy dogs back" to that variable. Now all we have to do is type strSentence every time we need the sentence and it knows to type "the quick brown fox jumps over the lazy dogs back"

how do we do it:
If we have multiple variables of the same type we can put them on the same line and separate the variables by commas. To create a variable we need to do what is called dimensioning the variable. To do this we type Dim before the variable then we put a prefix on the variable so we know what the variable represents.

Examples are
str for a string variable
obj for an object variable
const for a constant....
The list goes on and on. these are the ones we will use in this script so I will explain them a little. a string is usually text like a sentence. An object is usually something physical like a file or a program. and a constant is something that will always have the same meaning throughout the script.

NOTE:ANY TIME A LINE HAS A SINGLE ' BEFORE IT THE LINE WILL BE IGNORED BY THE SCRIPT.THEY ARE CALLED REMARKS AND ARE USUALLY PUT IN BY THE PROGRAMMER SO THEY CAN REMEMBER WHAT THEY DID LATER WHEN REVIEWING THE CODE. I WILL USE THEM TO MAKE COMMENTS ABOUT THE CODE SO YOU CAN SEE WHY I DID STUFF THE WAY I DID. AND THEY WILL BE ABOVE THE LINE THEY ARE COMMENTING ABOUT.

Actual Usage:

'this line forces you to define all variables. Without it any undefined variable will be treated as an object
Option Explicit

'now we create 5 variables for the folders and files we will need
'name them how you want but the convention is preName
Dim objFiSyOb, objSysFold, objShell, objFile, objTextFile

'now we need to create string variables for the directory, file and file name
Dim strDir, strFile, strFileName

'now assign values to the variables
'the strDir is our directory we want the folder in
strDir = "C:\Catcher"
'the strFile is where we want the text document and what it is named
strFile = "\Shutdown_catcher.txt"
'the strfile name is so we have the name of the file for use in pop ups
strFileName ="Shutdown_catcher.txt"

'next we have to assign a constant for accessing the file we have three options
'we will only be using the appending in this code but it is nice to know all of them.
Const forReading=1, forWriting = 2, ForAppending = 8

'lastly we need to set a variable to the file system object so we can call it later when we need it
Set objFiSyOb = CreateObject("Scripting.FileSystemObject")

Step 4: Explanations in Between

Now that we have our variables dimensioned all we have to do is recall that specific variable each time. This not only cuts down on the amount of typing we do but also creates less mistakes because of misspellings.

Now technically all we have to do is type the following code and we are done

'set the file for appending
Set objTextFile = objFiSyOb.OpenTextFile (strDir & strFile, ForAppending, True)
'write the new line to the file
objTextFile.WriteLine("Your computer Shut down on " & date & " at " & time)
'close the file
objTextFile.Close
'make a pop up
Wscript.Echo ("Your computer Shut down on " & date & " at " & time)
'open the file in IE
Set objShell = CreateObject("WScript.Shell")
objShell.run ("Explorer" &" " & strDir & "\shutdown_catcher.txt" )

The problem is it will have no error correction, and any simple error like file not existing or variable already assigned will crash the script.

Step 5: Writing the Script Part 3: If Then Statements As Error Correction.

The easiest way to do error correction is to use if/then statements.

Basically an if then statement says if something happens then do this otherwise do that. By nesting if/then statements you can create a string of things that have to happen. If a and b and c happen then do this else do that.

example:
we want to make sure the file we want to write to is there
if we find it we want to do nothing
if we dont find it we want to create it

or

we want to check if the folder is there and if not create it
and then check if the file is there once we find the folder
and if not create it

the first one is an example of a if then statement the second is a nested if then.

How we do it:
there are four parts to an if then statement
if/then
else
end if

it is basically if this is true then do this otherwise do that then end the statement
a properly coded if then statement would be :

If something = something Then
do what you need done
Else
do something else
End If

You can use any argument as long as it is true it will follow the then part and ignore the else part but if it is false it will skip the then and go to else

Then when we end the if statement we put "end if", but if we have nested if statements we have to end every if statement. It will end them from child to parent so the first "end if" ends the if statement inside the parent, where the second one ends the original (parent) if statement.

To make a pop up we just type
Wscript.Echo ("whatever we want to say in the message ")
to add a variables value into it we put it outside or in between double quotes using the ampersand (&)sign as a delimiter so the script knows it is a variable and not a text.
like this:
Wscript.Echo ("Blah blah blah " & strDir & " blah blah blah "& strFilename & " blah blah blah." )
The output would be a pop up that said
Blah blah blah C:\Catcher blah blah blah shutdown_catcher.txt blah blah blah.

Actual Usage:
'if the folder C;\exist
If objFiSyOb.FolderExists(strDir) Then
'get folder
Set objSysFold = objFiSyOb.GetFolder(strDir)
'otherwise
Else
'create the folder c;\catcher
Set objSysFold = objFiSyOb.CreateFolder(strDir)
'and check to see if the file and folder exists
If objFiSyOb.FileExists(strDir & strFile) Then
'if it does get folder c:\catcher
Set objSysFold = objFiSyOb.GetFolder(strDir)
'otherwise
Else
'create text document shutdown_catcher.txt
Set objFile = objFiSyOb.CreateTextFile(strDir & strFile)
'then pop up a message to say you created them both
Wscript.Echo ("We have created a folder named " & strDir & " and a file named "& strFilename & " and placed it in the " & strDir & strFile & " directory." )
'end nested if statement
End If
'end original if statement
End If

'now in the first if statement we already checked for folder and found it so now we check for file
If objFiSyOb.FileExists(strDir & strFile) Then
'if found get folder
Set objSysFold = objFiSyOb.GetFolder(strDir)
'otherwise
Else
'create the text doc
Set objFile = objFiSyOb.CreateTextFile(strDir & strFile)
'create a popup saying we created the text document
Wscript.Echo ("We have created a file named " & strFileName & " in the " & strDir & "file folder.")
End If

Step 6: Explanations in Between

If you look closely at the code from the previous step you will see both a nested and un-nested if then statement. The first one is a nested statement. It checks for the folder and file and if doesn't find them creates both. The second looks for just a file and if it doesn't find it creates just the file.

One might say why both. Well what happens if the file is deleted but the folder isn't. The first if statement would skip right over the creating of the file because it found the folder and would never actuate the else part of the statement. So when you went to write to the file it would error out because the file would be missing. So by having the second one there it solves this problem.

Technically you could take the nested statement out because it is almost the same as the second if then statement but then you would only have one pop up and it wouldn't be as personable

Plus it gave me a chance to show you nested as opposed to un-nested if statements.


Step 7: More Error Correction

Now that we have completed the majority of the error correction we want to write to the file and show the pop ups.

The problem is we will get permission errors if we don't release the reference of the previous object stored in the variable. So we want to put in a small snippet of code that says set the variables to nothing.

looks like this:
set objFile = nothing
set objSysFold = nothing

So for those of you following along at home your code to this point should be (I took the comment out):

Option Explicit
Dim objFiSyOb, objSysFold, objShell, objFile, objTextFile
Dim strDir, strFile, strFileName
strDir = "C:\Catcher"
strFile = "\Shutdown_catcher.txt"
strFileName ="Shutdown_catcher.txt"
Const forReading=1, forWriting = 2, ForAppending = 8

Set objFiSyOb = CreateObject("Scripting.FileSystemObject")

If objFiSyOb.FolderExists(strDir) Then
Set objSysFold = objFiSyOb.GetFolder(strDir)
Else
Set objSysFold = objFiSyOb.CreateFolder(strDir)
If objFiSyOb.FileExists(strDir & strFile) Then
Set objSysFold = objFiSyOb.GetFolder(strDir)
Else
Set objFile = objFiSyOb.CreateTextFile(strDir & strFile)
Wscript.Echo ("We have created a folder named " & strDir & " and a file named "& strFilename & " and placed it in the " & strDir & strFile & " directory." )
End If
End If

If objFiSyOb.FileExists(strDir & strFile) Then
Set objSysFold = objFiSyOb.GetFolder(strDir)
Else
Set objFile = objFiSyOb.CreateTextFile(strDir & strFile)
Wscript.Echo ("We have created a file named " & strFileName & " in the " & strDir & "file folder.")
End If

set objFile = nothing
set objSysFold = nothing

Step 8: Writing the Script Part 4: Setting Attributes

Now we need to set the attributes to the file so we can amend it. Previously we set three constant variables:
forReading=1
forWriting=2
forAppending=8

if you set it to forReading you will be able to read it but not write to it.
if you set it to forWriting you will overwrite the existing file everytime you run the program.
If you set it to forAppending you will add a new line every time the script executes to the document.

Since we want a log of every time it reboots we want it to be appended and not over writen so we use forAppending.

How we do it:
you have to name the object you want to set attributes to. Since we have already done most of the work in the previous steps we just need to recall variables at this point. Then just set it to true.

Actual Usage:
'object to set = filesystemobject.action(directory+name of file, attribute, true or false)
Set objTextFile = objFiSyOb.OpenTextFile (strDir & strFile, ForAppending, True)

Step 9: Writing the Script Part 5: Writing to File

Writing to a file is a lot like writing a pop up. You just put :
objTextFile.WriteLine("Whatever you want to say " & Variable to pull in to write line)

The only difference is the first part.
In a pop up you put :
Wscript.echo
in a write line you put:
objTextFile.WriteLine

And in a write line you have to close the write line so it knows to stop writing and for that you use:
objTextFile.Close

Actual Usage:

objTextFile.WriteLine("Your computer Shut down on " & date & " at " & time)
objTextFile.close

'now add in a popup to say you wrote to the file so the person knows it happened
'and they have to close it to open the file and view
Wscript.Echo ("Your computer Shut down on " & date & " at " & time)

Step 10: Writing the Script Part 5: Opening the File for Viewing.

The script will at this point stop until someone clicks the pop up. Once you get past the pop up you can put a line in that opens the file. Using some error correction on it helps to keep the program running nicely.

When you create an error in vbscript, it adds a value to the "err.number" variable which is a pre defined variable in vbscript. You don't have to define it and can use it just like any other variable as long as you don't violate its rules. The value assigned to the err.number variable is the code for the error that occurred. Now you can either check for every error code known in the vbscript database and make sure it didn't occur. Or easier, is just checking tho make sure the err.number variable is empty. if it is empty no errors occurred. To do that we just check to see if the variable is equal to vbEmpty.

So in our script we want to use another if then statement. And check to see if an error occurred and if it didn't open the file for viewing, but if it did then pop up a message with the error number in it so we can debug.

Then when we are all done with our script we type Wscript.Quit. This tells the script to terminate itself. This helps keep down the memory leaks and unwanted processes running.

How its used:
if err.number is equal to vbEmpty then
create a shell
then run the program from the shell
otherwise
pop up a message saying there was an error
end if.

Actual Usage:
'check for errors
If err.number = vbEmpty then
'if none create a shell
Set objShell = CreateObject("WScript.Shell")
'then open the file from that shell using explorer. it will actually open in IE though
objShell.run ("Explorer" &" " & strDir & "\shutdown_catcher.txt" )
'otherwise pop up a message with the error number
Else WScript.echo "VBScript Error: " & err.number
End If

'end script
Wscript.quit

Step 11: Getting the Script to Run on Startup.

There is a file folder in the start menu called Startup. Anything placed into this folder will start up as soon as the computer is started. By placing the script file in the startup folder it will execute every time the computer is rebooted whether intentional or not. Since I am only using this as a diagnostic tool, it need not be made into a service to start, and I need not put it into the registry to start it, so this will be fine. Once I fix the computer I can delete the file from the startup folder and there is no other cleanup required.

Step 12: Running the Program

When you execute the vbscript file it will now

A.) Check to see if the folder C:\Catcher exists and if not create it.
B:) Check to see if the file Shutdown_catcher.txt exists and if not create it.
C:) If it created either a folder or file or both it will pop up a message saying it did create them (Note: it should only do this on first run after that it should already be there unless deleted so you should only see that message once. It might be a good idea to run this script once under a controlled reboot so you can bypass these pop ups or you may not write anything to the file because it wont bypass the file created pop ups.)
D:) Write a line in the text document Shutdown_Catcher.txt saying that the computer shutdown on such and such a date at such and such a time.
E:) Pop up a message saying there was a reboot
F:) Open the file for viewing.

If you look in the next step you will see the code as written.

Now all I have to do is sit back and wait. Every time the computer reboots I will have a record of it. Then I can review the record and see if there is any consistency to it. Example might be that it reboots every half hour or it reboots any time there is a increment or 45 on the clock.

This isn't by any means the only way I am checking over the computer, just so you know. I also have a benchmarking program running to check temps so I know how hot it is when it shuts down, voltage so I know if the power supply is failing, and a few other helpful things.

Well I hope this was helpful. I know I enjoy it so maybe others will too. If you have any questions feel free to ask. I may not get to them immediately but withing a little bit and i will answer you . Depends on how my day/week is doing.

Step 13: The Code

This is the code as written without all of the hints and remarks

_

Option Explicit
Dim objFiSyOb, objSysFold, objShell, objFile, objTextFile
Dim strDir, strFile, strFileName
strDir = "C:\Catcher"
strFile = "\Shutdown_catcher.txt"
strFileName ="Shutdown_catcher.txt"
Const forReading=1, forWriting = 2, ForAppending = 8

Set objFiSyOb = CreateObject("Scripting.FileSystemObject")

If objFiSyOb.FolderExists(strDir) Then
Set objSysFold = objFiSyOb.GetFolder(strDir)
Else
Set objSysFold = objFiSyOb.CreateFolder(strDir)
If objFiSyOb.FileExists(strDir & strFile) Then
Set objSysFold = objFiSyOb.GetFolder(strDir)
Else
Set objFile = objFiSyOb.CreateTextFile(strDir & strFile)
Wscript.Echo ("We have created a folder named " & strDir & " and a file named "& strFilename & " and placed it in the " & strDir & strFile & " directory." )
End If
End If

If objFiSyOb.FileExists(strDir & strFile) Then
Set objSysFold = objFiSyOb.GetFolder(strDir)
Else
Set objFile = objFiSyOb.CreateTextFile(strDir & strFile)
Wscript.Echo ("We have created a file named " & strFileName & " in the " & strDir & "file folder.")
End If

set objFile = nothing
set objSysFold = nothing
Set objTextFile = objFiSyOb.OpenTextFile (strDir & strFile, ForAppending, True)

objTextFile.WriteLine("Your computer Shut down on " & date & " at " & time)
objTextFile.Close
Wscript.Echo ("Your computer Shut down on " & date & " at " & time)

If err.number = vbEmpty then
Set objShell = CreateObject("WScript.Shell")
objShell.run ("Explorer" &" " & strDir & "\shutdown_catcher.txt" )
Else WScript.echo "VBScript Error: " & err.number
End If

WScript.Quit