Excel VBA File Management Using The FileSytemObject
Excel VBA File Management Using The FileSytemObject
Menu Explore
codegym.cc
OPEN
This website uses cookies to improve your experience. Click on Accept to continue. Click Reject to exit this site. To manage
your cookie settings, please click Read More. Accept Reject Read More
70+ MUST KNOW EXCEL SHORTCUT KEYS: Download the pdf from our Excel training page
UNDERSTAND & FIX EXCEL ERRORS D l d th df f
https://www.bluepecantraining.com/excel-vba-file-management-using-filesytemobject/
E lt i i 1/21
8/14/2018 Excel VBA File Management Using The FileSytemObject –
UNDERSTAND & FIX EXCEL ERRORS: Download the pdf from our Excel training page
Learn how to fix these errors: #DIV/0!, #N/A!, #NAME?, #NULL!, #NUM!, #REF! & #VALUE!
You can browse the scripting objects, methods and properties using the Object Browser. Select
Scripting from the All Libraries drop down.
The FileSystemObject contains lots of useful methods that you can use for drives, folders and files.
This website uses cookies to improve your experience. Click on Accept to continue. Click Reject to exit this site. To manage
your cookie settings, please click Read More. Accept Reject Read More
https://www.bluepecantraining.com/excel-vba-file-management-using-filesytemobject/ 2/21
8/14/2018 Excel VBA File Management Using The FileSytemObject –
To use the File System Object you need to create a new instance of it as shown in the code below.
Get Methods
Get methods allow you to retrieve information about a drive, folder or file.
This website uses cookies to improve your experience. Click on Accept to continue. Click Reject to exit this site. To manage
your cookie settings, please click Read More. Accept Reject Read More
https://www.bluepecantraining.com/excel-vba-file-management-using-filesytemobject/ 3/21
8/14/2018 Excel VBA File Management Using The FileSytemObject –
70+ MUST KNOW EXCEL SHORTCUT KEYS: Download the pdf from our Excel training page
UNDERSTAND & FIX EXCEL ERRORS: Download the pdf from our Excel training page
Learn how to fix these errors: #DIV/0!, #N/A!, #NAME?, #NULL!, #NUM!, #REF! & #VALUE!
Get Drive
Use GetDrive to specify which drive you want to examine. With the drive specified you can retrieve
information about it using the properties shown in the Object Browser below.
In this example we use the FreeSpace property of the Drive object to calculate the available space on
a drive.
Sub UsingGetDrive()
Const BytesToTB As Double = 1099511627776#
Dim fso As Scripting.FileSystemObject
Set fso = New Scripting.FileSystemObject
Dim drv As Scripting.Drive
Set drv = fso.GetDrive("D:")
Dim AvlbSpace As Double
AvlbSpace = Round(drv.FreeSpace / BytesToTB, 2)
MsgBox "Drive " & drv.DriveLetter & " has " & AvlbSpace & "TBs of free
End Sub
Thewebsite
This code above creates
uses cookies the message
to improve box shown
your experience. below.
Click on Accept to continue. Click Reject to exit this site. To manage
your cookie settings, please click Read More. Accept Reject Read More
https://www.bluepecantraining.com/excel-vba-file-management-using-filesytemobject/ 4/21
8/14/2018 Excel VBA File Management Using The FileSytemObject –
GetFolder
Use GetFolder to specify which folder you want to examine. With the folder specified you can retrieve
information about it using the properties shown in the Object Browser below.
In this example we use the Files property to allow us to count the number of files present in a folder.
Sub UsingGetFolder()
Dim fso As Scripting.FileSystemObject
Set fso = New Scripting.FileSystemObject
Dim fld As Scripting.Folder
Set fld = fso.GetFolder("C:\Users\xxxx\Desktop\MyFolder")
Dim CountFiles As Byte
CountFiles = fld.Files.Count
MsgBox "There are " & CountFiles & " files in folder: " & fld.Name
End Sub
https://www.bluepecantraining.com/excel-vba-file-management-using-filesytemobject/ 5/21
8/14/2018 Excel VBA File Management Using The FileSytemObject –
GetFile
Use GetFile to specify which file you want to examine. With the file specified you can retrieve
information about it using the properties shown in the Object Browser below.
70+ MUST KNOW EXCEL SHORTCUT KEYS: Download the pdf from our Excel training page
This website uses cookies to improve your experience. Click on Accept to continue. Click Reject to exit this site. To manage
UNDERSTAND & FIX EXCEL ERRORS: Download the pdf from our Excel training page
your cookie settings, please click Read More. Accept Reject Read More
Learn how to fix these errors: #DIV/0!, #N/A!, #NAME?, #NULL!, #NUM!, #REF! & #VALUE!
https://www.bluepecantraining.com/excel-vba-file-management-using-filesytemobject/ 6/21
8/14/2018 Excel VBA File Management Using The FileSytemObject –
Sub UsingGetFile()
Dim fso As Scripting.FileSystemObject
Set fso = New Scripting.FileSystemObject
Dim fle As Scripting.File
Set fle = fso.GetFile("C:\Users\xxxx\Desktop\MyFolder\Budget.xlsx")
Dim LstMod As Date
LstMod = fle.DateLastModified
MsgBox fle.Name & " was last modified on " & LstMod
End Sub
Sub CreateAFolder()
Dim fso As Scripting.FileSystemObject
Set fso = New Scripting.FileSystemObject
Dim MyPath As String
MyPath = "C:\Users\xxxx\Desktop\"
fso.CreateFolder MyPath & "FolderXYZ"
End Sub
CopyFile Method
The CopyFile method has three parameters: Source, Designation and OverWriteFiles (defaults to
TRUE)
Sub CopyAFile()
Dim fso As Scripting.FileSystemObject
Set
This fsouses
website = New Scripting.FileSystemObject
cookies to improve your experience. Click on Accept to continue. Click Reject to exit this site. To manage
Dim MyPath As String
your cookie settings, please click Read More. Accept Reject Read More
MyPath = "C:\Users\xxxx\Desktop\"
fso CopyFile Source:=MyPath & "MyFolder\Budget xlsx", Destination:=MyPa
https://www.bluepecantraining.com/excel-vba-file-management-using-filesytemobject/ 7/21
8/14/2018 Excel VBA File Management Using The FileSytemObject –
fso.CopyFile Source: MyPath & MyFolder\Budget.xlsx , Destination: MyPa
End Sub
Note that you have to state the file name in the Destination parameter.
DeleteFile Method
The DeleteFile method has two parameters FileSpec and Force (delete if Read-only defaults to
FALSE)
Sub DeleteFile()
Dim fso As Scripting.FileSystemObject
Set fso = New Scripting.FileSystemObject
Dim MyPath As String
MyPath = "C:\Users\xxxx\Desktop\"
fso.DeleteFile MyPath & "MyFolder\Junk.xlsx"
End Sub
MoveFile Method
The MoveFile method has two parameters: Source and Destination.
Learn More
70+ MUST KNOW EXCEL SHORTCUT KEYS: Download the pdf from our Excel training page
UNDERSTAND & FIX EXCEL ERRORS: Download the pdf from our Excel training page
Learn how to fix these errors: #DIV/0!, #N/A!, #NAME?, #NULL!, #NUM!, #REF! & #VALUE!
Sub MoveFile()
Dim fso As Scripting.FileSystemObject
Set fso = New Scripting.FileSystemObject
Dim MyPath As String
MyPath = "C:\Users\xxxx\Desktop\"
This website uses cookies to improve your experience. Click on Accept to continue. Click Reject to exit this site. To manage
fso.MoveFile _
your cookie settings,
Source:=MyPath please click Read More.Me.xlsx",
& "MyFolder\Move Accept Reject Read More
Destination:=MyPath & "Fo
End Sub
https://www.bluepecantraining.com/excel-vba-file-management-using-filesytemobject/ 8/21
8/14/2018 Excel VBA File Management Using The FileSytemObject –
FolderExists Method
The FolderExists method has a single parameter: FolderSpec
Sub CheckIfFolderExists()
Dim fso As Scripting.FileSystemObject
Set fso = New Scripting.FileSystemObject
Dim MyPath As String
MyPath = "C:\Users\xxxx\Desktop\"
'Check to see if folder already exists
If fso.FolderExists(MyPath & "\MyFolder") Then
MsgBox "Folder Already Exists"
Else
'Create a folder
fso.CreateFolder MyPath & "New Folder"
End If
End Sub
FileExists Method
The FileExists method has a single parameter: FileSpec
Sub CheckIfFileExists()
Set fso = New Scripting.FileSystemObject
Dim MyPath As String
MyPath = "C:\Users\chest\Desktop\"
'Check to see if file exists
If fso.FileExists(MyPath & "MyFolder\Move Me.xlsx") Then
'Move a file
fso.MoveFile _
Source:=MyPath & "MyFolder\Move Me.xlsx", _
Destination:=MyPath & "FolderXYZ\Move Me.xlsx"
Else
MsgBox "File Does Not Exist"
End If
End Sub
Practical Examples
Copy Files With a Speci c File Type
This website
In this uses cookies
example to improve
the Type propertyyour experience.
of the Clickison
File object Accept
used to continue.whether
to determine Click Reject to gets
a file exit this site. To
copied. Wemanage
https://www.bluepecantraining.com/excel-vba-file-management-using-filesytemobject/ 9/21
8/14/2018 Excel VBA File Management Using The FileSytemObject –
Sub CopyFilesWithASpecificFileType()
This website uses cookies to improve your experience. Click on Accept to continue. Click Reject to exit this site. To manage
your cookie settings, please click Read More. Accept Reject Read More
https://www.bluepecantraining.com/excel-vba-file-management-using-filesytemobject/ 10/21
8/14/2018 Excel VBA File Management Using The FileSytemObject –
We can return the file type using the Scripting FileSystemObject method GetExtensionName
The first two characters of an Excel file extension are xl. Using the Left function we can determine if
this is the case.
We can then loop this code for each file in our folder.
Learn More
70+ MUST KNOW EXCEL SHORTCUT KEYS: Download the pdf from our Excel training page
UNDERSTAND & FIX EXCEL ERRORS: Download the pdf from our Excel training page
Learn how to fix these errors: #DIV/0!, #N/A!, #NAME?, #NULL!, #NUM!, #REF! & #VALUE!
Sub CopyAllExcelFilesIntoFolder()
Dim fso As Scripting.FileSystemObject
Set fso = New Scripting.FileSystemObject
Contents of All Excel Files folder (once code executed) shown below.
Sub OrganiseIntoFoldersBasedOnFileType()
Dim fso As Scripting.FileSystemObject
Set fso = New Scripting.FileSystemObject
This
Dimwebsite
fleuses
Ascookies to improve your experience. Click on Accept to continue. Click Reject to exit this site. To manage
Scripting.File
your cookie settings, please click Read More. Accept Reject Read More
'Store the old folder's path in a variable
Di M F ld P th A St i
https://www.bluepecantraining.com/excel-vba-file-management-using-filesytemobject/ 12/21
8/14/2018 Excel VBA File Management Using The FileSytemObject –
Dim MyFolderPath As String
MyFolderPath = "C:\Users\chest\Desktop\MyFolder"
End Sub
70+ MUST KNOW EXCEL SHORTCUT KEYS: Download the pdf from our Excel training page
UNDERSTAND & FIX EXCEL ERRORS: Download the pdf from our Excel training page
Learn how to fix these errors: #DIV/0!, #N/A!, #NAME?, #NULL!, #NUM!, #REF! & #VALUE!
Sub OrganiseFilesBasedOnFileName()
Dim fso As Scripting.FileSystemObject
Set fso = New Scripting.FileSystemObject
Next fle
End Sub
Ads by Google
This website uses cookies to improve your experience. Click on Accept to continue. Click Reject to exit this site. To manage
your cookie settings, please click Read More. Accept Reject Read More
Excel Training Courses - Excel VBA Macro to a Collapse and Expand Excel VBA Ma
Beginners' Intermediate Create Slicer for Heading in your Word Apply PivotTa
https://www.bluepecantraining.com/excel-vba-file-management-using-filesytemobject/ 15/21
8/14/2018 Excel VBA File Management Using The FileSytemObject –
Beginners , Intermediate, Create Slicer for Heading in your Word Apply PivotTa
Advanced & VBA... PivotTable 2013 Document – Blue...
Multiple Table of Excel VBA How to Filter – Page 2 of 6 – In House Excel 2016 Fo
Contents/ TOC for Each Data Using AutoFilter Excel Training & MS Sheet - New F
Section in Word Office Blog
This website uses cookies to improve your experience. Click on Accept to continue. Click Reject to exit this site. To manage
your cookie settings, please click Read More. Accept Reject Read More
https://www.bluepecantraining.com/excel-vba-file-management-using-filesytemobject/ 16/21
8/14/2018 Excel VBA File Management Using The FileSytemObject –
Related Tutorials
Excel Training
Courses -
Beginners',...
Collapse and
Expand Heading in
your Word 2013...
Multiple Table of
Contents/ TOC for
Each Section in...
– Page 2 of 6 – In
House Excel
Training & MS...
Excel 2016
Forecast Sheet -
New Feature
Excel VBA
Formatting Cells
and Values
Remove
This website uses cookies to improve the
your #N/A Click on Accept to continue. Click Reject to exit this site. To manage
experience.
Error in a
your cookie settings, please click Read More. Accept Reject Read More
VLOOKUP
https://www.bluepecantraining.com/excel-vba-file-management-using-filesytemobject/ 17/21
8/14/2018 Excel VBA File Management Using The FileSytemObject –
Excel 2013
Combining Multiple
Tables in a...
Excel Training Courses in East and West Sussex Delivered at Your Offices
Excel Training Courses in Burgess Hill, East Grinstead and Haywards Heath
Excel Training Courses in Kent Including Dartford, Tunbridge Wells, Tonbridge &
Maidstone
Weymouth
Excel Training Courses in Oxfordshire Including Oxford, Abingdon, Bicester & Banbury
Excel Training Courses in St Albans, Welwyn Garden City, Hoddesdon, Hatfield &
Cheshunt
Excel Training Courses In Berkshire Including Reading Slough Newbury & Bracknell
This website uses cookies to improve your experience. Click on Accept to continue. Click Reject to exit this site. To manage
Excel Training Courses in Bedfordshire
your cookie settings, please click Read More. Accept Reject Read More
Next
Previous
Copyright 2018
This website uses cookies to improve your experience. Click on Accept to continue. Click Reject to exit this site. To manage
your cookie settings, please click Read More. Accept Reject Read More
https://www.bluepecantraining.com/excel-vba-file-management-using-filesytemobject/ 20/21
8/14/2018 Excel VBA File Management Using The FileSytemObject –
Excel Training
MS Project Training
Word Training
OneNote Training
PowerPoint Training
Publisher Training
Visio Training
This website uses cookies to improve your experience. Click on Accept to continue. Click Reject to exit this site. To manage
your cookie settings, please click Read More. Accept Reject Read More
https://www.bluepecantraining.com/excel-vba-file-management-using-filesytemobject/ 21/21