how to search for a file in the computer and execute

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ravindarjobs
    New Member
    • May 2007
    • 86

    how to search for a file in the computer and execute

    hello friends,

    i want to seach for a file in the computer. i dont know the exact location where it is. so the requirement is to search entire hard disk and get the path of the specified file.

    after that we now save the path in a string variable.

    now, we have to execute the appication with the path stored in the string.

    may be using shell.


    my problem is, i know only how to search for a file in a particular drive, but how do we perform a entire hard disk check?

    and after we store the path in a string variable, say st.

    if i execute with the code

    Shell st,vbNormalFocu s

    or Shell " & st & ",vbNormalF ocus

    and in many ways i tried.
    but i couldnt work out.

    can any body please help me
  • dthompson
    New Member
    • May 2007
    • 8

    #2
    You looking to do a recursive search
    There are many example of such searches on the web, I have written code in the past to do this, if you can wait 24hrs, I may be able to locate it for you.

    As for the execution of the file once located and hopefully only 1 instance is located.

    Normal Shell command should work in this instance. If not I like using Windows Scripting Objects, below are a couple options.

    Assuming the search returns a true if the path (strPath) searched contains the file (defined for example as MyFile.exe) then
    strFile = strPath & "\MyFile.ex e"
    To options come to mind (Assuming your string is strFile).
    Dim objShell as Object
    objShell = CreateObject ("WScript.Shell ")
    objShell.run(st rFile)
    OR
    Dim objShell, objCmd
    objShell = CreateObject("W Script.Shell")
    objCmd = objShell.Exec(s trFile)

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Originally posted by ravindarjobs
      ...
      Shell st,vbNormalFocu s
      Assuming you have the path and (executable) filename in st, this should work. However, it might depend on what version of VB you're using.

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Originally posted by dthompson
        ...I have written code in the past to do this, if you can wait 24hrs, I may be able to locate it for you.
        Yeah, I've done it once or twice, too.

        This sounds like a good sample to add to the How-To series in the VB Articles area.

        Comment

        • danp129
          Recognized Expert Contributor
          • Jul 2006
          • 323

          #5
          I got started good but trying to figure out if I want to put folder recursion in same sub or different one...

          [CODE=vb]'Constants needed if not using MS Scripting Runtime referece
          'Const UnknownType=0
          'Const Removable=1
          'Const Fixed=2
          'Const Remote=3
          'Const CDRom=4
          'Const RamDisk=5
          Option Explicit
          Private Sub Command1_Click( )
          'Call Search
          Call SearchSystem(Fi xed Or CDRom)
          End Sub

          Sub SearchSystem(Dr iveTypes)
          'Replace As with 'As if not using MS Scripting Runtime reference
          Dim fs As FileSystemObjec t
          Dim dc As Drives
          Dim oDrive As Drive
          Set fs = CreateObject("S cripting.FileSy stemObject")
          Set dc = fs.Drives

          Debug.Print "---"

          For Each oDrive In dc
          'Use bitwise expression to see if Drivetype is one of the Drivetypes we wish to search
          If (oDrive.DriveTy pe And DriveTypes) = oDrive.DriveTyp e Then
          'oDrive is a drive type we wish to search
          Debug.Print oDrive.DriveLet ter & " - " & oDrive.DriveTyp e
          End If
          Next 'oDrive
          Set fs = Nothing
          Set dc = Nothing
          End Sub
          [/CODE]

          Comment

          • ravindarjobs
            New Member
            • May 2007
            • 86

            #6
            Originally posted by dthompson
            You looking to do a recursive search
            There are many example of such searches on the web, I have written code in the past to do this, if you can wait 24hrs, I may be able to locate it for you.
            yes friend, i can wait. or else give me the link when u find it, i will go through it.

            one more thing i have not mentioned is currently i am using MsAccess2003, and vb6 code

            thanq
            Last edited by Killer42; May 19 '07, 01:31 AM. Reason: Added [/QUOTE] tag

            Comment

            • danp129
              Recognized Expert Contributor
              • Jul 2006
              • 323

              #7
              Give this a try, You'll need to add the shell command yourself
              [CODE=vb]'Add MS Scripting Runtime reference
              'Const UnknownType=0
              'Const Removable=1
              'Const Fixed=2
              'Const Remote=3
              'Const CDRom=4
              'Const RamDisk=5
              Option Explicit
              Private Sub Command1_Click( )
              Dim mySearchPattern As String
              mySearchPattern = "*.exe"
              'look at constants above, use 'OR' + drive type to specify what type of drives to search. I added OR CDRom below
              Call SearchSystem(Fi xed Or CDRom, mySearchPattern )
              End Sub

              Sub SearchSystem(Dr iveTypes As Integer, _
              FileSpec As String, _
              Optional oFolder = Empty)
              'Replace As with 'As if not using MS Scripting Runtime reference
              Dim fs As FileSystemObjec t
              Dim dc As Drives
              Dim oDrive As Drive
              Dim ofld As Folder
              Dim file
              Dim subdirs As New Collection
              Dim subdir

              Set fs = CreateObject("S cripting.FileSy stemObject")
              Set dc = fs.Drives
              On Error GoTo errorhandler:
              If oFolder <> "" Then
              file = Dir$(oFolder & FileSpec)
              Do While Len(file)
              'File found
              file = oFolder & file

              'PUT YOUR CODE HERE TO EXECUTE FILE
              Debug.Print file

              'get next filename
              file = Dir$()
              Loop

              file = Dir$(oFolder & "*.*", vbDirectory)
              Do While Len(file)
              ' we've found a new directory
              If file = "." Or file = ".." Then
              ' exclude "." and ".."
              ElseIf (GetAttr(oFolde r & file) And vbDirectory) = 0 Then
              ' ignore regular files
              Else
              ' this is a directory, include the path in the collection
              subdirs.Add oFolder & file
              End If
              ' get next directory
              file = Dir$()
              Loop

              ' parse each subdirectory
              For Each subdir In subdirs
              Call SearchSystem(Dr iveTypes, FileSpec, subdir & "\")
              Next
              End If

              If oFolder = "" Then
              For Each oDrive In dc
              'Use bitwise expression to see if Drivetype is one of the Drivetypes we wish to search
              If (oDrive.DriveTy pe And DriveTypes) = oDrive.DriveTyp e Then
              'oDrive is a drive type we wish to search
              Debug.Print "Searching: " & oDrive.Path
              If oDrive.IsReady Then Call SearchSystem(Dr iveTypes, FileSpec, oDrive.Path & "\")
              End If
              Next 'oDrive
              Debug.Print "finished"
              End If
              Set fs = Nothing
              Set dc = Nothing
              Exit Sub
              errorhandler:
              If Err.Number = 75 Then
              'Path/File access error (file locked for reading or doesn't exist?)
              Debug.Print Err.Description
              Resume Next
              Else
              Debug.Print Err.Description
              MsgBox Err.Description & vbCrLf & "Hit CTRL+BREAK to debug or OK to quit."
              End If
              End Sub[/CODE]

              Comment

              • ravindarjobs
                New Member
                • May 2007
                • 86

                #8
                Originally posted by danp129
                Give this a try, You'll need to add the shell command yourself
                i am very glad dear friend.
                with the code given by you, i could perform the entire hard disk check.
                thanq


                now i got one more problem,
                when i find the path of the executable file, i used the statement,
                Shell file,vbNormalFo cus

                but it has given an error.

                i am searching for a file with the name IFs.exe, it is located in
                c:\Program Files\IFs\IFs.e xe

                since this path will be stored in variable file,
                i executed the file with the code
                Shell file,vbNormalFo cus

                it displayed a dialog box with a text box saying,
                "Specify the location for IFs
                IFs does not exists on c:\ Documents and settings\Ravi\M y Documents\.
                please specify the path"


                actually my file location was c:\program files
                but why it is searchig in Documents and Settings even though i specified the correct path by passing the variable "file" as
                Shell file,vnNormalFo cus
                i am panic.

                any how i am happy that i could perform the entire hard disk check.
                Last edited by Killer42; May 19 '07, 07:47 AM. Reason: Add [/QUOTE] tag

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #9
                  Probably you only have the file name, and not the path, in your variable. I'd suggest you put a breakpoint on the Shell line, and examine the contents of the variable at that point.

                  Comment

                  • ravindarjobs
                    New Member
                    • May 2007
                    • 86

                    #10
                    Originally posted by Killer42
                    Probably you only have the file name, and not the path, in your variable. I'd suggest you put a breakpoint on the Shell line, and examine the contents of the variable at that point.

                    i examined the content of the variable before executing the file.
                    it displayed the correct path with quotation marks as
                    "C:\Program Files\IFs\IFs.e xe"

                    but when i try to execute it, it is giving error

                    Comment

                    • Killer42
                      Recognized Expert Expert
                      • Oct 2006
                      • 8429

                      #11
                      Originally posted by ravindarjobs
                      i examined the content of the variable before executing the file.
                      it displayed the correct path with quotation marks as
                      "C:\Program Files\IFs\IFs.e xe"

                      but when i try to execute it, it is giving error
                      Hm, that is odd.

                      What happens if you just paste this command into the immediate window...

                      Shell "c:\Program Files\IFs\IFs.e xe"

                      Comment

                      • Killer42
                        Recognized Expert Expert
                        • Oct 2006
                        • 8429

                        #12
                        Here's another thought. Perhaps you need to change the current directory. So for example, you might try this in your code (hard-code it for now, just to try it out)...

                        ChDir "c:\Program Files\IFs"
                        Shell "c:\Program Files\IFs\IFs.e xe", vbNormalFocus

                        Comment

                        • ravindarjobs
                          New Member
                          • May 2007
                          • 86

                          #13
                          Originally posted by Killer42
                          Here's another thought. Perhaps you need to change the current directory. So for example, you might try this in your code (hard-code it for now, just to try it out)...

                          ChDir "c:\Program Files\IFs"
                          Shell "c:\Program Files\IFs\IFs.e xe", vbNormalFocus

                          thanq friend, it really worked well.
                          thanq every body.

                          also can we chage the directory dynamically?
                          i tried
                          ChDir " & st & "
                          and
                          ChDir st

                          but they didnt work.

                          any how my problem was solved.
                          thanq every body

                          Comment

                          • Killer42
                            Recognized Expert Expert
                            • Oct 2006
                            • 8429

                            #14
                            Originally posted by ravindarjobs
                            thanq friend, it really worked well.
                            thanq every body.
                            Excellent! Glad we could help. :)

                            As for the ChDir, certainly it can be done dynamically. It accepts a string argument. As is almost always the case in VB, this means you can use any expression which resolves to a string. For example, the following all produce the same result.

                            [CODE=vb]Dim strFullPath As String
                            Dim strPart1 As String, strPart2 As String
                            strFullPath = "C:\Temp\Te mp2"
                            strPart1 = "Temp"
                            strPart2 = "p2"
                            ' Version 1, a simple string literal...
                            ChDir "C:\Temp\Te mp2"
                            ' Version 2, a simple string variable...
                            ChDir strFullPath
                            ' Version 3, all sorts of things concatenated...
                            ChDir "C:\" & strPart1 & "\Tem" & strPart2[/CODE]

                            Comment

                            Working...