Logon Scripts

 

WSH - FSO (File System Object)

Introduction - Using FSO methods to create text files

The Key to using VBScripts to create and manipulate files is to learn the best methods to manipulate the FSO (File System Object).

FSO Create File Topics

 ♦

VBScript to Create a Text File

Here is a VBScript to make a text file.  Once you have created the file, use the WriteLine method to add text.  Whilst this may seem a cumbersome way of manipulating files, the learning points will help you create scripts which can automate reading and writing to files, Active Directory or the registry.

'VBScript
'CreateTextFile method to add text to a file

DIM fso, NewsFile

Set fso = CreateObject("Scripting.FileSystemObject")
Set NewsFile = fso.CreateTextFile("c:\ezine\bestpractice3.txt", True)
NewsFile.WriteLine("Best practice tips")
NewsFile.Write ("Use Remarks to show the purpose of your script.")
NewsFile.Write (" Remember to clear up at the end of the script.")
NewsFile.Close

Learning Points

In our script example, fso (FileSystemObject) creates an object which we can manipulate.  I have chosen the CreateTextFile method here, but be aware that there are other methods like CreateFolder or DeleteFile.  Once you have created the file as a TextStream object, then we can start doing some useful work. 

To get text into the file, I have selected two methods to append data to the file, WriteLine and Write.  WriteLine adds a carriage return and so makes a complete line of text.  Whereas write, just adds text but creates no end of line marker.

Finally, good practice is to tidy up with a Close statement.

Note: Trap for you in the above script.

Change the path on line 7.   Point it to a folder on your machine.

Set NewsFile = fso.CreateTextFile("c:\ezine\bestpractice3.txt", True)

See more on FSO here

Guy Recommends: WMI Monitor and It's Free!Solarwinds Free WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft's operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory or Exchange Server. Give this WMI monitor a try - it's free.

Download your free copy of WMI Monitor

Add Blank Lines To Your FSO Create File Script

Here is a variation of the CreateTextFile method which adds three blank lines between your text.  The crucial new command is WriteBlankLines

'CreateTextFile method to add text to a file

DIM fso, NewsFile

Set fso = CreateObject("Scripting.FileSystemObject")
Set NewsFile = fso.CreateTextFile("c:\ezine\bestpractice4.txt", True)
NewsFile.WriteLine("Best practice tips")
NewsFile.Write ("Use Remarks to show the purpose of your script.")
NewsFile.Write (" Remember to clear up at the end of the script.")
NewsFile.WriteBlankLines 3
NewsFile.WriteLine("WriteBlankLines method produced the gap")
NewsFile.Close

Other Methods you can apply to the FSO object

  • CreateFolder
  • CopyFile
  • GetFile
  • OpenTextFile

If you get an error message - check this page

See Also

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor for PowershellSolarwinds WMI Monitor

Windows Management Instrumentation (WMI) is most useful for PowerShell scripting.

SolarWinds have produced this Free WMI Monitor to take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2014 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: