1

I am trying to implement a recursive search script with following usecase:-

  1. User can input string and directory to search for.
  2. Script will list all the files with path that matches point 1 (maybe in separate file).

I tried it with batch script and tried to run from html page to pass parameters (string and directory). It failed as mentioned over stackoverflow (due to javascripts inability to access file system.)

My batch script is :- findstr /s /i /n /C:@name= *.* v > results.txt

Now I am wondering if my requirement can be fulfilled with batch file or I need to switch to vbscript. Please suggest. I have no Idea of vbscript.

I can not install any third party tool on my windows workstation.

1 Answer 1

2

This is vbscript and won't trigger security dialogs if run from a local page.

On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")

Dirname = InputBox("Enter Dir name")
Searchterm = Inputbox("Enter search term")

ProcessFolder DirName

Sub ProcessFolder(FolderPath)

    Set fldr = fso.GetFolder(FolderPath)

    Set Fls = fldr.files
    For Each thing in Fls
        Set contents = thing.OpenAsTextStream 
        If Instr(contents.readall, searchterm) then wscript.echo thing.path
    Next

    Set fldrs = fldr.subfolders
    For Each thing in fldrs
'        wscript.echo thing.name
        ProcessFolder thing.path
    Next

End Sub

EDIT and EDIT2 (add browse for folder)

In an HTA (I had to start from scratch - I couldn't make your butchering of my script work).

<HTML> 
<HEAD><TITLE>Simple Validation</TITLE> 
<SCRIPT LANGUAGE="VBScript">
Dim Dirname
Dim Searchterm
Dim FSO
Dim objOutFile

Sub Browse
    On Error Resume Next
    Set bffShell = CreateObject("Shell.Application")
    Set bff = bffShell.BrowseForFolder(0, "Select the My Documents folder", 9)
    If Err.number<>0 Then
        MsgBox "Error Setting up Browse for Folder"

    Else
        A = bff.ParentFolder.ParseName(bff.Title).Path
        If err.number=424 then err.clear
            tb2.value = A
    End If
End Sub

Sub Search
    On Error Resume Next
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set objOutFile = fso.CreateTextFile("results.txt",True)
    Dirname = tb2.value
    Searchterm = tb1.value
    ProcessFolder DirName
End Sub

Sub ProcessFolder(FolderPath)
    On Error Resume Next
    Set fldr = fso.GetFolder(FolderPath)

    Set Fls = fldr.files
    For Each thing in Fls
        Set contents = thing.OpenAsTextStream
        If err.number = 0 then
            Test = Instr(contents.readall, searchterm) 
            If Isnull(test) = false then If Test > 0 then ObjOutFile.WriteLine thing.path
        Else
            err.clear
        End If
    Next

    Set fldrs = fldr.subfolders
    For Each thing in fldrs
        ProcessFolder thing.path
    Next

End Sub


</script>
</head>
<body>
<p><INPUT Name=tb1 TYPE=Text Value="Search">
<p><INPUT Name=tb2 TYPE=Text Value="Folder"> <INPUT NAME="Browse" TYPE="BUTTON" VALUE="Browse" OnClick=Browse>
<p><INPUT NAME="Search" TYPE="BUTTON" VALUE="Search" OnClick=Search>
</body>
</html>
Sign up to request clarification or add additional context in comments.

2 Comments

Still struggling with running this vbscript from a local html form passing path and string variables........ Any hint???

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.