• No se han encontrado resultados

Store those scripts

Introduction to Create Folders

One of my favourite strategies with scripts is taking a section from one script and pasting it into another script. This is my point, regard the script on this page as the first stage in bolting three scripts together. The result in section 9 will be one big script, which creates a folder, a file and then appends text to that file. I

admit that this is a difficult project so early in your scripting career; however, I want to make the scripts useful as well as instructional.

Topics for VBScript

Meet the VBScript terms

Example 1 - Basic VBScript to Create a Folder Example 2 - Create a Folder with error-correcting Code

Example 3 - Script to Create a Folder, then Open Windows Explorer

Summary of Creating Folders

Meet the VBScript terms

Here is a reminder of the overall plan. In Stage 1, on this page, we build a folder. Once we have created the folder, then in Stage 2 we can add a file, finally, in Stage 3 I will show you how to write text to our file. As I mentioned earlier, VBScript is a simple but powerful language. I would like to introduce the VBScript commands that we will be employing:

1) Dim: Stands for dimension, it means I declare the variables.

2) Option Explicit: This does not achieve anything on it's own, however, it works with to Dim to force us to declare all variables. The benefit of Option Explicit is that it reduces errors caused by typos. For example, if we set strDirectory = c:\ logs but later referred to the variable as strDir instead of strDirectory, the result would not what we expected.

3) strDirectory: Is a variable which holds the path to the new folder. Feel free to amend c:\ logs to a different folder or even an alternative drive, for example

(D:\ vbscript) would work equally well as a place to hold your scripts.

4a) Set objFSO = CreateObject("Scripting.FileSystemObject"). This statement is the heart of the script and needs careful dissection. As ever proceed slowly, inch by inch and it will be a synch. Set objFSO means initialize the variable.

4b) CreateObject tells VBScript to create a new object rather than get an existing object. Moreover, what we want is a scripting FileSystemObject and not a user or a printer object. Think of FileSystemObject as defining the object, in this case, it will have methods for manipulating folders.

Introduction to VBScript.doc By Guy Thomas

Page 34 of 59

5) Set objFolder = objFSO.CreateFolder(strDirectory) This powerful command brings all our components together and finishes the job of creating the folder. Think of objFSO, which we so carefully created on the previous line, as being a magic source of new folders. All we need to do is to tell this magic object, to CreateFolder (not CreateTextFile), and tell it what name to give the new folder namely the value held by the strDirectory variable.

6) WScript.Echo "Just created " & strDirectory. This is an optional instruction to display a message, which reminds us what the script has done. As you will see, I love WScript.Echo boxes, especially when the script is at the testing stage when we want to confirm that VBScript has at least interpreted the previous lines of  code. Note in passing how I reuse the strDirectory variable. Observe if you change strDirectory s value, the confirmation message changes to reflect the name of the new folder.

Example 1

-

Basic VBScript to Create a Folder

Be aware that this script works brilliantly - first time. You get the folder specified by strDirectory, but if you run the script again, this time you get errors. My

work-around is to keep amending the value of strDirectory, for example, on the second running change strDirectory = "C: \ logs" to strDirectory = "C:

\ logs\ take2". However, I realize that this technique is no long-term solution, but do not worry, in Example 2 we cure the problem with error -correcting code.

Prerequisites

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

1. Copy and paste the example script below into notepad or a VBScript editor.

2. Decide whether to change the value for strDirectory (Particularly the drive letter).

3. Save the file with a .vbs extension, for example, NewFolder.vbs.

4. Double click NewFolder.vbs and check Windows Explorer for strDirectory.

Sample Script to Create a Folder

' NewFolder.vbs

' Sample VBScript to create a folder (Simple)

' Author Guy Thomas http://computerperformance.co.uk/ ' Version 2.4 - September 2005

' ---' Option Explicit

Dim objFSO, objFolder, strDirectory strDirectory = "c:\ logs"

' Create FileSystemObject. So we can apply .createFolder m ethod Set objFSO = CreateObject("Scripting.FileSystemObject")

' Here is the key line Create a Folder, using strDirectory Set objFolder = objFSO.CreateFolder(strDirectory) WScript.Echo "Just created " & strDirectory

WScript.Quit

Introduction to VBScript.doc By Guy Thomas

Page 35 of 59

