<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The VBScript Network and Systems Administrator&#039;s Cafe &#187; Log files</title>
	<atom:link href="http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/tag/log-files/feed/" rel="self" type="application/rss+xml" />
	<link>http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator</link>
	<description></description>
	<lastBuildDate>Tue, 11 Oct 2011 18:36:53 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Creating text files using the Scripting.FileSystemObject with VBScript</title>
		<link>http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/creating-text-files-using-the-scriptingfilesystemobject-with-vbscript/</link>
		<comments>http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/creating-text-files-using-the-scriptingfilesystemobject-with-vbscript/#comments</comments>
		<pubDate>Thu, 17 Apr 2008 20:52:08 +0000</pubDate>
		<dc:creator>Jerry Lees</dc:creator>
				<category><![CDATA[File System Object]]></category>
		<category><![CDATA[FSO]]></category>
		<category><![CDATA[Functions]]></category>
		<category><![CDATA[Log files]]></category>
		<category><![CDATA[Scripting.FileSystemObject]]></category>
		<category><![CDATA[VBScript]]></category>

		<guid isPermaLink="false">http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/creating-text-files-using-the-scriptingfilesystemobject-with-vbscript/</guid>
		<description><![CDATA[Well, I didn&#8217;t expect the cat to get out of the bag quite so quickly! (Congratulations go out to Stoinov for getting the correct answer I was thinking of so very quickly!) So while he&#8217;s been sleeping during his night, which seems to be my day&#8230; I thought I&#8217;d throw together a quick &#8220;Write a text file&#8221; script together [...]]]></description>
				<content:encoded><![CDATA[<p><font color="#0000ff"><font color="#000000">Well, I didn&#8217;t expect the cat to get out of the bag quite so quickly!</font> <font color="#000000">(Congratulations go out to</font> <a href="http://itknowledgeexchange.techtarget.com/profile/Stoinov">Stoinov</a><font color="#000000"> <font color="#000000">for getting the correct answer I was thinking of so very quickly!) So while he&#8217;s been sleeping during his night, which seems to be my day&#8230; I thought I&#8217;d throw together a quick &#8220;Write a text file&#8221; script together for you all. <em>(A little trivia for you all in the United States, that I found hard to believe when I learned it&#8230; VBScript, and VB for that matter, is in English regardless of where you are in the world. If you&#8217;re on a Japanese system writing VBScript, your using the same language as us folks in the United States. Talk about portable code!)</em></font></font></font></p>
<p>Back to the matter at hand, this script doesn&#8217;t do anything terribly spectacular&#8211; by itself. All it does is opens (or creates) a text file named what you call it where you specify. The heart of the script is the <font color="#0000ff">WriteTextFile () </font>function, which accepts two pieces of data; <strong>a full path to a file name</strong> and <strong>the data you want to write</strong>. That&#8217;s it! But&#8212;- this function will let you write logs, CSV (Comma Separated Value) files or any other form of text file from any type of script for which you choose to use it!</p>
<p>A quick little note on the access method I hard coded into the script. You  will notice three constants used<font color="#0000ff"> ForReading<font color="#000000">,</font> ForWriting<font color="#000000">, and</font> ForAppending </font>these are there so you could change the mode easily without having to go to the documentation for the <a target="_blank" href="http://msdn2.microsoft.com/en-us/library/314cz14s(VS.85).aspx">OpenTextFile Method</a> of the <a target="_blank" href="http://msdn2.microsoft.com/en-us/library/6kxy1a51(VS.85).aspx">Scripting.FileSystemObject</a> and figure out what numbers to use to change the access method. you simply just need to change the access method from ForReading on the OpenTextFile line of the script to accomplish this task. An excerpt of the Microsoft documentation for the values and descriptions are listed below, for your reference.</p>
<table border="2" width="100%">
<tr>
<th>Constant</th>
<th>Value</th>
<th>Description</th>
</tr>
<tr>
<td>ForReading</td>
<td>1</td>
<td>Open a file for reading only. You can&#8217;t write to this file.</td>
</tr>
<tr>
<td>ForWriting</td>
<td>2</td>
<td>Open a file for writing.</td>
</tr>
<tr>
<td>ForAppending</td>
<td>8</td>
<td>Open a file and write to the end of the file.</td>
</tr>
</table>
<p>Notice it has a ForAppending attribute? If you wanted to &#8230; say log a series of collected values over a number of calls to the script you would have to change the access method to <em>appending</em>, however, keep in mind that ForAppending assumes the file already exists and you will receive an error if it doesn&#8217;t.</p>
<p>So, here is the script to write a file&#8230;</p>
<p><font color="#0000ff">Option Explicit<br />
Dim Error</font></p>
<p><font color="#0000ff">Error = WriteTextFile (&#8220;c:\test.txt&#8221;, &#8220;I created a text file with VBScript!&#8221;)<br />
If Error = 0 Then<br />
    WScript.Echo &#8220;File Written Correctly.&#8221;<br />
Else<br />
    WScript.Echo&#8221;Error number &#8221; &amp; Error &amp; &#8221; occurred.&#8221;<br />
End If</font></p>
<p><font color="#0000ff">Function WriteTextFile (OutputFile, Data)<br />
    Dim wrtlog, fso<br />
 <br />
    &#8217;these are constants for the OpenTextFile method&#8217;s file access modes<br />
    Const ForReading = 1, ForWriting = 2, ForAppending = 8<br />
  <br />
    On Error Resume Next<br />
    Set fso = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)<br />
    Set wrtlog = fso.OpenTextFile (OutputFile, ForWriting , true)</font></p>
<p><font color="#0000ff">    wrtlog.WriteLine(Data)<br />
    WriteTextFile = Err.Number &#8216;You can use Err.Description here to debug.<br />
End Function</font></p>
<p><font color="#000000">That&#8217;s it. Small, but a <strong><em>VERY</em></strong> powerful tool to add to your tool belt in scripting.</font></p>
<p><font color="#0000ff"><font color="#0000ff"><font color="#000000"><font color="#0000ff"><font color="#000000">As always, this code works perfectly. However, sometimes the formatting of the blog breaks the code if you copy and paste it into your editor. So, if you’d like to not type or troubleshoot any syntax errors due to the copy and paste problems– I’ve provided the code for download, plus example output files  from my final tests for you. You’ll find the code and other files available for download from my website’s (<a href="http://www.websystemsadministration.com/">www.websystemsadministration.com</a>) File Depot under the <font color="#0000ff"><em>ITKE Blog Scripts</em></font> category.<font color="#0000ff"> </font><font color="#0000ff"><font color="#0000ff"><font color="#000000">Enjoy and happy scripting!</font></font></font></font></font></p>
<p><!--a href="http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/pinging-a-remote-computer-from-another-remote-computer-using-the-wmi-win32_pingstatus-class/trackback/" title="Trackback URL"&gt;Trackback URL</a>&#8211;></font></font></font><font color="#0000ff"><font color="#0000ff"><font color="#ff0000">Extra credit:</font> <font color="#000000">Can you modify this function to accept a third parameter for the access method?</font></font></font><font color="#0000ff"><font color="#0000ff"><font color="#000000"> I invite you to leave a comment and I’ll let you know if your on the same track as I am. Comments are always welcome!</font></font></font></p>
<p><!--a href="http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/vbscript-to-check-cpu-and-processor-performance-counter-statistics-using-wmi/trackback/" title="Trackback URL"&amp;gt;Trackback URL</a>&#8211;></p>
<!-- wpms-network-global-inserts -->]]></content:encoded>
			<wfw:commentRss>http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/creating-text-files-using-the-scriptingfilesystemobject-with-vbscript/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
