VBScript 似乎没有办法包含一个通用的函数文件。
有没有办法做到这一点?
您可以在要包含其他文件的每个文件中创建一个(相对)小函数,如下所示:
sub includeFile (fSpec)
dim fileSys, file, fileData
set fileSys = createObject ("Scripting.FileSystemObject")
set file = fileSys.openTextFile (fSpec)
fileData = file.readAll ()
file.close
executeGlobal fileData
set file = nothing
set fileSys = nothing
end sub
然后用它来包含特定的文件——这些文件就像它们是内联的一样被执行。
includeFile "commonapi.vbi"
includeFile "dbcalls.vbi"
它基本上打开文件,将整个内容读入一个字符串,然后执行该字符串。I/O 调用没有错误处理,因为这类事情通常在程序启动时完成一次,如果出现问题,您希望失败。
请注意,该includeFile
函数可以压缩为:
Sub includeFile(fSpec)
With CreateObject("Scripting.FileSystemObject")
executeGlobal .openTextFile(fSpec).readAll()
End With
End Sub
甚至是(如果您不反对排长队):
Sub includeFile(fSpec)
executeGlobal CreateObject("Scripting.FileSystemObject").openTextFile(fSpec).readAll()
End Sub
“Windows Script Host”框架(如果你想这样称呼它)提供了一个 XML 包装文档,它在常规 vbs 文件上添加了功能。其中之一是能够包含 VBscript 和 Jscript 风格的外部脚本文件。我从来没有深入了解它,但我认为它会做你想做的事。 http://msdn.microsoft.com/en-us/library/15x4407c(VS.85).aspx
您可以包含 JavaScript、VBScript 或其他 WScript 脚本语言的模块。
示例 WSF 文件:
<job id="IncludeExample">
<script language="JavaScript" src="sprintf.js"/>
<script language="VBScript" src="logging.vbs"/>
<script language="VBScript" src="iis-queryScriptMaps.vbs"/>
</job>
如果上述文件名为“iis-scriptmaps.wsf”,则使用 cscript.exe 以这种方式运行它:
cscript.exe iis-scriptmaps.wsf
我知道这是一个旧线程,但无论如何我都会发布我的答案,以便其他人可以通过“反复试验”了解我对 VBS 和 WSF 文件的了解:
因此,要拥有与其他语言相同的功能,您可以创建一个 WSF 文件并在其中包含所有 VBS 库,包括主程序。
像这样的东西:
<job id="MainProg">
<script language="VBScript" src="Constants.vbs"/>
<script language="VBScript" src="FileFunctions.vbs"/>
<script language="VBScript" src="SendMail.vbs"/>
<script language="VBScript" src="LoggingFunctions.vbs"/>
<script language="VBScript" src="MainProgram.vbs"/>
<script language="VBScript">
' Here we call the main program
MainProgram()
</script>
</job>
在Constants.vbs
收集您以后要使用的所有常量并在其他 VBS 文件中定义您的函数。在您的主程序文件MainProgram.vbs
中,创建一个sub
被调用MainProgram()
并在那里编写您的程序。在这个子例程中,您可以使用在其他 VBS 文件中定义的所有常量和函数。
例如 :
sub MainProgram()
' Local variables
Dim strMessage, strSendTo, strSubject
' OpenFile is a function from FileFunctions.vbs
strMessage = OpenFile("C:\Msg\message.html")
strSendTo = "email.address@yourdomain.com"
strSubject = "Daily report - " & date
' SendMessage is a function from SendMail.vbs
' cFrom and cServer are constants from Constants.vbs
SendMessage(cFrom, strSendTo, strSubject, strMessage, cServer)
' Logger is a function from LoggingFunctions.vbs
Logger("Daily report sent - " & now())
end sub
希望你明白我的想法,我可以帮助一些人编写更好的 VBS 应用程序 :)
在接受的答案的基础上,这是一个包含子过程,它采用相对于脚本位置而不是工作目录的路径:
Sub include( relativeFilePath )
Set fso = CreateObject("Scripting.FileSystemObject")
thisFolder = fso.GetParentFolderName( WScript.ScriptFullName )
absFilePath = fso.BuildPath( thisFolder, relativeFilePath )
executeGlobal fso.openTextFile( absFilePath ).readAll()
End Sub
请注意,您还可以在路径中使用.
和..
部分来将文件包含在父文件夹等中,并且从哪里启动脚本并不重要。例子:
include "..\Lib\StringUtilities.vbs"
这个 VBScript 是在本地使用,还是提供经典的 ASP 风格?
如果它是经典的 ASP,你可以使用 SSI 来做它:
<!-- #include virtual="/PathTo/MyFile.vbs" -->
您可以使用 ExecuteGlobal 函数在全局命名空间中运行任意 VBS 代码。一个例子可以在这里找到:http: //www.source-code.biz/snippets/vbscript/5.htm
IIS 5 及更高版本还允许使用script
标记来包含 ASP 文件中的其他文件。(您的 VBScript 是 ASP 页面还是 Windows 脚本?)这是一个示例:
<script language="VBScript" runat="server" src="include.asp"></script>
行为和规则与服务器端包含有点不同。注意:我从未真正尝试过使用经典 ASP 中的这种语法。
您绝对可以在 cscript 中使用 WSF 脚本标记:
<script language="VBScript" src="ADOVBS.INC"/>
如果您使用 ADOVBS.inc 进行 ADODB 访问,请确保删除
<% %>
来自 ADOVBS.INC 的标签。