Set Fso Createobjectscripting.filesystemobject Alternative

Leave a comment

Arnold' 'Bob Miller' I am try to convert the following routine into C#dim oFSOset oFSO = CreateObject('Scripting.FilesystemObject')dim sHomePathsHomePath = 'serverc$home'dim oFolderSet oFolder = oFSO.CreateFolder(sHomePath & 'BOB')What should I use instead of the filesystem object?I don't know if you're looking for this or what.I'm actually trying to do the same thing as 'Active Directory Users &Computers' does when you use the '%username%' variable in the home drivedefinition of a user account.But this article might be helpful in starting me down the right pathCheersBob. Hi Bob,The functionality you're looking for is in the System.IO namespace. Thanks Bram'Bram' dim oFSOset oFSO = CreateObject('Scripting.FilesystemObject')dim sHomePathsHomePath = 'serverc$home'dim oFolderSet oFolder = oFSO.CreateFolder(sHomePath & 'BOB')What should I use instead of the filesystem object?I don't know if you're looking for this or what.I'm actually trying to do the same thing as 'Active Directory Users &Computers' does when you use the '%username%' variable in the home drivedefinition of a user account.But this article might be helpful in starting me down the right pathCheersBob. Now that I can add in some more permissions, I need to modify some of theinherited permissions, but only for this directoryHow do a copy the inherited permissions, and then stop inheritance (thisshould let me modify the permissions)Thanks HeapsBob'Bob Miller' Hi Bob,The functionality you're looking for is in the System.IO namespace. Tocreate a directory, you can use Directory.Create(string path).Regards,Bram'Bob Miller'.

Function GetFileList(folderPath As String) As Collection 'mac vba does not support wildcards in DIR function Dim file As String Dim returnCollection As New Collection If Right$(folderPath, 1) '/' Then folderPath = folderPath & '/' End If file = Dir$(folderPath) 'setup initial file Do While Len(file) returnCollection.Add folderPath & file file = Dir$ Loop Set GetFileList = returnCollection End Function.

Home > Articles > Home & Office Computing > Microsoft Windows Desktop

  1. Using Objects with VBScript
< BackPage 2 of 8Next >
This chapter is from the book
Windows XP Under the Hood: Hardcore Windows Scripting and Command Line Power

This chapter is from the book

This chapter is from the book

Windows XP Under the Hood: Hardcore Windows Scripting and Command Line Power

Using Objects with VBScript

To use objects in VBScript, you first need to create an instance of an object and store its reference in a VBScript variable. Then, the methods and properties of the object can be accessed using variable.propertyname or variable.methodname. This is easier to demonstrate than explain, so here's an example (this is a short script that tells whether your C: drive has a folder named windows):

In the first line of the script, we create an instance of a Scripting.FileSystemObject. This is an object class provided with WSH that has handy properties and methods you can use when examining and manipulating disks and files.

Except for the word set, this looks just like a typical call to a function with the returned value being assigned to a variable. That's just what it is. CreateObject is a function that creates a new object instance. What's new is the word set, which VBScript requires you to use to indicate that an object reference is being stored rather than a regular value.

In general, the syntax to create an object instance in VBScript is

where variablename is the variable you want to use to hold the object reference, and objectname is the type of object you want to create.

In the second line of the example, we use the FolderExists method to find out whether a specified folder exists. Remember that methods and properties are just like regular functions and subroutines; they just happen to 'live' in a separate program that provides the object class. The presence of fso.before FolderExists tells VBScript that the FolderExists function is part of the object class to which fso refers, which in this example is Scripting.FileSystemObject.

Some properties and methods take arguments, as you saw with FolderExists. When they do, you have to use parentheses in the same way you would with any other VBScript function or subroutine call. If the method or property returns a function value, you must use parentheses:

If a method doesn't return a value, you can omit the parentheses:

Does this look familiar? We used objects all through the VBScript tutorial in Chapter 2, in statements such as this:

Now, you should be able to recognize that WScript is an object variable and that echo is one of its methods. We never needed to use CreateObject to get WScript set up, though, because VBScript provides it automatically. The WScript object has several other handy methods and properties that we'll discuss later in this chapter.

As mentioned earlier, some properties and methods return another object as their value. For example, Scripting.FileSystemObject has a GetFile method that returns a File object. The File object can then be used to examine and manipulate the file. Here's a sample script that gives the size and creation date of the program file windowsnotepad.exe:

The first line is the same as in the previous script, and it creates an instance of the helpful Scripting.FileSystemObject.

The second line asks the FileSystemObject to return a File object representing the file c:windowsnotepad.exe. Because I want to use this object several times, I saved it in the variable file, using the set keyword. (Although file is a reserved word in Visual Basic, it's not in VBScript, so it's available for use as a variable name.)

The next two lines use the File object's DateCreated and Size properties. Because these functions don't need arguments, there are no parentheses. The returned date/time and numeric values are printed by the WScript.echo method. On my computer, this prints the following:

Automation and Document Files

The GetObject function may be used to obtain an object that represents some already existing document file, through a process called Automation. GetObject uses the name of the document file to find the appropriate object class server, through the Windows standard file type association mechanism (the list of file types and applications you see in Windows Explorer when you click Tools, Folder Options, File Types).

