 




<?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; Scripting.FileSystemObject</title>
	<atom:link href="http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/tag/scriptingfilesystemobject/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>Getting disk usage data with the filesystem object and the Excel.Application object in VBScript</title>
		<link>http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/getting-disk-usage-data-with-the-filesystem-object-and-the-excelapplication-object-in-vbscript/</link>
		<comments>http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/getting-disk-usage-data-with-the-filesystem-object-and-the-excelapplication-object-in-vbscript/#comments</comments>
		<pubDate>Wed, 14 May 2008 20:58:25 +0000</pubDate>
		<dc:creator>Jerry Lees</dc:creator>
				<category><![CDATA[Disk usage]]></category>
		<category><![CDATA[Documentation]]></category>
		<category><![CDATA[Excel.Application]]></category>
		<category><![CDATA[File System Object]]></category>
		<category><![CDATA[FSO]]></category>
		<category><![CDATA[Functions]]></category>
		<category><![CDATA[Scripting.FileSystemObject]]></category>
		<category><![CDATA[system trending]]></category>
		<category><![CDATA[VBScript]]></category>

		<guid isPermaLink="false">http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/getting-disk-usage-data-with-the-filesystem-object-and-the-excelapplication-object-in-vbscript/</guid>
		<description><![CDATA[In my previous posting, entitled Working with the Excel.Application object in VBScript to create Excel Spreadsheets, we worked with excel in VBScript to create a excel spreadsheet. The spreadsheet wasn&#8217;t going to win any awards for complexity or usefulness, but none the less it was a spreadsheet&#8211; and more importantly it was generated by a [...]]]></description>
				<content:encoded><![CDATA[<p>In my previous posting, entitled <a rel="bookmark" href="http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/working-with-the-excelapplication-object-in-vbscript-to-create-excel-spreadsheets/" title="Permanent Link to Working with the Excel.Application object in vbscript to create Excel Spreadsheets">Working with the Excel.Application object in VBScript to create Excel Spreadsheets</a>, we worked with excel in VBScript to create a excel spreadsheet. The spreadsheet wasn&#8217;t going to win any awards for complexity or usefulness, but none the less it was a spreadsheet&#8211; and more importantly it was generated by a script! I also promised to bring you a script to help you chart disk space usage in my next posting in the series. This posting is the fulfilment of that promise! Also, please note that you will need Excel installed where the script runs for this script to operate correctly&#8211; but it does not need to be installed on the system you are pulling disk free space information from since I used the WMI object instead of the filesystem object.</p>
<p>The script below uses WMI&#8217;s <a target="_blank" href="http://msdn.microsoft.com/en-us/library/aa394173.aspx">Win32_LogicalDisk</a> class to grab the drives in the target system, specifically the drive you specify through the use of a where clause in the SQL statement that pulls back the WMI data. (Through the &#8220;where deviceid like&#8221; section of the SQL statement in the code)</p>
<p> Also, I didn&#8217;t put a lot of effort into the code where the for/next loop is that loops through getting and saving the free space because I didn&#8217;t want to create a lot of extra complexity and wanted to create a script that would run through to completion pretty quickly. Currently, the script takes 10 minutes to complete. To test the script while it&#8217;s running, just create files in a folder and delete them a number of times. I created a 10Mb and 20Mb file and made a series of copy/pastes during execution&#8211; with a smattering of deletes in the mix.</p>
<p>For further customization, you can look at the code from the posting I wrote a while back <a rel="bookmark" href="http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/reporting-cpu-usage-by-saving-it-to-a-file-with-the-vbscript-filesystem-object/" title="Permanent Link to Reporting CPU usage by saving it to a file with the VBScript filesystem object">Reporting CPU usage by saving it to a file with the VBScript filesystem object</a> to get a good feel for how to modify the loop to get what you want. In essence, change the number 2 in &#8220;Wscript.Sleep 2&#8243; to a lager number to get a bigger gap between samples and change the 300 in the &#8220;<font color="#000000">For x = 1 to 300&#8243; line to a larger number to get a longer sample period.</font></p>
<p>Here is the code:<br />
<font color="#0000ff">Dim Freespace, CurrentRow, ServerName<br />
Const xlSaveChanges = 1</font></p>
<p><font color="#0000ff">&#8216;the first row in excel is 1 (not 0)<br />
CurrentRow = 1<br />
ServerName=&#8221;.&#8221;</font></p>
<p><font color="#0000ff">Set objExcel = CreateObject(&#8220;Excel.Application&#8221;)<br />
objExcel.Visible = False<br />
objExcel.Workbooks.Add<br />
For x = 1 to 300 &#8216; change 300 to increase your sample duration<br />
     objExcel.Cells(CurrentRow, 1).Value = GetFreeSpace(&#8220;C:&#8221;,ServerName)<br />
     CurrentRow = CurrentRow + 1<br />
     Wscript.sleep 2 &#8216;change this to spread out your samples<br />
next</font></p>
<p><font color="#0000ff">objExcel.ActiveWorkbook.SaveAs (&#8220;C:\excelvbscript.xls&#8221;)<br />
objExcel.Quit</font><br />
<font color="#0000ff">Function GetFreeSpace(Drive,strComputer)<br />
     Set objWMIService = GetObject(&#8220;winmgmts:{impersonationLevel=impersonate}!\\&#8221;_<br />
         &amp; strComputer &amp; &#8220;\root\cimv2&#8243;)<br />
     Set colDisks = objWMIService.ExecQuery (&#8220;Select * from Win32_LogicalDisk where deviceid like &#8220;_<br />
         &amp;chr(34) &amp; Drive &amp; chr(34))<br />
     For Each objDisk in colDisks<br />
         GetFreeSpace = (objDisk.freespace/1024)/1024 &#8216; get MB free<br />
     Next<br />
End Function</font></p>
<p><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;></p>
<!-- wpms-network-global-inserts -->]]></content:encoded>
			<wfw:commentRss>http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/getting-disk-usage-data-with-the-filesystem-object-and-the-excelapplication-object-in-vbscript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reporting CPU usage by saving it to a file with the VBScript filesystem object</title>
		<link>http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/reporting-cpu-usage-by-saving-it-to-a-file-with-the-vbscript-filesystem-object/</link>
		<comments>http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/reporting-cpu-usage-by-saving-it-to-a-file-with-the-vbscript-filesystem-object/#comments</comments>
		<pubDate>Fri, 25 Apr 2008 01:26:34 +0000</pubDate>
		<dc:creator>Jerry Lees</dc:creator>
				<category><![CDATA[Error control]]></category>
		<category><![CDATA[File System Object]]></category>
		<category><![CDATA[Functions]]></category>
		<category><![CDATA[Scripting.FileSystemObject]]></category>
		<category><![CDATA[VBScript]]></category>

		<guid isPermaLink="false">http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/reporting-cpu-usage-by-saving-it-to-a-file-with-the-vbscript-filesystem-object/</guid>
		<description><![CDATA[Last time, in my entry entitled VBScript to check CPU and Processor performance counter statistics using WMI, I asked you to modify the code I gave to include the access method type in the function. If you haven&#8217;t read the article, take a second to go back and look it over and digest the code because [...]]]></description>
				<content:encoded><![CDATA[<p>Last time, in my entry entitled <a rel="bookmark" href="http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/vbscript-to-check-cpu-and-processor-performance-counter-statistics-using-wmi/" title="Permanent Link to Vbscript to check CPU and Processor performance counter statistics using WMI">VBScript to check CPU and Processor performance counter statistics using WMI</a>, I asked you to modify the code I gave to include the access method type in the function. If you haven&#8217;t read the article, take a second to go back and look it over and digest the code because this article is identical to that one, except some added features.</p>
<p>Again I got a rather quick response to the extra credit item that I placed at the end if the article. Basically, you just need to change the code in three places to accomplish this task. We could then use this code in our previous example to create a CSV file for trending and graphing of CPU usage on a server! Here is an example of the code to do this task.</p>
<p>I encourage you to play around with the various methods of file access both with an existing file and a new file that doesn&#8217;t exist.</p>
<p>Below is a working example of the code:</p>
<p><font color="#0000ff"><font color="#0000ff">Option Explicit<br />
On Error Goto 0<br />
Dim strSrv, strQuery,Errors<br />
Dim Counter, Timeframe, Delay</font></font></p>
<p><font color="#0000ff"><font color="#0000ff">Delay = 1000 &#8217;1000 Milliseconds to a second<br />
Timeframe = 300 &#8216;seconds (5 minutes)</font></font></p>
<p><font color="#0000ff"><font color="#0000ff"><br />
strSrv = &#8220;.&#8221;<br />
For Counter = 1 To Timeframe<br />
 Errors = WriteTextFile (&#8220;c:\cpu-usagelog.csv&#8221;, GetFormattedCPU (strSrv), ForAppending)<br />
 WScript.Sleep Delay<br />
Next<font color="#0000ff"><font color="#0000ff">Const ForReading = 1, ForWriting = 2, ForAppending = 8</font></font><font color="#0000ff"><font color="#0000ff">Function GetFormattedCPU(strSrv)<br />
    Dim objWMIService, Item, Proc, start<br />
   <br />
    start = Now()<br />
  <br />
    strQuery = &#8220;select * from Win32_PerfFormattedData_PerfOS_Processor&#8221;<br />
    Set objWMIService = GetObject(&#8220;winmgmts:\\&#8221; &amp; StrSrv &amp; &#8220;\root\cimv2&#8243;)<br />
    Set Item = objWMIService.ExecQuery(strQuery,,48)<br />
    WScript.Echo strQuery<br />
    For Each Proc In Item<br />
       GetFormattedCPU = GetFormattedCPU &amp; Proc.PercentProcessorTime &amp; &#8220;, &#8221;<br />
       wscript.echo &#8220;Processor &#8221; &amp; Proc.Name &amp; &#8221; = &#8221; &amp; Proc.PercentProcessorTime<br />
    Next<br />
   <br />
    GetFormattedCPU = GetFormattedCPU &amp; start  <br />
 <br />
End Function</font></font><font color="#0000ff"><font color="#0000ff">Function WriteTextFile (OutputFile, Data, AccessType)<br />
    Dim wrtlog, fso<br />
 <br />
    On Error Resume Next<br />
    Set fso = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)<br />
    Set wrtlog = fso.OpenTextFile (OutputFile, AccessType, true)<br />
 If Err.Number &lt;&gt; 0 Then<br />
  Set wrtlog = fso.OpenTextFile (OutputFile, ForWriting, true)<br />
 End If<br />
    wrtlog.WriteLine(Data)<br />
    WriteTextFile = Err.Number &#8216;You can use Err.Description here to debug.<br />
    On Error Goto 0<br />
End Function</font></font><font color="#0000ff"><font color="#0000ff"><font color="#0000ff"><font color="#0000ff"><font color="#0000ff"><font color="#0000ff"><font color="#000000">You now have all the pieces for a fully functional performance logging script, so you&#8217;re well on your way to being able to troubleshoot problems deep in the night without getting up and turning on Perfmon. The creates a comma separated value file called cpu-usagelog.csv at the root of the C: drive while the output looks similar to the last time, except it is repeated and I put a time/date stamp in the code by using a new command, <em><strong>now()</strong></em>.</font></font></font></font></font></font></font><font color="#0000ff"><font color="#0000ff"><font color="#000000">Also, pay attention to the error handling around the line that writes the data to the file, you&#8217;ll likely not see an error related to the file not existing and you using the <em>append</em> access methods, because I&#8217;ve checked for it while opening a file.</font></font></font><font color="#0000ff"><font color="#0000ff"><font color="#0000ff"><font color="#0000ff"><font color="#0000ff"><font color="#0000ff"><font color="#000000"> Lastly, notice the for next loop around the function call to call it <em>Timeframe</em> times (Timeframe is multiplied time the sleep <em>Delay</em> to get how long it will capture data). Also notice another new command:</font></font></font></font></font></font></font><font color="#0000ff"><font color="#0000ff"> </font></font><font color="#0000ff"><font color="#0000ff"><strong>Wscript.Sleep</strong>, which basically pauses the execution for the number of milliseconds you specify in <em>Delay</em>.<font color="#0000ff"><font color="#0000ff"> </font></font></font></font></p>
<p></font></font><font color="#0000ff"><font color="#0000ff"><font color="#0000ff"><font color="#0000ff"><font color="#ff0000"><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><font color="#0000ff"><font color="#0000ff"><font color="#ff0000">Extra credit:</font><font color="#0000ff"> </font><font color="#000000">Try and modify this script to monitor something else that is useful to you. Let us all know how you&#8217;ve used it.</font></font></font></p>
<p></font></font></font></font></font></p>
<!-- wpms-network-global-inserts -->]]></content:encoded>
			<wfw:commentRss>http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/reporting-cpu-usage-by-saving-it-to-a-file-with-the-vbscript-filesystem-object/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<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>
