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!!!

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?

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:

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"

A part from Comatose's above idea :

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.

I would do it something like this:

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

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.

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

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

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

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

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

If Dir("c:\quickrun.bat", vbNormal) <> "" Then Kill "c:\quickrun.bat"
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.

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:

File2Find = """somefile.txt"""

instead of ....

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!

commented: Absolutely Right. Double Quotes Is The Right Thing To Do. Great Call. +8
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.