How to Create a Folder with VBScript
VBScript Create Folder
My mission is to create a VBScript that will write data to files. The problem is that before we can run (write to file), we must learn to crawl (create the folder). To get the most out of this section realize that there are 3 different html pages. Each page has a tutorial, which explains one of the 3 stages, create the folder, create the file, write data to that text file. This page starts at the beginning with creating the folder.
Topics for Creating a Folder with VBScript
Without creating or locating the parent folder object, you cannot manipulate the child file object. The VBScript examples on this page are at the introduction level, both in terms of ease of scripting and the concept of first creating the folders.
Our FSO (File System Object) mission on this page is clear, to create a folder. However, I have decided to build the script in stages so that you can examine each component.
Scripting folders and files is often an add-on to another script. Take as an example, where you query the operating system, then display the data. At this point do you want to echo the output to screen or would you prefer to store the data in a file? The answer is often store the data in permanently in a text file.
Be aware that this script works brilliantly first time. You get the folder specified by strDirectory, but if you run the script again you get errors. My work-around is to keep modifying strDirectory, for example
2nd Running change strDirectory = "C: \logs" to strDirectory = "C: \logs\take2". However, I realize that this work-around is no long-term solution, and in Example 2 I cure the problem with error-correcting code.
This is a script that will execute 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 Folders
Script to Create a Folder
Note 1: CreateObject holds the key. What we want here is a FileSystemObject object suitable for making folders and files. My point is that for this section, we don't need a network or an Active Directory object.
Note 2: Now what we need here is a folder. So, we apply the .CreateFolder method to the objFSO and thus ensure that we get the desired folder object (and not a file object).
I like the Permissions Analyzer because it enables me to see WHO has permissions to do WHAT at a glance. When you launch this tool it analyzes a users effective NTFS permissions for a specific file or folder, and takes into account network share access, then displays the results in a nifty desktop dashboard!
Think of all the frustration that this free SolarWinds utility saves when you are troubleshooting authorization problems for user's access to a resource. Give this permissions monitor a try - it's free!
This script builds on Example 1 by adding code that copes with situations where the folder already exists. It also employs the objShell to run the Windows Explorer. This is a bonus method so that you can see that the script executed as designed.
Sample VBScript to Create a Folder
Note 1: The construction, If objFSO.FolderExists (strDirectory) is interesting and slightly unusual. This is because, while FSO supports the .FolderExists method, unfortunately, not all other objects support the 'Exists' family of methods.
Note 2: The extra section is also interesting in that you can see two different VBScript folder methods, GetFolder (line 16) and CreateFolder (line 19).
Note 3: Example 2 (above) is a proper script with error-correcting code. Whether the folder already exists, or whether the script creates the folder, VBScript cannot fail. The WScript.Echo message is largely for testing, feel free to remove such lines from your production script.
Note 4: objShell = CreateObject("WScript.Shell") is my way of demonstrating that the script has achieved its goal. The active part is objShell.run. In this example, it is as if you clicked on the Start (Menu), Run and typed 'Explorer'.
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!
PowerShell Create Folder with New-Item
The secret of mastering New-Item is to observe the rhythm, go with the flow of, -path -name -type.
# PowerShell New-Item Folder
Note 5: As usual with my -path examples, I introduce a variable mainly to remind you to change its value if you want the script to work on your machine!
Note 6: The -itemType parameter can be abbreviated to plain -Type. Also "Directory" does not have to be in quotes.
Note 7: If the script succeeds PowerShell gives you a summary of what the script achieved, however, I still like to open Windows Explorer to checkout my new folder.
Our goal is often to write data to text files. However, we must go through a natural progression, create the folder, then the file, then read or write text to that file. This page is a tutorial that shows you how to achieve the first goal, to creating a folder using the CreateFolder method. Next step, let us explore the CreateTextFile method.
If you like this page then please share it with your friends
See more VBScript file examples: