Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, been doing VB and VBScript of 3 days now and I think it is time to ask for help, so help

Ok I need to be able to search for multipul file extensions, lets say xls and mdb (might be more) on a local machine with more than 1 local drive (no more than 3) and then export that to a file say xls or txt with the following information.

Name of file
Location of the file
Last accessed time

and if possible the machine name that was searched.

I have a bsic VBScript I have managed to write that lets me search a folder on my pc for txt files

VB
strDir = "D:\USERDATA\RowellCJ\My Documents"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objDir = FSO.GetFolder(strDir)
getInfo(objDir)
Sub getInfo(pCurrentDir)
For Each aItem In pCurrentDir.Files
    If LCase(Right(Cstr(aItem.Name), 3)) = "txt" 
    Then wscript.Echo aItem.Name 
   End If
Next
For Each aItem In pCurrentDir.SubFolders   
	getInfo(aItem) 
Next
End Sub


and this VB code in Visual studio with help from books and google that runs on a folder and produces a report for me but

VB
Imports System.IO
Public Class Form1
    Function IsMatch(ByVal FExt As String) As Boolean
        Dim AllowedExts() As String = {".xls", ".xlsx", ".mdb", ".accdb"}
        For Each Ext As String In AllowedExts 
            If FExt = Ext Then Return True
        Next
        Return False 
    End Function
    Sub GetNames(ByVal DirPath As String)
        Dim objFileInfo As FileInfo 
        Dim objDir As DirectoryInfo = New DirectoryInfo(DirPath)
        Dim objSubFolder As DirectoryInfo
        Dim finfo As FileInfo
        Cursor = Cursors.WaitCursor
        Try
            For Each objFileInfo In objDir.GetFiles()
                finfo = My.Computer.FileSystem.GetFileInfo(objFileInfo.FullName)
                If IsMatch(objFileInfo.Extension) 
Then  TextBox1.AppendText(objFileInfo.FullName & "," & objFileInfo.LastAccessTime.ToShortDateString & _
                                       "," & objFileInfo.LastAccessTime.ToShortTimeString & vbCrLf) 
                End If
            Next
            For Each objSubFolder In objDir.GetDirectories()
                GetNames(objSubFolder.FullName)
            Next
        Catch Ex As Exception
            TextBox1.AppendText("Error fetching file/sub-dir" & vbCrLf)
        End Try
        Cursor = Cursors.Default
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim StrPath As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) 
        Dim StrName As String = "\File_Audit_Of-" & System.Environment.MachineName & "-Date=" & _
        Now.ToShortDateString & "-Time=" & Now.ToShortTimeString & ".txt"         StrName = StrName.Replace("/", "-") 
        StrName = StrName.Replace(":", "-")
        Dim StrTemp As String = StrPath & StrName 
        TextBox1.Text = System.Environment.MachineName & vbCrLf         GetNames(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)) 
        Dim sw As New StreamWriter(StrTemp) 
        sw.Write(TextBox1.Text) 
        sw.WriteLine() 
        sw.Close() 
        MsgBox("File saved as " & StrTemp) 
    End Sub
End Class


This runs on a folder and produces a report for me but how do I get this into a VBScript and do what I need as getting really confused and the more i get confused the harder it is becoming to see the answer.

Will keep going but advice and help will be really appricated

Chris
Posted

1 solution

You add VB code in a class and invoke it from VBScript.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900