Connecting Tech Pros Worldwide Forums | Help | Site Map

Microsoft Scripting Runtime #2

ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,218
#1   Feb 9 '08
This is the 2nd Tip in a series of Tips on the Microsoft Scripting Runtime Library. The 1st Tip related to Drives, while this Tip will concentrate on the Folders (Directories) in your PC and various methods to retrieve information from and how to manipulate them. The code should be self explanatory and basically documents itself. Should there be any questions related to this Topic, please feel free to ask.
Expand|Select|Wrap|Line Numbers
  1. 'Accessing Folder Properties
  2. Dim fso As FileSystemObject, fldr As Folder
  3. Set fso = New Scripting.FileSystemObject    'OR
  4. 'Set fso = CreateObject("Scripting.FileSystemObject")
  5.  
  6. 'Creates an Instance of a Folder
  7. Set fldr = fso.GetFolder("C:\Windows\System32")
  8.  
  9. Debug.Print "Folder Properties of " & fldr.Path
  10. Debug.Print "  Folder Name: " & fldr.Name
  11. Debug.Print "  Folder Size: " & FormatNumber(fldr.Size, 0, vbFalse, vbFalse, vbTrue) & " bytes"
  12. Debug.Print "  Folder Created: " & fldr.DateCreated
  13. Debug.Print "  Folder Path: " & fldr.Path
  14. Debug.Print "  Folder Last Modified: " & fldr.DateLastModified
  15. Debug.Print "  Folder Last Accessed: " & fldr.DateLastAccessed
  16. Debug.Print "  Folder Drive: " & fldr.Drive
  17. Debug.Print "  Files in Folder: " & FormatNumber(fldr.Files.Count, 0, vbFalse, vbFalse, vbTrue) & " files"
  18. Debug.Print "  Is this a Root Folder: " & fldr.IsRootFolder
  19. Debug.Print "  Parent Folder: " & fldr.ParentFolder
  20. Debug.Print "  Folder Path: " & fldr.Path
  21. 'Could also use the GetBaseName Method of the File System Object
  22. Debug.Print "  Folder Short Name: " & fldr.ShortName
  23. Debug.Print "  Folder Short Path: " & fldr.ShortPath
  24. Debug.Print "  Number of Sub-Folders: " & fldr.SubFolders.Count
  25. Debug.Print "  Folder Type: " & fldr.Type
  26. Debug.Print
  27.  
  28.  
  29. 'Code that creates a Temporary Folder (fso previously created)
  30. Dim msTemporaryFolder As String
  31. msTemporaryFolder = fso.GetSpecialFolder(TemporaryFolder).Path & "\"
  32. Debug.Print msTemporaryFolder
  33. Debug.Print
  34.  
  35.  
  36. 'Code to see if a Folder exists (fso previously created)
  37. 'Could also use the FolderExists Method of the File System Object
  38. If fso.FolderExists("C:\Test") Then
  39.   Debug.Print "Folder exists"
  40. Else
  41.   Debug.Print "Folder does not exist"
  42. End If
  43. Debug.Print
  44.  
  45.  
  46. 'Code to create a Folder (fso previously created)
  47. 'Could also use the CreateFolder Method of the File System Object
  48. Set fldr = fso.CreateFolder("C:\Windows\System32\Temp Extraction Area")
  49. Debug.Print "Created Folder " & fldr.Name
  50.  
  51.  
  52. 'Code to Delete a Folder (fso previously created)
  53. 'Could also use the DeleteFolder Method of the File System Object
  54. Set fldr = fso.GetFolder("C:\Folder 1\Folder 2\Folder to Delete")
  55. fldr.Delete True       '"Folder to Delete" is Deleted
  56.  
  57.  
  58. 'Code to Move a Folder (fso previously created)
  59. 'Could also use the MoveFolder Method of the File System Object
  60. Set fldr = fso.GetFolder("C:\Folder1\Folder2\Folder3")
  61. fldr.Move "C:\"     '"Folder3" moved to C:\
  62.  
  63.  
  64. 'Code to Copy a Folder (fso previously created)
  65. 'Could also use the CopyFolder Method of the File System Object
  66. Set fldr = fso.GetFolder("C:\Folder1\Folder2")
  67. fldr.Copy "C:\List", True       'Copies "Folder2" to C:\List
OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. Folder Properties of C:\WINDOWS\system32
  2.   Folder Name: system32
  3.   Folder Size: 888,994,639 bytes
  4.   Folder Created: 10/1/2005 2:46:52 AM
  5.   Folder Path: C:\WINDOWS\system32
  6.   Folder Last Modified: 2/5/2008 7:04:21 PM
  7.   Folder Last Accessed: 2/5/2008 7:04:22 PM
  8.   Folder Drive: C:
  9.   Files in Folder: 2,309 files
  10.   Is this a Root Folder: False
  11.   Parent Folder: C:\WINDOWS
  12.   Folder Path: C:\WINDOWS\system32
  13.   Folder Short Name: System32
  14.   Folder Short Path: C:\Windows\System32
  15.   Number of Sub-Folders: 50
  16.   Folder Type: File Folder
  17.  
  18. C:\DOCUME~1\Armund\LOCALS~1\Temp\
  19.  
  20. Folder exists
  21.  
  22. Created Folder Temp Extraction Area



Reply


Similar Microsoft Access / VBA bytes