In this article, I will explain about working with folders using the File System Object. The FileSystemObject component is used for working with files and folders on the webserver. This article only deals with the Folder component. The file component will be covered in a later article.
Folder Properties
The following demonstrates the various properties that are associated with the folder object. Place this script on your webserver and load it into your web browser by using website url corresponding to the script.
Dim oFSO, oFolder, oFile, oFolder2, oParent
Set oFSO = Server.CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder("c:Windows")
' Set oFolder = oFSO.GetFolder(Server.MapPath("/folder1"))
With Response
.Write "Folder Attributes: "
.Write oFolder.Attributes & "<br>"
.Write "Date Created: "
.Write oFolder.DateCreated & "<br>"
.Write "Last Accessed: "
.Write oFolder.DateLastAccessed & "<br>"
.Write "Last Modified: "
.Write oFolder.DateLastModified & "<br>"
.Write "Drive: "
.Write oFolder.Drive & "<br>"
.Write "<b>FILES IN THIS FOLDER</b>:<br>"
For Each oFile In oFolder.Files
.Write "<dd>" & oFile.Name & "<br>"
Next
.Write "Is Root Folder?: "
.Write oFolder.IsRootFolder & "<br>"
.Write "Name: "
.Write oFolder.Name & "<br>"
.Write "Parent Folder: "
Set oParent = oFolder.ParentFolder
.Write oParent.Name & "<br>"
.Write "Path: "
.Write oFolder.Path & "<br>"
.Write "Short Name: "
.Write oFolder.ShortName & "<br>"
.Write "ShortPath: "
.Write oFolder.ShortPath & "<br>"
.Write "Size: "
.Write oFolder.Size & "<br>"
.Write "<b>SUBFOLDERS IN THIS FOLDER</b>:<br>"
For Each oFolder2 In oFolder.SubFolders
.Write "<dd>" & oFolder2.Name & "<br>"
Next
.Write "Type: "
.Write oFolder.Type & "<br>"
End With
Attributes Property
In order to make sense of the attributes value, you need to understand how bitmasks work. Covering this would take up a whole new article. Suffice it to say that you use the bit-wise And operator to test if a bit is set and you use the bit-wise Or operator to set a bit yourself.
Dim oFSO, oFolder, nAttr
Set oFSO = Server.CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(Server.MapPath("/folder1"))
nAttr = oFolder.Attributes
.Write "<b>Folder Attributes</b><br>"
If nAttr And 1 Then .Write "<dd>ReadOnly<br>"
If nAttr And 2 Then .Write "<dd>Hidden<br>"
If nAttr And 4 Then .Write "<dd>System<br>"
If nAttr And 8 Then .Write "<dd>Volume<br>"
If nAttr And 16 Then .Write "<dd>Directory<br>"
If nAttr And 32 Then .Write "<dd>Archive<br>"
If nAttr And 1024 Then .Write "<dd>Alias<br>"
If nAttr And 2048 Then .Write "<dd>Compressed<br>"
You could also declare constants for these values to make your code more readable. Something like FILE_READONLY and FILE_COMPRESSED should work pretty well.
Folder Methods
Copy Method
The following example shows how to create a new file on the local filesystem and then copies it to a different filename. The file will be created in the same folder that the script is run from. The Server.MapPath builds the path to the website root and path you are using to run the script.
Dim oFSO, oFolder
Set oFSO = Server.CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(Server.MapPath("folder1"))
oFolder.Copy (Server.MapPath("folder2"))
CreateTextFile Method
Instead of creating a file from the FileSystemObject, you may choose to create a file from the Folder object. This way, you don't have to specify the complete path to the new text file. The interpreter can derive this from that path of the folder.
The second argument is the overwrite argument. When true, the new file will overwrite any existing file.
The third argument is the unicode argument. When true, the new text file will support unicode characters. Otherwise, the new file will only support the Windows character set.
Dim oFSO, oFolder
Set oFSO = Server.CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(Server.MapPath("folder1"))
oFolder.CreateTextFile("textfile.txt", true, true)
Delete Method
The folder delete method will delete a folder and all of it's contents. It's a very powerful method and should be used with caution when used on your website.
It takes a single parameter force indicating whether the operation should force the deletion of read only files. If you choose not to force the deletion, the method call will throw an error if any read only files exist.
Dim oFSO, oFolder
Set oFSO = Server.CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(Server.MapPath("copytest.txt"))
oFolder.Delete(true)
Move Method
Instead of copying a folder, you may want to move the folder to a new location. This can modify the path AND the name of the folder. So you can use this method to both move a folder and rename a folder.
Dim oFSO, oFolder
Set oFSO = Server.CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(Server.MapPath("/path1/folder1"))
oFolder.Move(Server.MapPath("/path2/folder2"));