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