VBA GetFile

The FileSystemObject VBA GetFile function will return a File object on which you can run several methods (Copy, Delete, Move, OpenAsTextStream) and obtain file properties (e.g. Date Created, Size, Type etc. see more below).

VBA GetFile Syntax

fso.GetFile( path )

path
The path to the file for which a File object is to be returned.

VBA GetFile Examples

Set fso = CreateObject("Scripting.FileSystemObject")

Set f = fso.GetFile("C:\Src\Hello.txt") 'Return the File object

'Now we can obtain various properties of the File
Debug.Print f.DateCreated 'Date when file was created
Debug.Print f.Drive 'Result: "C:" - the drive of the file path
Debug.Print f.Name 'Result: "Hello.txt" - name of the file
Debug.Print f.ParentFolder 'Result: "Hello.txt" - name of the file
Debug.Print f.Path 'Result: "Hello.txt" - name of the file
Debug.Print f.ShortName 'Returns short name of file with 8.3 naming convention
Debug.Print f.ShortPath 'Returns short path to file with 8.3 naming convention
Debug.Print f.Size 'Size of file in bytes
Debug.Print f.Type 'Result: "Text Document" - type of the file

'We can also run several basic file operations on the file
f.Copy "C:\NewFolder\NewName.txt" 'Copy file
f.Move "C:\NewFolder\" 'Move the file to new destination
Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault) 'Open file as text stream for read write or append
f.Delete 'Delete the fil