473,406 Members | 2,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Scan for files and folders and their properties

MMcCarthy
14,534 Expert Mod 8TB
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
0 2850

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Glen | last post by:
As I understand it, when the first page of an application is accessed, all ASPX/ASCX/etc. files in the same folder are compiled using the JIT compiler. Is there a way to turn this feature off? ...
4
by: SStory | last post by:
I have an app that includes media files (graphics) and those included in the msi setup. They are not 100% necessary and can change. The problem is if I delete one of them, then the next time I...
10
by: Dan | last post by:
Hi - I'm about a week into learning VB.NET, and I'm finding I can't delete any of the VB.NET directory structures that contain my test projects I've been trying to create. I've never seen this...
2
by: Ronald S. Cook | last post by:
I'm wanting to scan all folders and files under a specified path (e.g. C:\Files). I know using System.IO I can use Directory.GetFiles to get a list of files under a folder, but how do I get the...
3
by: Kimera.Kimera | last post by:
I'm trying to write a program in VB.net 2003 that basically deletes all files, folders, sub-folders and sub-sub folders (etc). The program is simply for deleting the Windows/Temp folder contents,...
5
by: rogersw8n | last post by:
Some how, some way the account that creates folders under Temporary Internet files has been changed to a domain account for VS 2003 and VS 2005. I recently installed VS 2005. All seemed to be ok...
3
by: =?Utf-8?B?Tkg=?= | last post by:
Hi, I am looking for some code that would scan a folder structure and files in those folders and display them on a web page so that people can click on the file links and open the files. There...
4
by: Joey | last post by:
asp.net 2/C#/VS2005 I have a web app where I use a stream writer to create a CSV (text) file. I then use my code to save the file in the site's 'App_Data' folder, in a subfolder called 'Temp'....
3
by: =?Utf-8?B?RGF2aWRN?= | last post by:
Hi, does anyone know if I can scan a subdirectory using wildcards? I do not see a way to do this using: string folder = @"C:\INPUT\CUSTOMERS\ID???BC.*" DirectoryInfo di = new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.