Search a particular file

Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jun 2007
Posts: 68
Reputation: RahulV is an unknown quantity at this point 
Solved Threads: 0
RahulV's Avatar
RahulV RahulV is offline Offline
Junior Poster in Training

Search a particular file

 
0
  #1
Jan 1st, 2009
Hai,
Once again im here with a problem.
Can u help me write a program to search a file in the project folder.
I also want to use a progress bar to represent the search status.

Can anyone help!!!

Thank you!!!
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Search a particular file

 
0
  #2
Jan 1st, 2009
What do you mean search a file? Are you trying to make a program that the user can type in a search text, and it will find the file name? or will it find that phrase/word /INSIDE/ the file?
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 68
Reputation: RahulV is an unknown quantity at this point 
Solved Threads: 0
RahulV's Avatar
RahulV RahulV is offline Offline
Junior Poster in Training

Re: Search a particular file

 
0
  #3
Jan 4th, 2009
Originally Posted by Comatose View Post
What do you mean search a file? Are you trying to make a program that the user can type in a search text, and it will find the file name? or will it find that phrase/word /INSIDE/ the file?

Making it clearer --

Actually, the project should search for a file (name of the file is hard-coded) somewhere on the hard disk.
If found it should return a boolean value or something notifying that it has been found.

I hope i've made myself clear.
Please help.
Thank you.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Search a particular file

 
0
  #4
Jan 5th, 2009
I would do it something like this:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Dim File2Find As String
  2. Dim FileList() As String
  3. ReDim FileList(0)
  4. Dim wsh
  5. Set wsh = CreateObject("WScript.Shell")
  6. File2Find = "somefile.txt"
  7.  
  8. Open "c:\quickrun.bat" For Output As #1
  9. Print #1, "@echo off"
  10. Print #1, "cd \"
  11. Print #1, "dir /a/s/b " & File2Find & " >c:\found.dat"
  12. Close #1
  13.  
  14. wsh.run "c:\quickrun.bat", 0, 1
  15.  
  16. If Dir("c:\found.dat", vbNormal) = "" Then
  17. MsgBox "hmmm... something isn't right"
  18. Exit Sub
  19. End If
  20.  
  21.  
  22. Open "c:\found.dat" For Input As #1
  23. Do Until EOF(1)
  24. Line Input #1, chk
  25. If chk <> vbNullString And chk <> vbNewLine Then
  26. If UBound(FileList()) > 0 Then
  27. ReDim Preserve FileList(UBound(FileList()) + 1)
  28. FileList(UBound(FileList())) = chk
  29. Else
  30. FileList(0) = chk
  31. End If
  32. End If
  33. Loop
  34. Close #1
  35.  
  36.  
  37. If Dir("c:\quickrun.bat", vbNormal) <> "" Then Kill "c:\quickrun.bat"
  38. If Dir("c:\found.dat", vbNormal) <> "" Then Kill "c:\found.dat"
  39.  
  40. For Each xFile In FileList()
  41. If xFile <> vbNullString Then
  42. MsgBox xFile
  43. End If
  44. Next xFile
  45.  
  46. MsgBox "Done"
Last edited by Comatose; Jan 5th, 2009 at 12:12 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 68
Reputation: RahulV is an unknown quantity at this point 
Solved Threads: 0
RahulV's Avatar
RahulV RahulV is offline Offline
Junior Poster in Training

Re: Search a particular file

 
0
  #5
Jan 6th, 2009
A part from Comatose's above idea :

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Open "c:\found.dat" For Input As #1
  2. Do Until EOF(1)
  3. Line Input #1, chk
  4. If chk <> vbNullString And chk <> vbNewLine Then
  5. If UBound(FileList()) > 0 Then
  6. ReDim Preserve FileList(UBound(FileList()) + 1)
  7. FileList(UBound(FileList())) = chk
  8. Else
  9. FileList(0) = chk
  10. End If
  11. End If
  12. Loop
  13. Close #1
  14.  
  15.  
  16. If Dir("c:\quickrun.bat", vbNormal) <> "" Then Kill "c:\quickrun.bat"
  17. If Dir("c:\found.dat", vbNormal) <> "" Then Kill "c:\found.dat"
  18.  
  19. For Each xFile In FileList()
  20. If xFile <> vbNullString Then
  21. MsgBox xFile
  22. End If
  23. Next xFile
  24.  
  25. MsgBox "Done"

