我正在尝试创建一个VBScript代码,该代码可以接受用户输入(字符串,目录)并在目录中搜索字符串。
我的.vbs文件在这里:
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
这是合理的。但是,当我尝试将其包装在HTA文件中时,我无法弄清楚如何传递表单值,然后在HTML页面上显示结果。
这是我尝试创建.hta文件的方式:
<HTML>
<HEAD><TITLE>Simple Validation</TITLE>
<SCRIPT LANGUAGE="VBScript">
Sub Submit_OnClick
Dim TheForm
Set TheForm = Document.ValidForm
Dim FileName
On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
strTemp = "result.txt"
Dirname = TheForm.Text1.Value
Searchterm = TheForm.Text2.Value
Set objOutFile = objFS.CreateTextFile(strTemp,True)
ProcessFolder DirName
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 Set FileName = thing.Path"
Next
Set fldrs = fldr.subfolders
For Each thing In fldrs
' WScript.Echo thing.Name
ProcessFolder thing.Path
Next
End Sub
</SCRIPT>
</HEAD>
<BODY>
<H3>Simple Validation</H3><HR>
<FORM NAME="ValidForm" align = "center">
Enter String to Serach:
<INPUT NAME="Text1" TYPE="TEXT">
</br>
</br>
Enter Directory:
<INPUT NAME="Text2" TYPE="TEXT">
</br>
</br>
<INPUT NAME="Submit" TYPE="BUTTON" VALUE="Submit">
</FORM>
<p id = "demo"></p>
</BODY>
<script LANGUAGE = "javascript">
//document.getElementById('demo').innerHTML = FileName;
</script>
</HTML>
也许我可以将结果显示在列表中或将其存储在文本文件中并访问以显示。
最佳答案
您的HTA从根本上被破坏了。
<HTA:APPLICATION ID="oHTA" APPLICATIONNAME="Test" ...>
Sub Submit_OnClick
)。在其中调用过程ProcessFolder
(两次),但实际上从未在任何地方定义它。您需要将该过程定义放在Submit_OnClick
的定义之前或之后,否则将无法调用它:Sub Submit_OnClick
...
End Sub
Sub ProcessFolder(FolderPath)
...
End Sub
If InStr(contents.ReadAll, searchterm) Then Set FileName = thing.Path"
Set
语句将字符串(路径)分配给变量。该关键字是保留的,用于将对象分配给变量,在分配原始数据类型时必须将其省略。 WScript.Echo
在HTA中不起作用,因为HTA脚本宿主不提供WScript
对象。如果需要显示消息,请改用 MsgBox
函数。 objOutFile
时,您使用objFS
,但是实际的FileSystemObject
实例变量是fso
。 demo.innerHtml = demo.innerHtml & thing.Path & "<br>"
关于windows - 在目录中搜索字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35575601/