Link to home
Start Free TrialLog in
Avatar of Brindha
Brindha

asked on

Finding a file


I have to find if an exe file exists using Visual Basic 6. The file can be in any path or drive.I should check if the file is present without specifying the path explicitly.This is similar to the Find file option in windows. How do i write VB code for this.I have written the code to identify the drives present.

Code:

Private Sub Command1_Click()
Dim myfile, mypath, myname, nextPath
Set fso = CreateObject("Scripting.FileSystemObject")
Set dc = fso.Drives
For Each d In dc
    s = d.DriveLetter
    If s <> "A" Then
    mypath = "" & s & ":\Program Files\Microsoft Office\Office\"
       myname = Dir(mypath)
    Do While myname <> ""
    'If myname <> "." & myname <> ".." Then
    'If (GetAttr(mypath & myname) And vbDirectory) = vbDirectory Then
            'Debug.Print myname
            If myname = "EXCEL.EXE" Then MsgBox myname & " - Excel present"
            If myname = "WINWORD.EXE" Then MsgBox myname & " - Word Present"
    'End If
    'End If
   myname = Dir
   Loop
   End If
Next
End Sub
Avatar of AzraSound
AzraSound
Flag of United States of America image

very good example here for download:
http://www.mvps.org/btmtz/drvscan/
Avatar of riduce
riduce

This should work

Option Explicit
    Dim fileCo As Integer
    Dim thisPath As String
    Dim filecount As Single
    Dim filelentot As Single

Private Sub FindFileBtn_Click()
    Dim inspectfilename, findperiod, findstar, i
    Dim driveselected As String, msg As String
    Dir2.Path = App.Path
   filelentot = 0: filecount = 0

    list1.Clear     'clear the listbox
       
    File1.Pattern = "execl.exe"  'gives the program the pattern to look for
    driveselected = Left$(Drive1.Drive, 2) & "\" 'tells which drive is selected
    Drive1.Drive = "c:\"
    Dir1.Path = Drive1.Drive
    File1.Path = Dir1.Path
    ChDir "\"   'starts at the root directory
    GetFileList driveselected    'makes the call to the getfilelist proc
 'I have returned from the get file list procedure

   
    If filelentot > 0 Then '
        status = "Size of files is - " & Format(filelentot, "###,###,###")
        status = status & " Bytes" & "    File Count - " & Format(filecount, "###,###")
    Else
        Me.status.Visible = False
    End If

MsgBox "File not present", vbOKOnly
End Sub


Private Sub GetFileList(ByVal p$)

Dim T$, i As Integer

If Dir1.ListCount > 0 Then
  For i = 0 To Dir1.ListCount - 1
    T$ = Dir1.List(i)
    GetFileList (T$)
    Dir1.Path = p$
  '  Me.status = p$
  '  Me.status.Refresh
  Next
End If

End Sub
ASKER CERTIFIED SOLUTION
Avatar of wsh2
wsh2

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Riduce:
How can you propose that as a definitive answer?.. First of all the questioner is using FSO for accessing the tree structure.. Second.. you fail to mention that Drive, Directory and File controls have got to be added to the program.. Third.. there is are APIs that will achieve the same thing you are doing only faster.

Please let the questioner decide which is the best comment.. not force your answer upon them.. <smile>.

Brindha:
Unless Riduce's answer is conclusive for you, REJECT his answer and allow others to comment. When you have decided which is the best answer for your purpose.. then select the comment you feel is best. Mine, Riduce's or anyone elses.. <smile>.
Hi Brindha
  Try following

Before that open new Project
On a for drag CommandButton and List Box
Then

Paste the following Code


Option Explicit
Public fso As New FileSystemObject
Public fld As Folder
     

Private Sub Command1_Click()
Dim nDirs As Integer, nFiles As integer , lSize As Long Dim sDir As String, sSrchString As String
sDir = InputBox("Please enter the   directory to search", _
                        "FileSystemObjects example", "C:\")
        sSrchString = InputBox("Please enter the file name to search", _
                        "FileSystemObjects example", "vb.ini")
        MousePointer = vbHourglass
        Label1.Caption = "Searching " & vbCrLf & UCase(sDir) & "..."
        lSize = FindFile(sDir, sSrchString, nDirs, nFiles)
        MousePointer = vbDefault
        MsgBox Str(nFiles) & " files found in" & Str(nDirs) & _
               " directories", vbInformation
        MsgBox "Total Size = " & lSize & " bytes"
        End Sub

Private Function FindFile(ByVal sFol As String, sFile As String, _
        nDirs As Integer, nFiles As Integer) As Long
        Dim tFld As Folder, tFil As File, FileName As String
        Set fld = fso.GetFolder(sFol)
        FileName = Dir(fso.BuildPath(fld.Path, sFile), vbNormal Or vbHidden Or vbSystem Or vbReadOnly)
            While Len(FileName) <> 0
                FindFile = FindFile + FileLen(fso.BuildPath(fld.Path, FileName))
                nFiles = nFiles + 1
                List1.AddItem fso.BuildPath(fld.Path, FileName)  ' Load ListBox
                FileName = Dir()  ' Get next file
                DoEvents
            Wend
                Label1 = "Searching " & vbCrLf & fld.Path & "..."
                nDirs = nDirs + 1
            If fld.SubFolders.Count > 0 Then
                For Each tFld In fld.SubFolders
                    DoEvents
                    FindFile = FindFile + FindFile(tFld.Path, sFile, nDirs, nFiles)
                Next
            End If
End Function

if any problem mail me at hijai23@yahoo.com
Good Luck
Bye
Jay
<g>
Mine's *fast*
<end g>
riduce changed the proposed answer to a comment
Welp.. there ya go Brindha.. an API solution by Caraf_g.. a FSO solution by Jdhananjay.. a pure VB solution from Rudice.. and a file association solution from wsh2.. <smile>.

Now, its your turn to decide which best fills your needs.. Enjoy !!!
There are many ways to skin a cat.

Keep your hands of mine though! Meow...
Hai y u worry Brindha. VB5 has given u the option. Just search for WinSeek.Vbp

That wiil solve your problem. Get back to me. (With points probably)
lolbôks
Gosh caraf_g.. you never cease to stump me.. now what the heck is lolbo(with a hat thing on top)ks?.. <lol>.
Lolbôks

closely related term:

Draosbôks

Also:

Knoetsoër

They are all Limburgs expressions and as such not really translatable.

Literally, lolbôks would be translated as funnypants, but in English it doesn't have quite the same impact ;-)
caraf_g:
<looking at his lap and stating meekly>.. lolboks?.. <sheesh>.
As opposed to Lederhosen which aren't funny at all.
check the samples on the vb6 cd there is a prog on there to do the job