Can u pls explain this part of ur program? It would be of very much help.

Thanks.
Last edited by Ancient Dragon; Jan 6th, 2009 at 6:54 pm. Reason: replaced quote tags with code tags
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 68
Reputation: RahulV is an unknown quantity at this point 
Solved Threads: 0
RahulV's Avatar
RahulV RahulV is offline Offline
Junior Poster in Training

Re: Search a particular file

 
0
  #6
Jan 13th, 2009
Originally Posted by Comatose View Post
I would do it something like this:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Dim File2Find As String
  2. Dim FileList() As String
  3. ReDim FileList(0)
  4. Dim wsh
  5. Set wsh = CreateObject("WScript.Shell")
  6. File2Find = "somefile.txt"
  7.  
  8. Open "c:\quickrun.bat" For Output As #1
  9. Print #1, "@echo off"
  10. Print #1, "cd \"
  11. Print #1, "dir /a/s/b " & File2Find & " >c:\found.dat"
  12. Close #1
  13.  
  14. wsh.run "c:\quickrun.bat", 0, 1
  15.  
  16. If Dir("c:\found.dat", vbNormal) = "" Then
  17. MsgBox "hmmm... something isn't right"
  18. Exit Sub
  19. End If
  20.  
  21.  
  22. Open "c:\found.dat" For Input As #1
  23. Do Until EOF(1)
  24. Line Input #1, chk
  25. If chk <> vbNullString And chk <> vbNewLine Then
  26. If UBound(FileList()) > 0 Then
  27. ReDim Preserve FileList(UBound(FileList()) + 1)
  28. FileList(UBound(FileList())) = chk
  29. Else
  30. FileList(0) = chk
  31. End If
  32. End If
  33. Loop
  34. Close #1
  35.  
  36.  
  37. If Dir("c:\quickrun.bat", vbNormal) <> "" Then Kill "c:\quickrun.bat"
  38. If Dir("c:\found.dat", vbNormal) <> "" Then Kill "c:\found.dat"
  39.  
  40. For Each xFile In FileList()
  41. If xFile <> vbNullString Then
  42. MsgBox xFile
  43. End If
  44. Next xFile
  45.  
  46. MsgBox "Done"
URGENT

Can anyone please explain this code????
OR
Can u provide me some other ideas????
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Search a particular file

 
0
  #7
Jan 14th, 2009
Alright... Here is the break down.
This block of code, declares a string variable (File2Find) which will contain the name of the file we are looking to find on the system. Then, we declare a string array (filelist), but we don't mention how big to make it (since we have no idea how big we need it to be just yet). This allows us to change how big or small the array is at any time. The very next line does just that, redim filelist(0) sets the filelist array to have 1 element (arrays start at 0, not 1... if I put a 1 in there, there would be two items, zero and one).
  1. Dim File2Find As String
  2. Dim FileList() As String
  3. ReDim FileList(0)

These next three lines of code are fairly simple. I declare a variant type variable (called wsh). I then force wsh to become an object (a WScript.Shell object). Which creates an instance of "WScript.Shell." This gives me access to files and folders, and the ability to run commands/programs from within my app. Then, we set the File2Find string variable, to "somefile.txt". This can be any file that we are looking for.
  1. Dim wsh
  2. Set wsh = CreateObject("WScript.Shell")
  3. File2Find = "somefile.txt"

Next, we open a file for output (for writing). This is a batch file, however..... which is sort of like a DOS Script file. We write 3 lines to this batch file... the first line is fairly irrelevant, but is sort of a tradition now for batch file writers. Basically, it stops the batch file from echoing every command to the screen. Since we end up hiding that window anyway, this line is not needed, but it's good form (I'm talking about @echo off). The next line writes the dos command cd \ to the batch file. That would take the batch file to the root directory of the C: Drive. Then, we do Print #1, "dir /a/s/b " & File2Find & " >c:\found.dat". What this does, is it searches all directories and sub-directories (and sub-sub directories, etc) showing 1 item per line, and without any unneeded garbage information... only the path and file name. The >c:\found.dat is called "command line redirection". What that means is, the output from the dir command will not actually go to the screen at all. Instead, it will be put into a file called "found.dat". Then we close the file, which saves it. At this point, the batch file has not run yet.... it is simply a file on the C: drive. With the lines in it...
  1. Open "c:\quickrun.bat" For Output As #1
  2. Print #1, "@echo off"
  3. Print #1, "cd \"
  4. Print #1, "dir /a/s/b " & File2Find & " >c:\found.dat"
  5. Close #1

