Anyone who can help,
I'm in the process of developing a VB program (.exe) that I can run on my file servers to detect & print changes in the file system of a particular drive. The code I have so far will only search SOME directories for files but misses other dirs. I can't figure out why. Also, it will only recursively examine under the directory that the .exe was saved in. Can anyone at least tell me if I'm headed in the right direction to reach my goal? Goal: To create a comma-delimitted file that lists every file and its properties (size, attribs., etc.) on a particular drive. Then to compare it a month later with the current file system and print out changes.
Ideas welcome. Thanks.
Here's what I have so far (some of the code is probably not necessary):
Option Explicit
Dim mFileSysObj As New FileSystemObject
Dim fs As New FileSystemObject
Dim fso As New FileSystemObject
Dim File As String
Dim a
Dim fld As Folder
Dim DPath As String
Dim sDir As String
Dim nFiles As Variant
Private Sub Command1_Click()
Dim lSize As Variant
Dim sSrchString As String
sDir = Drive1.Drive
DPath = Left(sDir, 2)
sSrchString = "*.*"
File = (DPath & "\Snapshot.txt"
Set fs = CreateObject "Scripting.FileSystemObject"
Set a = fs.CreateTextFile(File, True)
lSize = FindFile(DPath, sSrchString, nFiles)
MsgBox "Snapshot of " & sDir & " taken successfully." & vbCrLf & vbCrLf & DPath & "\snapshot.txt created."
a.Close
End Sub
Private Function FindFile(ByVal sFol As String, sFile As String, nFiles As Variant) As Variant
Dim tFld As Folder
Dim tFil As File
Dim FileName As String
Dim theFile As File
Set fso = CreateObject("Scripting.FileSystemObject"
Set fld = fso.GetFolder(sFol)
FileName = Dir(fso.BuildPath(fld.Path, sFile), vbNormal Or vbHidden Or vbSystem Or vbReadOnly)
Set fso = CreateObject("Scripting.FileSystemObject"
Set theFile = mFileSysObj.GetFile(fld.Path & "\" & FileName)
While Len(FileName) <> 0
On Error Resume Next
FindFile = FindFile + FileLen(fso.BuildPath(fld.Path, FileName))
a.WriteLine fso.BuildPath(fld.Path, FileName) & "," & theFile.DateCreated & "," & theFile.DateLastAccessed & "," & theFile.DateLastModified & "," & theFile.Size & "," & theFile.Attributes
FileName = Dir()
DoEvents
Wend
Label1.Caption = "Taking Snapshot" & vbCrLf & fld.Path & "..."
If fld.SubFolders.Count > 0 Then
For Each tFld In fld.SubFolders
DoEvents
FindFile = FindFile + FindFile(tFld.Path, sFile, nFiles)
On Error Resume Next
Next
End If
End Function
Private Sub Command2_Click()
Unload Form1
End Sub
This code is a mess. Any suggestions on cleaning it up?
Sorry for the *HUGE* post.
db
I'm in the process of developing a VB program (.exe) that I can run on my file servers to detect & print changes in the file system of a particular drive. The code I have so far will only search SOME directories for files but misses other dirs. I can't figure out why. Also, it will only recursively examine under the directory that the .exe was saved in. Can anyone at least tell me if I'm headed in the right direction to reach my goal? Goal: To create a comma-delimitted file that lists every file and its properties (size, attribs., etc.) on a particular drive. Then to compare it a month later with the current file system and print out changes.
Ideas welcome. Thanks.
Here's what I have so far (some of the code is probably not necessary):
Option Explicit
Dim mFileSysObj As New FileSystemObject
Dim fs As New FileSystemObject
Dim fso As New FileSystemObject
Dim File As String
Dim a
Dim fld As Folder
Dim DPath As String
Dim sDir As String
Dim nFiles As Variant
Private Sub Command1_Click()
Dim lSize As Variant
Dim sSrchString As String
sDir = Drive1.Drive
DPath = Left(sDir, 2)
sSrchString = "*.*"
File = (DPath & "\Snapshot.txt"
Set fs = CreateObject "Scripting.FileSystemObject"
Set a = fs.CreateTextFile(File, True)
lSize = FindFile(DPath, sSrchString, nFiles)
MsgBox "Snapshot of " & sDir & " taken successfully." & vbCrLf & vbCrLf & DPath & "\snapshot.txt created."
a.Close
End Sub
Private Function FindFile(ByVal sFol As String, sFile As String, nFiles As Variant) As Variant
Dim tFld As Folder
Dim tFil As File
Dim FileName As String
Dim theFile As File
Set fso = CreateObject("Scripting.FileSystemObject"
Set fld = fso.GetFolder(sFol)
FileName = Dir(fso.BuildPath(fld.Path, sFile), vbNormal Or vbHidden Or vbSystem Or vbReadOnly)
Set fso = CreateObject("Scripting.FileSystemObject"
Set theFile = mFileSysObj.GetFile(fld.Path & "\" & FileName)
While Len(FileName) <> 0
On Error Resume Next
FindFile = FindFile + FileLen(fso.BuildPath(fld.Path, FileName))
a.WriteLine fso.BuildPath(fld.Path, FileName) & "," & theFile.DateCreated & "," & theFile.DateLastAccessed & "," & theFile.DateLastModified & "," & theFile.Size & "," & theFile.Attributes
FileName = Dir()
DoEvents
Wend
Label1.Caption = "Taking Snapshot" & vbCrLf & fld.Path & "..."
If fld.SubFolders.Count > 0 Then
For Each tFld In fld.SubFolders
DoEvents
FindFile = FindFile + FindFile(tFld.Path, sFile, nFiles)
On Error Resume Next
Next
End If
End Function
Private Sub Command2_Click()
Unload Form1
End Sub
This code is a mess. Any suggestions on cleaning it up?
Sorry for the *HUGE* post.
db