For example, you can create a Word document object representing an existing file and print it:

GetObject can also obtain a reference to an already existing object that was created by some other program, through a name called a moniker. Several preexisting objects can be used to manage networking, Windows, and Active Directory user accounts. We'll cover these in Chapter 7, 'Windows Management Instrumentation (WMI),' and Chapter 8, 'Active Directory Scripting Interface (ADSI).'

The Difference Between Properties and Methods

I don't know about you, but for a long time I found the distinction between properties and methods to be confusing. Now, it's not crucially important to understand the difference, but if you're curious, I'll tell you how I finally came to an understanding of sorts.

If you look back in the preceding section, you'll see I mentioned the FolderExists method that is part of the object FileSystemObject. Why is FolderExists a method and not a property? It comes down to these main points:

  • Propertiesrelate directly to aspects of the object, or rather, of the thing the object represents.

  • Properties act like variables: You just refer to them by their name.

  • Every property returns a value of some sort. Retrieving a property's value doesn't change anything about the object or whatever it represents.

  • Some properties let you assign new values to them. This changes the attribute of the object and the underlying thing it represents.

  • Methodsare the things the object's program can do for you.

  • Methods act like functions and subroutines: They can have arguments passed to them.

  • Methods don't have to return a value, but some do.

  • Invoking a method can change something about the object or the real-world thing it represents.

Therefore, FolderExists is a method because it takes an argument (the name of the file it is to look up). Properties don't need arguments because they are intrinsic attributes of the object itself. As such, they don't need any additional information in order to return a value.

There is something else I should mention about properties: In many cases you can both evaluate them (examine their values) and assign new values to them. They work just like variables in this regard. The difference is that when you assign a new value to a property, the object software makes a corresponding change in the actual thing that the object represents. For example, assigning a new value to a File object's Name property changes the actual file's name, as shown here:

Keep in mind, however, that some objects don't let you change a property's value. Wacom cte 630 sapphire drivers for mac. In this case, the object's documentation will call it a read-only property.

Nested Objects

One other thing I want to point out is that you don't necessarily need to save every object reference in a variable. In the previous example that displayed information about Notepad.exe, if I only wanted to see the creation date, I could have skipped the step of storing the File object in variable file and could have used these statements:

In this case, VBScript refers to fso to call the GetFile method, and the returned object is used to call the DateCreated property. It's not unusual to see several levels of objects this way; this is called a nested object reference.

When you're working with Microsoft Word objects, this is very common. In scripts or Word macros, you may often see statements like these:

In this example, the ActiveDocument object returns a PageSetup object, which has orientation and margin properties you can set. You could save yourself some extra keystrokes in creating this script by saving a reference to the PageSetup object, as follows:

However, VBScript has a special program construction called the With statement that makes this even easier. The previous example could be rewritten this way:

The With statement lets you specify an object reference that is taken as the 'default' object between With and End With. Inside the With statement, you can refer to the default object's methods and properties by preceding them with a period but no variable name. Not only can this save you a lot of typing, but it's easier to read, and it lessens the workload on VBScript, thus speeding up your script.

NOTE

If you need to, you can refer to other objects inside the With statement by using the fully spelled-out object.method.etc syntax.

Releasing Objects

When you create an object, Windows activates the object's class server program to manage the object for you. In the case of Scripting.FileSystemObject, you will usually create one of these objects at the beginning of your script and use it throughout. When your script completes, Windows releases the object you've created. The class server program takes care of freeing up its memory and other housekeeping chores. You really don't have to worry about it at all.

However, if you use a script to create multiple objects, you may find that it's appropriate to explicitly release them when you are through using them. For instance, a script that creates multiple Word documents should tell Word to close each document when you're finished with it, lest you end up with hundreds of documents open at once.

You can explicitly tell an object that you're finished with it by assigning the value Nothing to the variable that holds the object reference. Later in this book, you will see examples of this in some of the sample scripts.

Working with Collections

If you ask Scripting.FileSystemObject for the files or subfolders contained in a folder or drive, it may need to return multiple File or Folder objects. In order to manage this, it actually returns a single collection object that contains all the File or Folder objects inside it. You can then examine the contents of the collection to look at the individual items.

A collection object has a Count property that tells how many items are inside and an Item method that returns a specific item from the collection. This would lead you to expect that you could write a script like this to print the names of the files of the root folder on your hard drive:

However, this script doesn't work. With a folder colection, Item doesn't allow you to retrieve items by number. It requires you to specify the name of the particular object you want, and if you don't yet know the names, this isn't much very useful.

So, to scan through collection objects, each scripting language provides a way to scan through collections without knowing what they contain. VBScript, for example, provides a special version of the For loop called For Each.

Pattern

To scan through a collection object named collectionin VBScript, use the For Each loop as follows:

The For Each loop runs through the statements once for each object in the collection, and the variable objectvar will be made to refer to each of the individual objects in turn. Using For Each, and using a variable named file to hold the individual file objects, our folder-listing script will now work:

You could even use the following:

Now, if you don't plan on writing scripts in any other languages, skip ahead to the section titled 'Using the WScript Object' of this chapter for more information about the built-in WScript object.

Related Resources

  • eBook (Watermarked) $44.79
  • eBook (Watermarked) $38.39
  • Book $47.99