This next line, actually runs the batch file. So first, it turns off echo, then it changes to c:\, then it basically requests a list of every file on the computer, one entry per line. Then it puts that information into the file "c:\found.dat". wsh.run actually runs the batch file. The 0, means don't show the DOS prompt window, just do this hidden. The 1, means wait (do not continue processing my code) until the batch file has finished running (and hence, finished getting a list of every file and folder on the C: drive that matches our file2find So, if we are looking file mydoc.doc, then it would return every document found with the name mydoc.doc... regardless of where it is).
  1. wsh.run "c:\quickrun.bat", 0, 1

This chunk of the program, basically makes sure that the batch file worked (by checking if the file that the batch file makes exists... if it does not, then something is wrong with the batch file... we should pretty much always have this file here.)
  1. If Dir("c:\found.dat", vbNormal) = "" Then
  2. MsgBox "hmmm... something isn't right"
  3. Exit Sub
  4. End If

Since we know the file was found (see above) we open the file so that we can read it's contents (input). Then we loop through the entire file (until we reach end of file (EOF). We read in each line of the file, and we check to make sure that the line is NOT blank or just the enter key. Then we check the size of the FileList Array (the variable we use to contain a list of all the found copies of the searched for file). If the size of the FileList array is 0, then this is the first line of the file, we don't need to change the size of the array. Just assign it (after the else). If it is greater than 0, then we need to make sure to resize the array for each new line we read in, but we can't lose the data already put into the array. That's what This code ReDim Preserve FileList(UBound(FileList()) + 1) does. Next, we assign the newly created array item to the line of the file we just read in (which is actually a path to the file you were searching for.).
  1. Open "c:\found.dat" For Input As #1
  2. Do Until EOF(1)
  3. Line Input #1, chk
  4. If chk <> vbNullString And chk <> vbNewLine Then
  5. If UBound(FileList()) > 0 Then
  6. ReDim Preserve FileList(UBound(FileList()) + 1)
  7. FileList(UBound(FileList())) = chk
  8. Else
  9. FileList(0) = chk
  10. End If
  11. End If
  12. Loop
  13. Close #1

These two lines are used to clean up our mess.... we made a batch file, and need to delete it. The batch file made some kind file (a file that contains the redirected output of our dir command... essentially, a list of file names that match our search). Since we made a mess by adding these files to the system, the right thing to do is to delete them. If we try to delete them, however, and the file is not there... the program acts stupid. That's why we test to see if the file is there first, and then if it is, we delete it (with kill).
  1. If Dir("c:\quickrun.bat", vbNormal) <> "" Then Kill "c:\quickrun.bat"
  2. If Dir("c:\found.dat", vbNormal) <> "" Then Kill "c:\found.dat"
Then, we basically loop through all of the found filenames, and make sure they aren't blank lines (again). Then we simply display the path and filename to the searched item in a msgbox.
  1. For Each xFile In FileList()
  2. If xFile <> vbNullString Then
  3. MsgBox xFile
  4. End If
  5. Next xFile

And that about wraps it up.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 68
Reputation: RahulV is an unknown quantity at this point 
Solved Threads: 0
RahulV's Avatar
RahulV RahulV is offline Offline
Junior Poster in Training

Re: Search a particular file

 
1
  #8
Jan 15th, 2009
Thank you very much dear friend......!!
but I've an addition/suggestion to your idea:
  1. File2Find = """somefile.txt"""
instead of ....
  1. File2Find = "somefile.txt"
doing this change will help if the file name consists of spaces in between (i.e. if file name consists of multiple words). Like if file name is "some file.txt".

That sure did help!!!
Thank you for the help in coding explaining the code.

SOLVED!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC