An Introduction to Files and Folders in WSH - Using the Sub-Objects
(Page 4 of 5 )
There is a second way to accomplish this same task. In the last code example, we used just the methods and properties of the FileSystemObject. However, as we’ve seen, the FileSystemObject also contains File and Folder objects. Each of these objects also contains methods and properties that we can use.
The File and Folder Objects
Shared Methods
object.Copy Destination[, Overwrite]
object.Delete [Force]
object.Move Destination
Shared Properties
object.Attributes [= newattributes]
object.DateCreated
object.DateLastAccessed
object.DateLastModified
object.Drive
object.Name
object.ParentFolder
object.Path
object.ShortName
object.ShortPath
object.Type
Folder Methods
object.CreateTextFile(Filename[, Overwrite[, Unicode]]))
File Methods
object.OpenAsTextStream([Iomode [, Format]])
Folder Properties
object.Files
object.IsRootFolder
object.Size
object.SubFolders
Many of the methods and properties are shared by both the File object and the Folder object. Most of these methods and properties are self explanatory so I’m not going into too much detail in an effort to save space.
Each of these methods and properties returns a string, an object, or a collection with the exception of Copy, Delete, and Move.
However, there are a couple of points I want to make. First, the Attributes property will either get or change the attributes of a file or folder based on whether or not a parameter is supplied. You should also note that the ParentFolder property returns an object. (It actually performs a GetFolder on the parent folder). The OpenAsTextStream method also returns an object. And finally, both the Files property and the SubFolders property return collections.
Keeping these things in mind, let’s rewrite our last code example. This time we’ll create a third folder and move the files from the second to the third. Then we’ll delete the empty folder. To make things interesting, I’m only going to reference one file from the second folder. This will give us a chance to see some of the other properties in action. Obviously, I wouldn’t do this in a real world situation.
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFolder3 = objFso.CreateFolder("C:TestFolder3")
Set objFile = objFso.GetFile("C:TestFolder2test1.txt")
Set objFolder2 = objFile.ParentFolder
For Each file In objFolder2.Files
file.Move objFolder3.Path & "" & file.Name
Next
objFolder2.Delete
At this point, you should be able to see exactly what this code is doing. First we create our new folder. Next, we use the GetFile method to get a handle on our file. Then we use the ParentFolder property to get a handle on its parent folder. Remember to use the Set statement since the ParentFolder returns an actual object and not a string.
Once we have a handle on our second folder, set up a loop to grab each file and move it to the new folder. We finish up by simply deleting the second folder.
If you’ve been following along, at this point you should have two folders, TestFolder and TestFolder3, both containing the exact same files.
Next: Using What You’ve Learned >>
More Windows Scripting Articles
More By Nilpo/Developer Shed Staff Writer