Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello the community,

I'm a beginner in devellopement.
I try to perform a basic web search engine for files on a files server.

1. The first step is to search file by name (if a file name contain the researched string it add it to the list box).

My script is working for one folder but it doesn't go to the subfolders.
If someone can help me, it will be fantastic

VB
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    ListBox1.Items.Clear()

    Dim fso, count, src, folder, file
    Dim stringtofind

    fso = CreateObject("Scripting.FileSystemObject")
    src = "D:\DOCUMENTS_MEDECINS\"
    stringtofind = TextBox1.Text
    folder = fso.GetFolder(src)
    count = 0

    For Each file In folder.files
        If InStr(LCase(file.name), LCase(stringtofind)) > 0 Then
            ListBox1.Items.Add(file.name)
        End If
    Next

End Sub


2. The second step ... display the found files in a datagrid and when you click on the file it open it ! But for the moment it's just a dream.

Thanks for all the help you can bring me.
Posted
Updated 6-Oct-15 2:45am
v2

1 solution

You're using ASP.NET, so there's no need to use the ancient and obsolete "Scripting.FileSystemObject". The .NET Framework has plenty of built-in classes for working with the file system, mostly located in the System.IO namespace[^].

For example, to find all files in all sub-folders of a given folder:
VB.NET
Dim stringToFind As String = TextBox1.Text
Dim sourcePath As New DirectoryInfo("D:\DOCUMENTS_MEDECINS\")

For Each file As FileInfo In sourcePath.EnumerateFiles("*", SearchOption.AllDirectories)
    If file.Name.IndexOf(stringToFind, StringComparison.OrdinalIgnoreCase) <> -1 Then
        ...
    End If
Next

DirectoryInfo[^] | FileInfo[^] | EnumerateFiles[^] | String.IndexOf[^]

Using hard-coded local server paths in an ASP.NET application is not a good idea. Instead, you should add the directory to your site as a "virtual directory", and then use the Server.MapPath method[^] to convert the virtual path (eg: "~/DOCUMENTS_MEDECINS/") to the physical path. That way, if you need to move the folder to a different location, you only need to update the mapping in IIS; you don't need to go through your code and change all the paths.

Once you have the folder mapped as a virtual directory, linking to the file is just a case of converting the physical path to a virtual path. There's no built-in way to do that, but it's not too hard:
VB.NET
Dim virtualPath As String = "~/DOCUMENTS_MEDECINS/"
Dim stringToFind As String = TextBox1.Text

Dim sourcePath As New DirectoryInfo(Server.MapPath(virtualPath))
Dim sourceUri As New Uri(sourcePath.FullName)
 
For Each file As FileInfo In sourcePath.EnumerateFiles("*", SearchOption.AllDirectories)
    If file.Name.IndexOf(stringToFind, StringComparison.OrdinalIgnoreCase) <> -1 Then
        Dim fileUri As New Uri(file.FullName)
        Dim relativeUri As Uri = sourceUri.MakeRelativeUri(fileUri)
        Dim filePath As String = virtualPath & relativeUri
        ListBox1.Items.Add(filePath)
    End If
Next

Server.MapPath[^] | Uri[^] | Uri.MakeRelativeUri[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900