物件的物件引用

使用方法:

.GetFile(strPath) - Returns an object referring to a file.

我們可以使用 getFile 方法設定對檔案的物件引用,並對它們執行不同的操作。

碼:

Dim strFilePath, objFso, objFile
strFilePath = "C:\Users\GS\Desktop\LogsFolder\file.txt"
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFile = objFso.getFile(strFilePath)

'Accessing the File's Properties
Msgbox objFile.Name                            'Returns the File's Name
Msgbox objFile.Size                            'Returns the File's size in Bytes  
Msgbox objFile.DateCreated                     'Returns the File's creation date 
Msgbox objFile.DateLastModified                'Returns the File's last modified date
Msgbox objFile.Path                            'Returns the File's absolute path

'Using the File's Methods
objFile.Delete True                            'Forcefully deletes the File
objFile.Copy strDestPath, True                 'Copies the file to path contained in variable strDestPath
objFile.Move strDestPath                       'Moves the file to the path contained in the variable strDestPath
objFile.OpenAsTextStream mode                  'Opens the file as a text stream in either Read mode(mode=1), write mode(mode=2) or Append mode(mode=8)
Set objFile = Nothing
Set objFso = Nothing