Skip to content

Instantly share code, notes, and snippets.

@DataSolveProblems
Last active May 19, 2019 13:21
Show Gist options
  • Select an option

  • Save DataSolveProblems/60fe5f86a87048c4a47d6ec99a5c5968 to your computer and use it in GitHub Desktop.

Select an option

Save DataSolveProblems/60fe5f86a87048c4a47d6ec99a5c5968 to your computer and use it in GitHub Desktop.
Option Explicit
Dim fso As Object
Dim Folder_Path As String
Private Sub Init()
Set fso = CreateObject("Scripting.FileSystemObject")
Folder_Path = "<Your Folder Path>"
End Sub
Sub Example1()
' BuildPath method
Dim New_Folder_Path As String
Call Init
New_Folder_Path = fso.BuildPath(Folder_Path, "Hello")
MsgBox New_Folder_Path
End Sub
Sub Example2()
' CopyFile method
Call Init
fso.CopyFile "HelloWorld.txt", Folder_Path & "\FolderA\"
End Sub
Sub Example3()
' CopyFolder Method
Call Init
fso.CopyFolder "FolderA", "FolderB"
End Sub
Sub Example4()
' CreateFolder method
Call Init
fso.CreateFolder (Format(Now(), "mm-dd-yyyy"))
End Sub
Sub Example5()
' CreateTextFile method
Dim txtFile As Object
Call Init
Set txtFile = fso.CreateTextFile("MyTextFile.txt")
txtFile.WriteLine ("This is a text")
txtFile.Close
End Sub
Sub Example6()
' DeleteFile method
Call Init
fso.DeleteFile "MyTextFile.txt"
End Sub
Sub Example7()
' DeleteFolder method
Call Init
fso.DeleteFolder "FolderB"
End Sub
Sub Example8()
' DriveExists Method
Call Init
MsgBox fso.DriveExists("T")
End Sub
Sub Example9()
' FileExists method
Call Init
MsgBox fso.FileExists(".\FolderA\HelloWorld.txt")
End Sub
Sub Example10()
' FolderExists method
Call Init
MsgBox fso.FolderExists("FolderA")
End Sub
Sub Example11()
' GetAbsolutePathName method
Call Init
Debug.Print fso.GetAbsolutePathName("")
Debug.Print fso.GetAbsolutePathName("..")
Debug.Print fso.GetAbsolutePathName("..\Student")
End Sub
Sub Example12()
' GetBaseName method
Call Init
Debug.Print fso.GetBaseName(fso.GetAbsolutePathName(""))
End Sub
Sub Example13()
' GetDriveName method
Call Init
Debug.Print fso.GetDriveName(ThisWorkbook.FullName)
End Sub
Sub Example14()
' GetDrive method
Dim myDrive As Object
Call Init
Set myDrive = fso.GetDrive(fso.GetDriveName(ThisWorkbook.FullName))
Debug.Print TypeName(myDrive)
Debug.Print myDrive.SerialNumber
Debug.Print myDrive.TotalSize
End Sub
Sub Example15()
' GetExtensionName
Call Init
Debug.Print fso.GetExtensionName(ThisWorkbook.Name)
End Sub
Sub Example16()
' GetFileName
Call Init
Debug.Print fso.GetFileName(ThisWorkbook.Name)
Debug.Print Replace(fso.GetFileName(ThisWorkbook.Name), "." & fso.GetExtensionName(ThisWorkbook.Name), "")
End Sub
Sub Example17()
' GetFile method
Dim fileObject As Object
Call Init
Set fileObject = fso.GetFile(ThisWorkbook.FullName)
Debug.Print fileObject.Size / 1024 & "KB"
Debug.Print fileObject.DateCreated
Debug.Print fileObject.DateLastModified
End Sub
Sub Example18()
' GetFolder method
Dim folderObject As Object
Call Init
Set folderObject = fso.GetFolder("C:\Users\jiejenn\Desktop\Tutorial\FileSystemObject (VBA)")
Debug.Print folderObject.Name
Debug.Print folderObject.Size / 1024 & "KB"
End Sub
Sub Example19()
' GetParentFolderName method
Call Init
Debug.Print fso.GetParentFolderName("C:\Users\jiejenn\Desktop\Tutorial\FileSystemObject (VBA)\FileSystemObject Demo.xlsm")
End Sub
Sub Example20()
' GetSpecialFolder method
Call Init
Debug.Print fso.GetSpecialFolder(0)
Debug.Print fso.GetSpecialFolder(1)
Debug.Print fso.GetSpecialFolder(2)
End Sub
Sub Example21()
' GetTempName method
Call Init
Debug.Print fso.GetTempName()
End Sub
Private Sub Example22()
' MoveFile method
Call Init
fso.MoveFile "Hello.txt", ".\FolderA\"
End Sub
Private Sub Example23()
' MoveFolder method
Call Init
fso.MoveFolder "Test", ".\FolderA\"
End Sub
Private Sub Example24()
' OpenTextFile method
Dim myTextFile As Object
Call Init
Set myTextFile = fso.OpenTextFile("Hello.txt", 2, -2)
myTextFile.WriteLine "Hello"
myTextFile.WriteLine "World"
myTextFile.Close
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment