By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,145 Members | 1,485 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,145 IT Pros & Developers. It's quick & easy.

Scan for files and folders and their properties

MMcCarthy
Expert Mod 10K+
P: 14,534
This is a module that scans for files and folders on a specified path and describe them in comma separated values file in a text format.

The information is stored in this file consecutively like:
1. File Name
2. File Path and Name
3. Attributes as integer
4. Date Created
5. Date Last Accessed
6. Date Last Modified
7. Size
8. Type

Once the data is saved, you can import it easily in MS Access using the module to import from csv files.

The field delimiter is ,.

This Structure is true also for files and folders! If there is Permissions problem the structure of the respective places can be damaged.

The function that does this work is: scan_drive(Mypath, FileName, Optional Overwriteit, Optional ScanSubFolders, Optional With_Folders)

Parameters:

Mypath: The Path that should be scanned for files or folders. For exemple : C:\
FileName: The name of the file and its path where the information will be saved. If the file exists it will be overwritten or appended.
Overwriteit: If this option is True than the file where is stored the information is Overwriten, if not the new data is appended to the end of the file. The value by default is True.
ScanSubFolders: If true so the function will scan in the subfolders in the specified Path. The default value is False
With_Folders: If true this option, it will enumerate the files and the folders in the specified path. If false only the files!

The study of this module can teach you:

  • Creating functions in Visual Basic with arguments
  • Declaring and using Optional arguments in functions
  • Declaring variables
  • Creating files using Scripting.FileSystemObject
  • Opening files using Scripting.FileSystemObject for Append
  • Accessing objects and properties from Scripting.FileSystemObject
  • Working with the file like as stream
  • Some Stream properties and methods
  • Using recursion for achieving the sub scan
Expand|Select|Wrap|Line Numbers
  1. ---------------------------------------------------------------------------------------
  2. ' This code is written by Vladimir Dimitrov
  3. ' It is not to be altered or distributed,
  4. ' except as part of an application.
  5. ' You are free to use it in any application,
  6. ' provided the copyright notice is left unchanged.
  7. '
  8. ' Code Courtesy of Vladimir Dimitrov
  9. '
  10. ---------------------------------------------------------------------------------------
  11.  
  12. Function scan_drive(Mypath, FileName, Optional Overwriteit, Optional ScanSubFolders, Optional With_Folders)
  13. On Error Resume Next
  14. Dim fso As Object
  15. Dim myFolder As Object
  16. Dim fileColl As Object
  17. Dim aFile As Object
  18. Dim fs As Object
  19. Dim outFile As Object
  20. Dim myline
  21. Dim i
  22.  
  23.     Set fs = CreateObject("Scripting.FileSystemObject")
  24.     If IsMissing(Overwriteit) Then
  25.         Set outFile = fs.CreateTextFile(FileName, True)
  26.     Else
  27.         If IsNull(Overwriteit) Then
  28.             Set outFile = fs.CreateTextFile(FileName, True)
  29.         Else
  30.             If Overwriteit = True Then
  31.                 Set outFile = fs.CreateTextFile(FileName, True)
  32.             Else
  33.                 Set outFile = fs.OpenTextFile(FileName, 8, True, -2)
  34.             End If
  35.         End If
  36.     End If
  37.  
  38.     Set fso = CreateObject("Scripting.FileSystemObject")
  39.     Set myFolder = fso.GetFolder(Mypath)
  40.  
  41.     If (IsMissing(With_Folders) = False) Then
  42.  
  43.         If (With_Folders = True) And (IsNull(With_Folders) = False) Then
  44.             Set fileColl = myFolder.subFolders
  45.             For Each aFile In fileColl
  46.  
  47.                 myline = aFile.name
  48.                 myline = myline + "," + ns(aFile.Path)
  49.                 myline = myline + "," + ns(aFile.Attributes)
  50.                 myline = myline + "," + ns(aFile.DateCreated)
  51.                 myline = myline + "," + ns(aFile.DateLastAccessed)
  52.                 myline = myline + "," + ns(aFile.DateLastModified)
  53.                 myline = myline + "," + ns(aFile.Size)
  54.                 myline = myline + "," + ns(aFile.Type)
  55.                 outFile.WriteLine myline
  56.             Next
  57.         End If
  58.     End If
  59.  
  60.     Set fileColl = myFolder.Files
  61.     For Each aFile In fileColl
  62.  
  63.         myline = aFile.name
  64.         myline = myline + "," + ns(aFile.Path)
  65.         myline = myline + "," + ns(aFile.Attributes)
  66.         myline = myline + "," + ns(aFile.DateCreated)
  67.         myline = myline + "," + ns(aFile.DateLastAccessed)
  68.         myline = myline + "," + ns(aFile.DateLastModified)
  69.         myline = myline + "," + ns(aFile.Size)
  70.         myline = myline + "," + ns(aFile.Type)
  71.         outFile.WriteLine myline
  72.     Next
  73.  
  74.     outFile.Close
  75.     Set outFile = Nothing
  76.     Set fs = Nothing
  77.     If IsMissing(ScanSubFolders) = False Then
  78.         If IsNull(ScanSubFolders) = False Then
  79.             If ScanSubFolders = True Then
  80.                 Set fileColl = myFolder.subFolders
  81.                 For Each aFile In fileColl
  82.                     i = scan_drive(aFile.Path, FileName, False, ScanSubFolders, With_Folders)
  83.                 Next
  84.             End If
  85.         End If
  86.     End If
  87.     Set fileColl = Nothing
  88.     Set myFolder = Nothing
  89.     Set fso = Nothing
  90.     scan_drive = True
  91.  
  92. End Function
  93. ---------------------------------------------------------------------------------------
  94.  
  95.  
  96.  
  97.  Function ns(Stri) As String
  98. On Error Resume Next
  99. Dim result As String
  100.  
  101.     If IsNull(Stri) Then
  102.         result = ""
  103.     Else
  104.         result = Stri
  105.     End If
  106.     ns = result
  107.  
  108. End Function
  109.  
Jan 8 '07 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.