| | |
Search a particular file
Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
•
•
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.
I would do it something like this:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Dim File2Find As String Dim FileList() As String ReDim FileList(0) Dim wsh Set wsh = CreateObject("WScript.Shell") File2Find = "somefile.txt" Open "c:\quickrun.bat" For Output As #1 Print #1, "@echo off" Print #1, "cd \" Print #1, "dir /a/s/b " & File2Find & " >c:\found.dat" Close #1 wsh.run "c:\quickrun.bat", 0, 1 If Dir("c:\found.dat", vbNormal) = "" Then MsgBox "hmmm... something isn't right" Exit Sub End If Open "c:\found.dat" For Input As #1 Do Until EOF(1) Line Input #1, chk If chk <> vbNullString And chk <> vbNewLine Then If UBound(FileList()) > 0 Then ReDim Preserve FileList(UBound(FileList()) + 1) FileList(UBound(FileList())) = chk Else FileList(0) = chk End If End If Loop Close #1 If Dir("c:\quickrun.bat", vbNormal) <> "" Then Kill "c:\quickrun.bat" If Dir("c:\found.dat", vbNormal) <> "" Then Kill "c:\found.dat" For Each xFile In FileList() If xFile <> vbNullString Then MsgBox xFile End If Next xFile MsgBox "Done"
Last edited by Comatose; Jan 5th, 2009 at 12:12 am.
A part from Comatose's above idea :
Can u pls explain this part of ur program? It would be of very much help.
Thanks.
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Open "c:\found.dat" For Input As #1 Do Until EOF(1) Line Input #1, chk If chk <> vbNullString And chk <> vbNewLine Then If UBound(FileList()) > 0 Then ReDim Preserve FileList(UBound(FileList()) + 1) FileList(UBound(FileList())) = chk Else FileList(0) = chk End If End If Loop Close #1 If Dir("c:\quickrun.bat", vbNormal) <> "" Then Kill "c:\quickrun.bat" If Dir("c:\found.dat", vbNormal) <> "" Then Kill "c:\found.dat" For Each xFile In FileList() If xFile <> vbNullString Then MsgBox xFile End If Next xFile 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
•
•
•
•
I would do it something like this:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Dim File2Find As String Dim FileList() As String ReDim FileList(0) Dim wsh Set wsh = CreateObject("WScript.Shell") File2Find = "somefile.txt" Open "c:\quickrun.bat" For Output As #1 Print #1, "@echo off" Print #1, "cd \" Print #1, "dir /a/s/b " & File2Find & " >c:\found.dat" Close #1 wsh.run "c:\quickrun.bat", 0, 1 If Dir("c:\found.dat", vbNormal) = "" Then MsgBox "hmmm... something isn't right" Exit Sub End If Open "c:\found.dat" For Input As #1 Do Until EOF(1) Line Input #1, chk If chk <> vbNullString And chk <> vbNewLine Then If UBound(FileList()) > 0 Then ReDim Preserve FileList(UBound(FileList()) + 1) FileList(UBound(FileList())) = chk Else FileList(0) = chk End If End If Loop Close #1 If Dir("c:\quickrun.bat", vbNormal) <> "" Then Kill "c:\quickrun.bat" If Dir("c:\found.dat", vbNormal) <> "" Then Kill "c:\found.dat" For Each xFile In FileList() If xFile <> vbNullString Then MsgBox xFile End If Next xFile MsgBox "Done"
URGENT
Can anyone please explain this code????
OR
Can u provide me some other ideas????
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).
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.
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
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).
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.)
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
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).
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.
And that about wraps it up.
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).
vb Syntax (Toggle Plain Text)
Dim File2Find As String Dim FileList() As String 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.
vb Syntax (Toggle Plain Text)
Dim wsh Set wsh = CreateObject("WScript.Shell") 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... vb Syntax (Toggle Plain Text)
Open "c:\quickrun.bat" For Output As #1 Print #1, "@echo off" Print #1, "cd \" Print #1, "dir /a/s/b " & File2Find & " >c:\found.dat" 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).
vb Syntax (Toggle Plain Text)
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.)
vb Syntax (Toggle Plain Text)
If Dir("c:\found.dat", vbNormal) = "" Then MsgBox "hmmm... something isn't right" Exit Sub 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.). vb Syntax (Toggle Plain Text)
Open "c:\found.dat" For Input As #1 Do Until EOF(1) Line Input #1, chk If chk <> vbNullString And chk <> vbNewLine Then If UBound(FileList()) > 0 Then ReDim Preserve FileList(UBound(FileList()) + 1) FileList(UBound(FileList())) = chk Else FileList(0) = chk End If End If Loop 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).
vb Syntax (Toggle Plain Text)
If Dir("c:\quickrun.bat", vbNormal) <> "" Then Kill "c:\quickrun.bat" If Dir("c:\found.dat", vbNormal) <> "" Then Kill "c:\found.dat"
vb Syntax (Toggle Plain Text)
For Each xFile In FileList() If xFile <> vbNullString Then MsgBox xFile End If Next xFile
And that about wraps it up.
Thank you very much dear friend......!!
but I've an addition/suggestion to your idea:
instead of ....
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.
but I've an addition/suggestion to your idea:
vb Syntax (Toggle Plain Text)
File2Find = """somefile.txt"""
vb Syntax (Toggle Plain Text)
File2Find = "somefile.txt"
That sure did help!!!
Thank you for the help in coding explaining the code.
SOLVED!
![]() |
Similar Threads
- Invalid Boot.ini file booting from C:\windows (Windows NT / 2000 / XP)
- Search file for links (C++)
- Count number of occurrences of a character search it in a file (C++)
- home search asistent log!!! (Viruses, Spyware and other Nasties)
- Internet Explorer won't load, cannot open search (Viruses, Spyware and other Nasties)
- String search in a file compare? (Computer Science)
Other Threads in the Visual Basic 4 / 5 / 6 Forum
- Previous Thread: want to populate the listbox with msaccess database table data
- Next Thread: Showing result in vb6 to crystal rpeort 10's text box
| Thread Tools | Search this Thread |
* 6 429 2007 access activex add age append application basic beginner birth bmp calculator cd cells.find click client code college column component connection connectionproblemusingvb6usingoledb copy creat ctrl+f data database datareport date delete dissertations dissertationthesis dissertationtopic edit error excel excelmacro file filename form hardware header iamthwee image inboxinvb internetfiledownload keypress label listbox listview liveperson login looping machine microsoft movingranges number objectinsert open oracle password prime program prompt range-objects readfile reading record refresh remotesqlserverdatabase report retrieve save search sendbyte sites sort sql sql2008 sqlserver subroutine table tags textbox time timer urldownloadtofile vb vb6 vb6.0 vba visual visualbasic visualbasic6 web window windows






