Hello:

I am trying to understand the meaning of a certain line of code for a
script that queries the event log and then writes it to a text file. I
found the script on the web. I would appreciate if someone could
explain the function of the following command in this script:

set objFile = nothing
set objFolder = nothing

What does the above do and what's it for? The relevant parts of the
script deailing with the file system object is listed below. THank you.

TIA

- Raheem

PS. The script below is from this excellent site on scripting
http://www.computerperformance.co.uk

----> Begin Script

Option Explicit

Dim objFSO, objFolder, objFile ' Objects
Dim strComputer, strFileName, strFolder, strPath ' strings

' --------------------------------------------------------
' Set the folder and file name
strComputer = "."
strFileName = "\Event672.txt"
strFolder = "e:\logs"
strPath = strFolder & strFileName

' -----------------------------------------------------
' Section to create folder and hold file.
' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Check that the strFolder folder exists
If objFSO.FolderExists(strFolder) Then
Set objFolder = objFSO.GetFolder(strFolder)
Else
Set objFolder = objFSO.CreateFolder(strFolder)
WScript.Echo "Just created " & strFolder
End If

If objFSO.FileExists(strFolder & strFileName) Then
Set objFolder = objFSO.GetFolder(strFolder)
Else
Set objFile = objFSO.CreateTextFile(strFolder & strFileName)
Wscript.Echo "Just created " & strFolder & strFileName
End If
' --------------------------------------------------
' Two tiny but vital commands (Try script without)
set objFile = nothing
set objFolder = nothing

' ----------------------------------------------------
' Write the information to the file
Set strFileName = objFSO.CreateTextFile(strPath, True)
strFileName.WriteLine("Computer to test " & strComputer)
Wscript.Echo "Check " & strPath

WScript.Quit

' End of Guy's FSO sample VBScript

<---- End Script

Re: What does "set objFile = nothing" mean? by mayayana

mayayana
Sun Jul 16 16:09:08 CDT 2006

Setting to Nothing releases the object from
memory. Theoretically, the Windows Script
Host will take care of that eventually, but you
can also do it explicitly. See here for a long
list of pro and con arguments about its use:

http://blogs.msdn.com/ericlippert/archive/2004/04/28/122259.aspx

The gist of it is that some people like to set
objects to nothing for the sake of thoroughness
(and just in case there's a bug involved that
may prevent object release) while others think
the method is superfluous and makes code
unnecessarily busy-looking.
>
> I am trying to understand the meaning of a certain line of code for a
> script that queries the event log and then writes it to a text file. I
> found the script on the web. I would appreciate if someone could
> explain the function of the following command in this script:
>
> set objFile = nothing
> set objFolder = nothing
>
> What does the above do and what's it for? The relevant parts of the
> script deailing with the file system object is listed below. THank you.
>
> TIA
>
> - Raheem
>
> PS. The script below is from this excellent site on scripting
> http://www.computerperformance.co.uk
>
> ----> Begin Script
>
> Option Explicit
>
> Dim objFSO, objFolder, objFile ' Objects
> Dim strComputer, strFileName, strFolder, strPath ' strings
>
> ' --------------------------------------------------------
> ' Set the folder and file name
> strComputer = "."
> strFileName = "\Event672.txt"
> strFolder = "e:\logs"
> strPath = strFolder & strFileName
>
> ' -----------------------------------------------------
> ' Section to create folder and hold file.
> ' Create the File System Object
> Set objFSO = CreateObject("Scripting.FileSystemObject")
>
> ' Check that the strFolder folder exists
> If objFSO.FolderExists(strFolder) Then
> Set objFolder = objFSO.GetFolder(strFolder)
> Else
> Set objFolder = objFSO.CreateFolder(strFolder)
> WScript.Echo "Just created " & strFolder
> End If
>
> If objFSO.FileExists(strFolder & strFileName) Then
> Set objFolder = objFSO.GetFolder(strFolder)
> Else
> Set objFile = objFSO.CreateTextFile(strFolder & strFileName)
> Wscript.Echo "Just created " & strFolder & strFileName
> End If
> ' --------------------------------------------------
> ' Two tiny but vital commands (Try script without)
> set objFile = nothing
> set objFolder = nothing
>
> ' ----------------------------------------------------
> ' Write the information to the file
> Set strFileName = objFSO.CreateTextFile(strPath, True)
> strFileName.WriteLine("Computer to test " & strComputer)
> Wscript.Echo "Check " & strPath
>
> WScript.Quit
>
> ' End of Guy's FSO sample VBScript
>
> <---- End Script
>



