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
- ‘---------------------------------------------------------------------------------------
- ' This code is written by Vladimir Dimitrov
- ' It is not to be altered or distributed,
- ' except as part of an application.
- ' You are free to use it in any application,
- ' provided the copyright notice is left unchanged.
- '
- ' Code Courtesy of Vladimir Dimitrov
- '
- ‘---------------------------------------------------------------------------------------
- Function scan_drive(Mypath, FileName, Optional Overwriteit, Optional ScanSubFolders, Optional With_Folders)
- On Error Resume Next
- Dim fso As Object
- Dim myFolder As Object
- Dim fileColl As Object
- Dim aFile As Object
- Dim fs As Object
- Dim outFile As Object
- Dim myline
- Dim i
- Set fs = CreateObject("Scripting.FileSystemObject")
- If IsMissing(Overwriteit) Then
- Set outFile = fs.CreateTextFile(FileName, True)
- Else
- If IsNull(Overwriteit) Then
- Set outFile = fs.CreateTextFile(FileName, True)
- Else
- If Overwriteit = True Then
- Set outFile = fs.CreateTextFile(FileName, True)
- Else
- Set outFile = fs.OpenTextFile(FileName, 8, True, -2)
- End If
- End If
- End If
- Set fso = CreateObject("Scripting.FileSystemObject")
- Set myFolder = fso.GetFolder(Mypath)
- If (IsMissing(With_Folders) = False) Then
- If (With_Folders = True) And (IsNull(With_Folders) = False) Then
- Set fileColl = myFolder.subFolders
- For Each aFile In fileColl
- myline = aFile.name
- myline = myline + "," + ns(aFile.Path)
- myline = myline + "," + ns(aFile.Attributes)
- myline = myline + "," + ns(aFile.DateCreated)
- myline = myline + "," + ns(aFile.DateLastAccessed)
- myline = myline + "," + ns(aFile.DateLastModified)
- myline = myline + "," + ns(aFile.Size)
- myline = myline + "," + ns(aFile.Type)
- outFile.WriteLine myline
- Next
- End If
- End If
- Set fileColl = myFolder.Files
- For Each aFile In fileColl
- myline = aFile.name
- myline = myline + "," + ns(aFile.Path)
- myline = myline + "," + ns(aFile.Attributes)
- myline = myline + "," + ns(aFile.DateCreated)
- myline = myline + "," + ns(aFile.DateLastAccessed)
- myline = myline + "," + ns(aFile.DateLastModified)
- myline = myline + "," + ns(aFile.Size)
- myline = myline + "," + ns(aFile.Type)
- outFile.WriteLine myline
- Next
- outFile.Close
- Set outFile = Nothing
- Set fs = Nothing
- If IsMissing(ScanSubFolders) = False Then
- If IsNull(ScanSubFolders) = False Then
- If ScanSubFolders = True Then
- Set fileColl = myFolder.subFolders
- For Each aFile In fileColl
- i = scan_drive(aFile.Path, FileName, False, ScanSubFolders, With_Folders)
- Next
- End If
- End If
- End If
- Set fileColl = Nothing
- Set myFolder = Nothing
- Set fso = Nothing
- scan_drive = True
- End Function
- ‘---------------------------------------------------------------------------------------
- Function ns(Stri) As String
- On Error Resume Next
- Dim result As String
- If IsNull(Stri) Then
- result = ""
- Else
- result = Stri
- End If
- ns = result
- End Function