物件的物件引用

使用的方法:

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

我們可以使用 getFolder 方法設定對資料夾的物件引用,並對它們執行不同的操作。

碼:

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