Today, let’s try the FileSystemObject.
What is FileSystemObject?
The FileSystemObject, or FSO, is an often used component to access the file system. For example, you can create files, read the contents of files, determine whether or not a folder or file exists, iterate through the contents of a folder or directory, or any other number of file system-related tasks.
FileSystemObject can be found in Scrrun.dll. In addition to FileSystemOject, Scrrun.dll includes four other objects available for File I/O and other tasks. These objects include the File object, the TextStreamObject object, the Folder object, and the Drive object. We will concentrate on FileSystemObject.
Public Sub CreateFolder()
'Create a folder using FileSystemObject
Dim objFileSystemObject As Scripting.FileSystemObject
Dim FolderPath As String, FolderNameToCreate As String, FullPath As String
'Get the folder path and folder name to create
FolderPath = Sheets(“Create”).Range(“B1”).Value
FolderNameToCreate = Sheets(“Create”).Range(“B2”).Value
'Create FileSystemObject object
Set objFileSystemObject = New Scripting.FileSystemObject
'Check if “\” (backslash) is available at the end of the folder path and add if it’s not there
If Right(FolderPath, 1) <> “\” Then FolderPath = FolderPath & “\”
If Not objFileSystemObject.FolderExists(FolderPath) Then
'If the folder path does not exist
MsgBox “The folder, ” & FolderPath & ” does not exist.”, _
vbOKOnly, “Create Folder Using FSO”
Exit Sub
End If
FullPath = FolderPath & FolderNameToCreate
If Not objFileSystemObject.FolderExists(FullPath) Then
'If the folder to create does not exist, create the folder
Call objFileSystemObject.CreateFolder(FullPath)
Else
'If the folder to create exists
MsgBox “The folder, ” & FolderNameToCreate & ” already exists!”, _
vbOKOnly, “Create Folder Using FSO”
End If
Set objFileSystemObject = Nothing
End Sub
Public Sub CopyFolder()
'Copy a folder using FileSystemObject
Dim objFileSystemObject As Scripting.FileSystemObject
Dim SourceFilePath As String, DestinationFilePath As String
'Get source folder and target folder names
SourceFilePath = Sheets(“Copy”).Range(“B1”).Value
DestinationFilePath = Sheets(“Copy”).Range(“B2”).Value
'Check if “\” (backslash) is available at the end of the folder path and remove if it’s there
If Right(SourceFilePath, 1) = “\” Then _
SourceFilePath = Left(SourceFilePath, (Len(SourceFilePath) – 1))
'Check if “\” (backslash) is available at the end of the folder path and remove if it’s there
If Right(DestinationFilePath, 1) = “\” Then _
DestinationFilePath = Left(DestinationFilePath, (Len(DestinationFilePath) – 1))
'Create FileSystemObject object
Set objFileSystemObject = New Scripting.FileSystemObject
If Not objFileSystemObject.FolderExists(SourceFilePath) Then
'If source folder does not exist
MsgBox “The folder, ” & SourceFilePath & ” does not exist.”, _
vbOKOnly, “Copy Folder Using FSO”
Exit Sub
End If
If Not objFileSystemObject.FolderExists(DestinationFilePath) Then
'If destination folder does not exist
objFileSystemObject.CopyFolder SourceFilePath, DestinationFilePath
Else
'If destination folder exists
MsgBox “The folder, ” & DestinationFilePath & ” already exists!”, _
vbOKOnly, “Copy Folder Using FSO”
End If
Set objFileSystemObject = Nothing
End Sub
Public Sub MoveFolder()
'Move a folder using FileSystemObject (moving and renaimg a folder)
Dim objFileSystemObject As Scripting.FileSystemObject
Dim SourceFilePath As String, DestinationFilePath As String
'Get source folder and target folder names
SourceFilePath = Sheets(“Move”).Range(“B1”).Value
DestinationFilePath = Sheets(“Move”).Range(“B2”).Value
'Check if “\” (backslash) is available at the end of the folder path and remove if it’s there
If Right(SourceFilePath, 1) = “\” Then _
SourceFilePath = Left(SourceFilePath, (Len(SourceFilePath) – 1))
'Check if “\” (backslash) is available at the end of the folder path and remove if it’s there
If Right(DestinationFilePath, 1) = “\” Then _
DestinationFilePath = Left(DestinationFilePath, (Len(DestinationFilePath) – 1))
'Create FileSystemObject object
Set objFileSystemObject = New Scripting.FileSystemObject
If Not objFileSystemObject.FolderExists(SourceFilePath) Then
'If source folder does not exist
MsgBox “The folder, ” & SourceFilePath & ” does not exist.”, _
vbOKOnly, “Move Folder Using FSO”
Exit Sub
End If
If Not objFileSystemObject.FolderExists(DestinationFilePath) Then
'If destination folder does not exist, move the folder
objFileSystemObject.MoveFolder SourceFilePath, DestinationFilePath
Else
'If destination folder exists
MsgBox “The folder, ” & DestinationFilePath & ” already exists!”, _
vbOKOnly, “Move Folder Using FSO”
End If
Set objFileSystemObject = Nothing
End Sub
Public Sub DeleteFolder()
'Delete a folder using FileSystemObject
Dim objFileSystemObject As Scripting.FileSystemObject
Dim RemoveFolderPath As String
'Get folder path to Delete
RemoveFolderPath = Sheets(“Delete”).Range(“B1”).Value
'Check if “\” (backslash) is available at the end of the folder path and remove if it’s there
If Right(RemoveFolderPath, 1) = “\” Then _
RemoveFolderPath = Left(RemoveFolderPath, (Len(RemoveFolderPath) – 1))
'Create FileSystemObject object
Set objFileSystemObject = New Scripting.FileSystemObject
If objFileSystemObject.FolderExists(RemoveFolderPath) Then
'If the folder exists, delete the folder
objFileSystemObject.DeleteFolder RemoveFolderPath, True
Else
'If folder does not exist
MsgBox “The folder, ” & RemoveFolderPath & ” does not exist.”, _
vbOKOnly, “Move Folder Using FSO”
End If
Set objFileSystemObject = Nothing
End Sub