vbscript Object reference to a folder


Example

Methods used:

.GetFolder(strPath) - Returns an object referring to the path

We can set an object reference to a folder using the getFolder method and perform different operations on them.

Code:

Dim strFolderPath, objFso, objFolder
strFolderPath = "C:\Users\GS\Desktop\LogsFolder"
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFso.getFolder(strFolderPath)

'Accessing the Folder's Properties
Msgbox objFolder.Name                            'Returns the Folder's Name
Msgbox objFolder.Size                            'Returns the Folder's size in Bytes  
Msgbox objFolder.DateCreated                     'Returns the Folder's creation date 
Msgbox objFolder.DateLastModified                'Returns the Folder's last modified date
Msgbox objFolder.Path                            'Returns the Folder's Absolute Path

Dim objChildFolders
Set objChildFolders = objFolder.SubFolders       'Returns the collection of all subfolder 

Dim objChildFiles
Set objChildFiles = objFolder.Files              'Returns the collection of all files contained in the folder  

'Using the Folder's methods
objFolder.Copy strDestPAth, True                 'Copies the folder to path contained in strDestPath and overwrite Flag=True
objFolder.Delete True                            'Deletes the Folder; True indicates forceful Deletion
objFolder.Move strDestPath                       'Moves the Folder to the path contained in strDestPath variable 
objFolder.CreateTextFile strFileName, True       'Created a new text file inside the folder and overwrites the existing file(if it exists)
Set objChildFiles = Nothing
Set objChildFolders = Nothing
Set objFolder = Nothing
Set objFso = Nothing