File System Object (FSO) - Working with Folders
'Creating
a folder
Option Explicit
Dim objFSO, objFolder,
strDirectory
strDirectory
= "D:\logs"
Set objFSo = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.CreateFolder(strDirectory)
'Deleting
a folder
Option Explicit
Dim objFSO, strDirectory
strDirectory
= "D:\logs"
Set objFSo = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFolder(strDirectory)
'Copying
folders -- > Will copy all folders within the specified folder to the
destination folder
Option Explicit
Dim objFSO
Set objFSo = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFolder
"D:\Logs", "C:\Logs", True 'Use False instead of
True, if you want to copy entire folder with its subfolders
'Checking
wheather the folder exist or not, if not creating the folder
Option Explicit
Dim objFSO, objFolder,
strDirectory
strDirectory
= "D:\logs"
Set objFSo = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(strDirectory)
Then
Set objFolder = objFSO.GetFolder(strDirectory)
msgbox strDirectory & " aleady exist"
Else
Set objFolder = objFSO.CreateFolder(strDirectory)
End if
'Displaying
all disk drives on system
Option Explicit
Dim objFSO, colDrives,
objDrive
Set objFSo = CreateObject("Scripting.FileSystemObject")
Set colDrives = objFSO.Drives
For Each ObjDrive in colDrives
Msgbox "Drive Letter: " & objDrive.DriveLetter
Next
'Getting
available space on disk drive
Option Explicit
Dim objFSO, colDrives,
objDrive
Set objFSo = CreateObject("Scripting.FileSystemObject")
Set objDrive = objFSO.GetDrive
("C:")
Msgbox
"Available
Space:"
& objDrive.AvailableSpace
'Displaying
all disk drives on system and their corrosponding available spaces
Option Explicit
Dim objFSO, colDrives,
objDrive
Set objFSo = CreateObject("Scripting.FileSystemObject")
Set colDrives = objFSO.Drives
For Each ObjDrive in colDrives
Msgbox "Drive Letter: " & objDrive.DriveLetter
& ",
Available Space:" & objDrive.AvailableSpace
Next
'
Get path of the root folder
'Specify
the parent folder
FilePath
= CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
folderspec
= FilePath & "\Folders"
set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(folderspec)
Set sf = f.SubFolders
LocateFolder1
= folderspec
Do while sf.Count >0
For Each f1 in sf
LocateFolder1 = LocateFolder1 &
"\" & f1.name
Fold = LocateFolder1
Exit For
Next
Set f = fso.GetFolder(LocateFolder1)
Set sf = f.SubFolders
Loop
Msgbox
Fold
Set fso = Nothing
Set f = Nothing
Set sf = Nothing
'Get
root folder count, names, and their paths
'Specify
the parent folder
FilePath
= CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
strFolderPath
= FilePath & "\Folders"
Set objFso = Createobject("Scripting.FileSystemObject")
Set objFolder = objFso.GetFolder(strFolderPath)
Set objFolders = objFolder.SubFolders
'Get
number of folders in the Parent folder
intfoldercount
= objFolders.count
Msgbox
"Folder
Counts: "
& intfoldercount
'Collecting
the name of the subfolders
For each objfold in objFolders
strSubFoldername=objfold.Name
msgbox "Folder Names: " & strSubFoldername
'Collecting the path of the subfolders
strSubFolderPath=objfold.path
msgbox "Folder path: " & strSubFolderPath
Next
Comments