How to Create a File with VBScript
VBScript Create File
This page concentrates on creating a text file with a VBScript. I expect that you can think of many scripting scenarios where it would be advantageous to output the data, not to the screen, but to a file. Software inventory, registry values and event logs are just some of the examples that spring to mind. Remember, that scripting starts at the folder level, therefore, even if you are primarily interested in saving to a file, your script must first get or create the parent folder.
Topics for Creating a File with VBScript
Creating the file, covered on this page, is the middle of a series of three FSO examples. The basics are covered in creating folders (Page 1), this page deals with creating files, whilst Page 3 deals with reading and writing text into the file that we which we created with the first two scripts.
Our mission on this page is clear, to create a file. Naturally, we need a folder to hold our file, therefore, our script will test to see if the folder exists, and if not, then our code will create a folder.
The idea behind this script is to highlight the code need to create a file. The cost of focussing on one method is that Example 1 works fine the first time you execute the script, but if you run it a second time you get an error. To cure the problem, move on to the second script. Else keep altering the strDirectory. Here is what I did as a work-around:
1st Run strDirectory = "E: \logs"
2nd Run strDirectory = "E: \logs\guy1"
3rd Run strDirectory = "E:\ logs\guy2"
I admit this is poor scripting, but my dilemma is keeping it simple and highlighting the learning steps, versus a slick script that is difficult to follow. To tell the whole truth, I fervently believe that you learn more when scripts go wrong - providing you can fix the error quickly.
This is a script that will create a file equally well on a Windows server or an XP machine. Should you get permission errors, I recommend that you logon as administrator.
Instructions for Creating Files
Sample VBScript to Create a File
Note 1: All FSO scripts begin by creating a File System Object with
Note 2: The specific method for creating the file is called: .CreateTextFile.
Note 3: Observe that we also employed the sister method CreateFolder. In fact, this causes a problem, if you run the script for a second time because it generates the file exists error 800A003A. So, in Example 2 we are going to introduce error-correcting code.
SolarWinds' Orion performance monitor will help you discover what's happening on your network. This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.
What I like best is the way NPM suggests solutions to network problems. Its also has the ability to monitor the health of individual VMware virtual machines. If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.
This script is much better than the primitive Example 1. My idea is to add code, which copes with situations where the folder already exists. It also employs the objShell to run the Windows Explorer so that you can test that the script works as designed.
Sample Script to Create a File and Check if a File Already Exists
SolarWinds' Config Generator is a free tool, which puts you in charge of controlling changes to network routers and other SNMP devices. Boost your network performance by activating network device features you've already paid for.
Guy says that for newbies the biggest benefit of this free tool is that it will provide the impetus for you to learn more about configuring the SNMP service with its 'Traps' and 'Communities'. Try Config Generator now - it's free!
VBScript Tutorial - Learning Points
Note 1: If you do not add error-correcting code, then there is a real danger that running the script again will destroy the original file. What .FileExists does is to check to see if there is already a file with the name as that referenced by strFile.
Note 2: This script also features code which protects against the folder already existing - FolderExists.
Note 3: A tiny point, but the script produces an error when its first run, unless I added set objFolder and objFile = nothing. Here is an example where I always learn more when things go wrong. I have known for a long time that I should nullify objects when I have finished them, but idleness and wanting to keep the script short have meant I have omitted these set objxyz = nothing from of other scripts. Sometimes I get away with it, but not in this example.
Note 4: objShell = CreateObject("WScript.Shell") is my way of showing that the script has achieved its goal. The active part is objShell.run. In this example, it is as if you click on the Start (Menu), Run and type Explorer.
With file system objects there is a natural progression, create the folder, then the file, then read or write text to that file. This page shows you to creating a file using the CreateTextFile method. Next step let us explore reading or writing data to the file.
If you like this page then please share it with your friends
See more VBScript file examples: