473,382 Members | 1,437 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,382 software developers and data experts.

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,vbNormalFocus

or Shell " & st & ",vbNormalFocus

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

can any body please help me
May 18 '07 #1
13 3036
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.exe"
To options come to mind (Assuming your string is strFile).
Dim objShell as Object
objShell = CreateObject ("WScript.Shell")
objShell.run(strFile)
OR
Dim objShell, objCmd
objShell = CreateObject("WScript.Shell")
objCmd = objShell.Exec(strFile)
May 18 '07 #2
Killer42
8,435 Expert 8TB
...
Shell st,vbNormalFocus
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.
May 18 '07 #3
Killer42
8,435 Expert 8TB
...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.
May 18 '07 #4
danp129
323 Expert 256MB
I got started good but trying to figure out if I want to put folder recursion in same sub or different one...

Expand|Select|Wrap|Line Numbers
  1. 'Constants needed if not using MS Scripting Runtime referece
  2. 'Const UnknownType=0
  3. 'Const Removable=1
  4. 'Const Fixed=2
  5. 'Const Remote=3
  6. 'Const CDRom=4
  7. 'Const RamDisk=5
  8. Option Explicit
  9. Private Sub Command1_Click()
  10.     'Call Search
  11.     Call SearchSystem(Fixed Or CDRom)
  12. End Sub
  13.  
  14. Sub SearchSystem(DriveTypes)
  15.     'Replace As with 'As if not using MS Scripting Runtime reference
  16.     Dim fs As FileSystemObject
  17.     Dim dc As Drives
  18.     Dim oDrive As Drive
  19.     Set fs = CreateObject("Scripting.FileSystemObject")
  20.     Set dc = fs.Drives
  21.  
  22.     Debug.Print "---"
  23.  
  24.     For Each oDrive In dc
  25.         'Use bitwise expression to see if Drivetype is one of the Drivetypes we wish to search
  26.         If (oDrive.DriveType And DriveTypes) = oDrive.DriveType Then
  27.             'oDrive is a drive type we wish to search
  28.             Debug.Print oDrive.DriveLetter & " - " & oDrive.DriveType
  29.         End If
  30.     Next 'oDrive
  31.     Set fs = Nothing
  32.     Set dc = Nothing
  33. End Sub
  34.  
May 18 '07 #5
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
May 18 '07 #6
danp129
323 Expert 256MB
Give this a try, You'll need to add the shell command yourself
Expand|Select|Wrap|Line Numbers
  1. 'Add MS Scripting Runtime reference
  2. 'Const UnknownType=0
  3. 'Const Removable=1
  4. 'Const Fixed=2
  5. 'Const Remote=3
  6. 'Const CDRom=4
  7. 'Const RamDisk=5
  8. Option Explicit
  9. Private Sub Command1_Click()
  10.     Dim mySearchPattern As String
  11.     mySearchPattern = "*.exe"
  12.     'look at constants above, use 'OR' + drive type to specify what type of drives to search.  I added OR CDRom below
  13.     Call SearchSystem(Fixed Or CDRom, mySearchPattern)
  14. End Sub
  15.  
  16. Sub SearchSystem(DriveTypes As Integer, _
  17.         FileSpec As String, _
  18.         Optional oFolder = Empty)
  19.     'Replace As with 'As if not using MS Scripting Runtime reference
  20.     Dim fs As FileSystemObject
  21.     Dim dc As Drives
  22.     Dim oDrive As Drive
  23.     Dim ofld As Folder
  24.     Dim file
  25.     Dim subdirs As New Collection
  26.     Dim subdir
  27.  
  28.     Set fs = CreateObject("Scripting.FileSystemObject")
  29.     Set dc = fs.Drives
  30.     On Error GoTo errorhandler:
  31.     If oFolder <> "" Then
  32.         file = Dir$(oFolder & FileSpec)
  33.         Do While Len(file)
  34.             'File found
  35.             file = oFolder & file
  36.  
  37. 'PUT YOUR CODE HERE TO EXECUTE FILE
  38. Debug.Print file
  39.  
  40.             'get next filename
  41.             file = Dir$()
  42.         Loop
  43.  
  44.         file = Dir$(oFolder & "*.*", vbDirectory)
  45.         Do While Len(file)
  46.             ' we've found a new directory
  47.             If file = "." Or file = ".." Then
  48.                 ' exclude "." and ".."
  49.             ElseIf (GetAttr(oFolder & file) And vbDirectory) = 0 Then
  50.                 ' ignore regular files
  51.             Else
  52.                 ' this is a directory, include the path in the collection
  53.                 subdirs.Add oFolder & file
  54.             End If
  55.             ' get next directory
  56.             file = Dir$()
  57.         Loop
  58.  
  59.         ' parse each subdirectory
  60.         For Each subdir In subdirs
  61.             Call SearchSystem(DriveTypes, FileSpec, subdir & "\")
  62.         Next
  63.     End If
  64.  
  65.     If oFolder = "" Then
  66.         For Each oDrive In dc
  67.             'Use bitwise expression to see if Drivetype is one of the Drivetypes we wish to search
  68.             If (oDrive.DriveType And DriveTypes) = oDrive.DriveType Then
  69.                 'oDrive is a drive type we wish to search
  70.                 Debug.Print "Searching: " & oDrive.Path
  71.                 If oDrive.IsReady Then Call SearchSystem(DriveTypes, FileSpec, oDrive.Path & "\")
  72.             End If
  73.         Next 'oDrive
  74.         Debug.Print "finished"
  75.     End If
  76.     Set fs = Nothing
  77.     Set dc = Nothing
  78. Exit Sub
  79. errorhandler:
  80.     If Err.Number = 75 Then
  81.         'Path/File access error (file locked for reading or doesn't exist?)
  82.         Debug.Print Err.Description
  83.         Resume Next
  84.     Else
  85.         Debug.Print Err.Description
  86.         MsgBox Err.Description & vbCrLf & "Hit CTRL+BREAK to debug or OK to quit."
  87.     End If
  88. End Sub
May 18 '07 #7
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,vbNormalFocus

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.exe

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

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\My 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,vnNormalFocus
i am panic.

any how i am happy that i could perform the entire hard disk check.
May 19 '07 #8
Killer42
8,435 Expert 8TB
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.
May 19 '07 #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.

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.exe"

but when i try to execute it, it is giving error
May 19 '07 #10
Killer42
8,435 Expert 8TB
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.exe"

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.exe"
May 19 '07 #11
Killer42
8,435 Expert 8TB
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.exe", vbNormalFocus
May 19 '07 #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.exe", 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
May 21 '07 #13
Killer42
8,435 Expert 8TB
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.

Expand|Select|Wrap|Line Numbers
  1. Dim strFullPath As String
  2. Dim strPart1 As String, strPart2 As String
  3. strFullPath = "C:\Temp\Temp2"
  4. strPart1 = "Temp"
  5. strPart2 = "p2"
  6. ' Version 1, a simple string literal...
  7. ChDir "C:\Temp\Temp2"
  8. ' Version 2, a simple string variable...
  9. ChDir strFullPath
  10. ' Version 3, all sorts of things concatenated...
  11. ChDir "C:\" & strPart1 & "\Tem" & strPart2
May 21 '07 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Abby Lee | last post by:
My code does what I want (works unless there is a lot of volume...works for this month cause not a lot of items marked paid yet...) but the page times out for last month because there is just so...
60
by: Julie | last post by:
What is the *fastest* way in .NET to search large on-disk text files (100+ MB) for a given string. The files are unindexed and unsorted, and for the purposes of my immediate requirements, can't...
0
by: Chung Leong | last post by:
Here's a short tutorial on how to the OLE-DB extension to access Windows Indexing Service. Impress your office-mates with a powerful full-text search feature on your intranet. It's easier than you...
3
by: Chung Leong | last post by:
Here's the rest of the tutorial I started earlier: Aside from text within a document, Indexing Service let you search on meta information stored in the files. For example, MusicArtist and...
13
by: Ray Muforosky | last post by:
Hello all: Task: I want to do file search, using the "conatining text" option from a web page. How do I search for a file on my local drive containing a certain string, from a web page. That...
2
by: Ola K | last post by:
Hi guys, I wrote a script that works *almost* perfectly, and this lack of perfection simply puzzles me. I simply cannot point the whys, so any help on it will be appreciated. I paste it all here,...
4
by: bb nicole | last post by:
BElow is my php code for candidate search, it have 4 criteria which is academic background, functional experience, preferred location and languages ability. but it show yhe blanks page.. Emm, is it...
4
by: ravindarjobs | last post by:
hi...... i am using ms access 2003,vb6 i have a form. in that i have 2 buttons 1. start search 2 stop search when i click the "start search" button the fucntion SearchSystem() is called,...
3
by: Sabine | last post by:
Hi there, I would need your help again. I am using Dev Avish's API function "Search for a file" http://www.mvps.org/access/api/api0006.htm to find a file on my computer which works well. As...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.