VBScript Tutorial

-

Learning Points

Note 1: CreateObject holds the key. What we want here is a FileSystemObject object because it's 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 specifically 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).

Example 2

-

Create a Folder with error

-

correcting Code

This script builds on Example 1 by adding error-correcting code, the benefit is that the script now copes with situations where the folder already exists. One of  the quirks of VBScript is that adding error-correcting code often takes more effort than creating the main task. Another advantage of adding the extra code is that it makes you understand what is going. Writing error-correcting also teaches you to anticipate what could go wrong.

From my spreadsheet days, I have always loved the 'If' function. In VBScript the 'If' construction may take five or more lines, it works like this:

1. If - test - then (the word 'then' ends the first line) 2. What should happen, if - test - is true

3. Else (a line unto itself)

4. What should happen if test was false 5. End If (on its own line)

' NewFolderEC.vbs

' Sample VBScript to create a folder with error-correcting Code. ' Author Guy Thomas http://computerperformance.co.uk/ ' Version 2.6 - May 2005

' ---' Option Explicit

Dim objFSO, objFolder, objShell, strDirectory strDirectory = "c:\ logs"

' Create the File System Object

Set objFSO = CreateObject("Scripting.FileSystemObject") ' Note If..Exists. Then, Else ... End If construction

If objFSO.FolderExists(strDirectory) Then

Set objFolder = objFSO.GetFolder(strDirectory) WScript.Echo strDirectory & " already created " Else

Set objFolder = objFSO.CreateFolder(strDirectory) WScript.Echo "Just created " & strDirectory

End If  WScript.Quit

' End of Sample VBScript to create a folder with error -correcting Code

Example 3

-

Script to Create a Folder, then Open Windows

Explorer

Here is the icing on the cake. Not strictly necessary, but a final flourish that opens Windows Explorer for you to check your newly created folder. First, get the basic model to create the folder (Example 1). Then add error-correcting code (Example 2) and finally, add 5 lines to open Windows Explorer.

Introduction to VBScript.doc By Guy Thomas

Page 36 of 59 ' NewFolderEC.vbs

' Sample VBScript to create a folder with error-correcting Code. ' Author Guy Thomas http://computerperformance.co.uk/ ' Version 2.6 - May 2005

' ---' Option Explicit

Dim objFSO, objFolder, objShell, strDirectory strDirectory = "c:\ logs"

' Create the File System Object

Set objFSO = CreateObject("Scripting.FileSystemObject") ' Note If..Exists. Then, Else ... End If construction

If objFSO.FolderExists(strDirectory) Then

Set objFolder = objFSO.GetFolder(strDirectory) WScript.Echo strDirectory & " already created " Else

Set objFolder = objFSO.CreateFolder(strDirectory) WScript.Echo "Just created " & strDirectory

End If 

' Extra section to open Windows Explorer If err.number = vbEmpty then

Set objShell = CreateObject("WScript.Shell") objShell.run ("Explorer" &" " & strDirectory & "\ " ) Else WScript.echo "VBScript Error: " & err.number End If 

WScript.Quit

' End of Sample VBScript to create a folder with error -correcting Code

VBScript Tutorial

-

Learning Points

Note 1: 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 simulates you clicking on the Start (Menu), selecting Run and typing, 'Explorer'.

Note 2: You may have noticed that objShell is made by:

CreateObject("WScript.Shell") also it has a different job compared with objFSO, which was built with FileSystemObject.

Note 3: At first don't worry about these different types of objects. Just work smart, find a suitable example, copy the code, and amend to your

circumstances. After a while you will have your own library of scripts and can start bolting together sections and building production scripts.

Summary of Creating Folders

We have covered a great deal of ground. In terms of theory you can appreciate how VBScript employs the Object, method, value system. As far as script

structure is concerned, here is a classic example of declaring variables, creating objects and manipulating properties. Another important lesson is to add error- correcting code, in is important to anticipate what could go wrong, and ensure that the script always quits gracefully.

This page covered the following VBScript methods, commands and syntax:

FileSystemObject, .CreateFolder, .GetFolder, If...then...End if and also CreateObject("WScript.Shell")

Introduction to VBScript.doc By Guy Thomas

Page 37 of 59

In document IV. Administración Local (página 43-53)

Documento similar