Re: What does "set objFile = nothing" mean? by Pandu

Pandu
Sun Jul 16 18:11:00 CDT 2006

Mayayana:

Thanks so much for that explanation and the great link! its nice to
know what the heck was going on with those two lines of code ...
apparently not much :)

- Raheem

mayayana wrote:
> Setting to Nothing releases the object from
> memory. Theoretically, the Windows Script
> Host will take care of that eventually, but you
> can also do it explicitly. See here for a long
> list of pro and con arguments about its use:
>
> http://blogs.msdn.com/ericlippert/archive/2004/04/28/122259.aspx
>
> The gist of it is that some people like to set
> objects to nothing for the sake of thoroughness
> (and just in case there's a bug involved that
> may prevent object release) while others think
> the method is superfluous and makes code
> unnecessarily busy-looking.
> >
> > I am trying to understand the meaning of a certain line of code for a
> > script that queries the event log and then writes it to a text file. I
> > found the script on the web. I would appreciate if someone could
> > explain the function of the following command in this script:
> >
> > set objFile = nothing
> > set objFolder = nothing
> >
> > What does the above do and what's it for? The relevant parts of the
> > script deailing with the file system object is listed below. THank you.
> >
> > TIA
> >
> > - Raheem
> >
> > PS. The script below is from this excellent site on scripting
> > http://www.computerperformance.co.uk
> >
> > ----> Begin Script
> >
> > Option Explicit
> >
> > Dim objFSO, objFolder, objFile ' Objects
> > Dim strComputer, strFileName, strFolder, strPath ' strings
> >
> > ' --------------------------------------------------------
> > ' Set the folder and file name
> > strComputer = "."
> > strFileName = "\Event672.txt"
> > strFolder = "e:\logs"
> > strPath = strFolder & strFileName
> >
> > ' -----------------------------------------------------
> > ' Section to create folder and hold file.
> > ' Create the File System Object
> > Set objFSO = CreateObject("Scripting.FileSystemObject")
> >
> > ' Check that the strFolder folder exists
> > If objFSO.FolderExists(strFolder) Then
> > Set objFolder = objFSO.GetFolder(strFolder)
> > Else
> > Set objFolder = objFSO.CreateFolder(strFolder)
> > WScript.Echo "Just created " & strFolder
> > End If
> >
> > If objFSO.FileExists(strFolder & strFileName) Then
> > Set objFolder = objFSO.GetFolder(strFolder)
> > Else
> > Set objFile = objFSO.CreateTextFile(strFolder & strFileName)
> > Wscript.Echo "Just created " & strFolder & strFileName
> > End If
> > ' --------------------------------------------------
> > ' Two tiny but vital commands (Try script without)
> > set objFile = nothing
> > set objFolder = nothing
> >
> > ' ----------------------------------------------------
> > ' Write the information to the file
> > Set strFileName = objFSO.CreateTextFile(strPath, True)
> > strFileName.WriteLine("Computer to test " & strComputer)
> > Wscript.Echo "Check " & strPath
> >
> > WScript.Quit
> >
> > ' End of Guy's FSO sample VBScript
> >
> > <---- End Script
> >


Re: What does "set objFile = nothing" mean? by mayayana

mayayana
Sun Jul 16 22:13:35 CDT 2006



> Thanks so much for that explanation and the great link! its nice to
> know what the heck was going on with those two lines of code ...
> apparently not much :)
>

Indeed. But it's a "not much" that generates
surprisingly vehement opinions. :)