vbscript Object reference to a File


Example

Methods Used:

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

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

Code:

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