Thursday, May 13, 2010

File System Object Vs Folders

Size a Folder Copy a Folder Move a Folder Delete a Folder File Count in a folder SubFolder Count in a Folder Add a New Folder Filenames in a folder SubFoldernames in a Folder
'Syntax:
Copy
FolderObject.Copy(destination[, overwrite])
MoveFolder
FolderObject.MoveFolder(source, destination)
Delete a Folder
FolderObject.DeleteFolder(folderspec[, false])

'Size a Folder
Dim SourceFolder, oFSO
SourceFolder = "C:\IM"
Set oFSO = CreateObject("Scripting.FileSystemObject")
oFSO.GetFolder(SourceFolder)
Msgbox "Size of the Folder "&oFSO.Size&" bytes" 'Display the size of the folder

'Copy a Folder
Dim SourceFolder, oFSO
SourceFolder = "C:\IM"
Set oFSO = CreateObject("Scripting.FileSystemObject")
oFSO.CopyFolder SourceFolder, "D:\"

'Move a Folder
Dim SourceFolder, DestinationFolder, oFSO
SourceFolder = "C:\IM"
DestinationFolder = "D:\"
Set oFSO = CreateObject("Scripting.FileSystemObject")
oFSO.MoveFolder SourceFolder, DestinationFolder

'Delete a Folder
Dim SourceFolder, oFSO
SourceFolder = "D:\IM"
Set oFSO = CreateObject("Scripting.FileSystemObject")
oFSO.DeleteFolder SourceFolder

'Get the File Count in a folder
Set FSO = CreateObject("Scripting.FileSystemObject")
FolderPath = "C:\Documents and Settings\"
Set TestFolder = FSO.GetFolder(FolderPath)
Set Files = TestFolder.Files
MsgBox("Number of Files in:"&vbCrLf&FolderPath&vbcrlf&Files.Count)

'Get the SubFolder Count in a Folder
Set FSO = CreateObject("Scripting.FileSystemObject")
Set TestFolder = FSO.GetFolder("C:\Documents and Settings")
Set SubFolders = TestFolder.SubFolders 'Returns the collection of subfolders
Msgbox(SubFolders.Count)

'Add a New Folder
Sub AddNewFolder(FolderPath, FolderName)
Dim FSO, f, fc,nf
Set FSO = CreateObject("Scripting.FileSystemObject")
Set f = FSO.GetFolder(FolderPath)
Set fc = f.SubFolders
If FolderName <> "" Then
Set nf = fc.Add(FolderName)
Else
Set nf = fc.Add("New Folder")
End If
End Sub
AddNewFolder "C:\",""

'Filenames in a Folder
Folderpath = "C:\MyFolder"
Dim FSO, oFolder
Set FSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = FSO.GetFolder(FolderPath)
Set Files = oFolder.Files 'Gets the collection of files in a folder
For each file in Files
Msgbox File.Name
Next

'SubFoldernames in a Folder
Folderpath = "C:\MyFolder"
Dim FSO, oFolder
Set FSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = FSO.GetFolder(FolderPath)
Set Folders = oFolder.SubFolders 'Gets the collection of subfolders in a folder
For each folder in Folders
Msgbox folder.Name
Next

1 comment: