Computer Performance, Windows 2003, Logon Scripts

Google

WebSearch Site

 Home
Subscribe to Newsletter
WSH - Home
WSH - Simple Script
WSH - Create User
WSH - Bulk Import
WSH - Create Group
WSH - Create Contact
WSH - FSO Create File
WSH - List Members
WSH - Error Messages
VBScript Home
Logon Script Home
CSVDE & LDIFDE
 LDAP Attributes

 

 More Sections
 Guy's eBooks
 Windows 2003
 Exchange 2003 (New)
 Performance Monitor

 

 Windows 2000 AD
 Exchange 2000
 SQL Server 2000
 SharePoint Portal

 

 Litmus Tests
 Registry Hacks
 Problem Solving
 On-line advice
 Free ebooks

 

Contact Guy
About Us
Online Jobs at CP
Guy's eBooks

 

 

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 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

Add blank lines

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

 

 

'VBScript
'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

 *


Google

Webcomputerperformance.co.uk

GFi Events Manager

Guy Recommends: GFi EventsManager

Here is a solution to monitor, manage and archive thousands of events that are generated by devices across your entire network.  Get your free evaluation copy of GFI EventsManager.

 

Home Copyright © 1999-2008 Computer Performance LTD All rights reserved

Please report a broken link, or an error.