如何在VBScript中包含公共文件(类似于C #include)?

时间:2021-06-23 05:54:27

VBScript doesn't appear to have a way to include a common file of functions.

VBScript似乎没有办法包含一个公共函数文件。

Is there a way to achieve this?

有没有办法实现这个目标?

8 个解决方案

#1


43  

The "Windows Script Host" framework (if ya want to call it that), offers an XML wrapper document that adds functionality over regular vbs files. One of which is the ability to include external script files of both the VBscript and Jscript flavors. I never got very deep into it, but I think it would do what you're wanting to do. http://msdn.microsoft.com/en-us/library/15x4407c(VS.85).aspx

“Windows脚本宿主”框架(如果你想称之为),提供了一个XML包装器文档,它可以增加常规vbs文件的功能。其中之一是能够包含VBscript和Jscript风格的外部脚本文件。我从来没有深入到它,但我认为它会做你想做的事情。 http://msdn.microsoft.com/en-us/library/15x4407c(VS.85).aspx

You can include JavaScript, VBScript, or modules of other WScript script languages.

您可以包含JavaScript,VBScript或其他WScript脚本语言的模块。

Example WSF file:

示例WSF文件:

<job id="IncludeExample">
   <script language="JavaScript" src="sprintf.js"/>
   <script language="VBScript" src="logging.vbs"/>
   <script language="VBScript" src="iis-queryScriptMaps.vbs"/>
</job>

If the above file is called "iis-scriptmaps.wsf", run it this way with cscript.exe:

如果上面的文件名为“iis-scriptmaps.wsf”,请使用cscript.exe以这种方式运行:

cscript.exe  iis-scriptmaps.wsf

#2


52  

You can create a (relatively) small function in each file that you want to include other files into, as follows:

您可以在要包含其他文件的每个文件中创建(相对)小函数,如下所示:

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

and then use it to include specific files - these are executed as if they were inline.

然后使用它来包含特定文件 - 这些文件就像它们是内联的一样执行。

includeFile "commonapi.vbi"
includeFile "dbcalls.vbi"

It basically opens the file, reads the entire contents into a string, then executes that string. There's no error handling on the I/O calls since this sort of stuff is usually done once on program start, and you want to fail if there's a problem including it.

它基本上打开文件,将整个内容读入一个字符串,然后执行该字符串。 I / O调用没有错误处理,因为这种东西通常在程序启动时完成一次,如果出现包含它的问题,你想要失败。


Note that the includeFile function can be compressed to:

请注意,includeFile函数可以压缩为:

Sub includeFile(fSpec)
    With CreateObject("Scripting.FileSystemObject")
       executeGlobal .openTextFile(fSpec).readAll()
    End With
End Sub

Or even to (if you're not adverse to long lines):

或者甚至(如果你不对长线不利):

Sub includeFile(fSpec)
    executeGlobal CreateObject("Scripting.FileSystemObject").openTextFile(fSpec).readAll()
End Sub

#3


15  

I know this is an old thread but I post my answer anyway so others can learn what I have learnt about VBS and WSF files by "trial and error" :

我知道这是一个老线程,但无论如何我发布了我的答案,所以其他人可以通过“反复试验”了解我对VBS和WSF文件的了解:

So to have the same functionality as in other languages you can create one WSF file and include all of your VBS libs there, including the main program.

因此,要获得与其他语言相同的功能,您可以创建一个WSF文件并在其中包含所有VBS库,包括主程序。

Something like this :

像这样的东西:

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

In Constants.vbs collect all constants you want to use later and in the other VBS files define your functions. In your main program file MainProgram.vbs, create a sub called MainProgram() and write your program there. In this subroutine, you can use all of the constants and functions defined in the other VBS files.

在Constants.vbs中,收集以后要使用的所有常量,并在其他VBS文件中定义您的函数。在主程序文件MainProgram.vbs中,创建一个名为MainProgram()的子程序并在那里编写程序。在此子例程中,您可以使用其他VBS文件中定义的所有常量和函数。

For example :

例如 :

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

Hope you get the idea and I could help some people write better VBS apps :)

希望你能得到这个想法,我可以帮助一些人写出更好的VBS应用:)

#4


5  

Building on the accepted answer, here's an include sub procedure which takes a path relative to the script location instead of the working directory:

在接受的答案的基础上,这是一个include子过程,它采用相对于脚本位置而不是工作目录的路径:

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

Note the you can additionally use . and .. parts in your path to include files in parent folders, etc. and it will not matter where you launch the script from. Example:

请注意,您可以另外使用。和路径中的部分包含父文件夹中的文件等,从哪里启动脚本无关紧要。例:

include "..\Lib\StringUtilities.vbs"

#5


1  

Is this VBScript being used locally, or served classic ASP style?

这个VBScript是在本地使用,还是经典的ASP风格?

If its classic ASP, you can use SSI todo it:

如果它的经典ASP,你可以使用SSI todo它:

<!-- #include virtual="/PathTo/MyFile.vbs" -->

#6


1  

You can use the ExecuteGlobal function to run arbitrary VBS code in the global namespace. An example can be found here : http://www.source-code.biz/snippets/vbscript/5.htm

您可以使用ExecuteGlobal函数在全局命名空间中运行任意VBS代码。可以在这里找到一个例子:http://www.source-code.biz/snippets/vbscript/5.htm

#7


0  

IIS 5 and up also allow a script tag for including other files from an ASP file. (Is your VBScript an ASP page or a Windows script?) Here's an example:

IIS 5及更高版本还允许脚本标记包含ASP文件中的其他文件。 (您的VBScript是ASP页面还是Windows脚本?)以下是一个示例:

<script language="VBScript" runat="server" src="include.asp"></script>

The behavior and rules are a bit different from server-side includes. Note: I have never actually tried using this syntax from classic ASP.

行为和规则与服务器端包含有点不同。注意:我从未尝试过使用经典ASP中的这种语法。

#8


0  

you can definately use the WSF script tag in cscript:

您可以在cscript中明确使用WSF脚本标记:

<script language="VBScript" src="ADOVBS.INC"/>

If you use ADOVBS.inc for an ADODB access make sure to remove the

如果您使用ADOVBS.inc进行ADODB访问,请确保删除

<% %>

tags from ADOVBS.INC.

来自ADOVBS.INC的标签。

#1


43  

The "Windows Script Host" framework (if ya want to call it that), offers an XML wrapper document that adds functionality over regular vbs files. One of which is the ability to include external script files of both the VBscript and Jscript flavors. I never got very deep into it, but I think it would do what you're wanting to do. http://msdn.microsoft.com/en-us/library/15x4407c(VS.85).aspx

“Windows脚本宿主”框架(如果你想称之为),提供了一个XML包装器文档,它可以增加常规vbs文件的功能。其中之一是能够包含VBscript和Jscript风格的外部脚本文件。我从来没有深入到它,但我认为它会做你想做的事情。 http://msdn.microsoft.com/en-us/library/15x4407c(VS.85).aspx

You can include JavaScript, VBScript, or modules of other WScript script languages.

您可以包含JavaScript,VBScript或其他WScript脚本语言的模块。

Example WSF file:

示例WSF文件:

<job id="IncludeExample">
   <script language="JavaScript" src="sprintf.js"/>
   <script language="VBScript" src="logging.vbs"/>
   <script language="VBScript" src="iis-queryScriptMaps.vbs"/>
</job>

If the above file is called "iis-scriptmaps.wsf", run it this way with cscript.exe:

如果上面的文件名为“iis-scriptmaps.wsf”,请使用cscript.exe以这种方式运行:

cscript.exe  iis-scriptmaps.wsf

#2


52  

You can create a (relatively) small function in each file that you want to include other files into, as follows:

您可以在要包含其他文件的每个文件中创建(相对)小函数,如下所示:

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

and then use it to include specific files - these are executed as if they were inline.

然后使用它来包含特定文件 - 这些文件就像它们是内联的一样执行。

includeFile "commonapi.vbi"
includeFile "dbcalls.vbi"

It basically opens the file, reads the entire contents into a string, then executes that string. There's no error handling on the I/O calls since this sort of stuff is usually done once on program start, and you want to fail if there's a problem including it.

它基本上打开文件,将整个内容读入一个字符串,然后执行该字符串。 I / O调用没有错误处理,因为这种东西通常在程序启动时完成一次,如果出现包含它的问题,你想要失败。


Note that the includeFile function can be compressed to:

请注意,includeFile函数可以压缩为:

Sub includeFile(fSpec)
    With CreateObject("Scripting.FileSystemObject")
       executeGlobal .openTextFile(fSpec).readAll()
    End With
End Sub

Or even to (if you're not adverse to long lines):

或者甚至(如果你不对长线不利):

Sub includeFile(fSpec)
    executeGlobal CreateObject("Scripting.FileSystemObject").openTextFile(fSpec).readAll()
End Sub

#3


15  

I know this is an old thread but I post my answer anyway so others can learn what I have learnt about VBS and WSF files by "trial and error" :

我知道这是一个老线程,但无论如何我发布了我的答案,所以其他人可以通过“反复试验”了解我对VBS和WSF文件的了解:

So to have the same functionality as in other languages you can create one WSF file and include all of your VBS libs there, including the main program.

因此,要获得与其他语言相同的功能,您可以创建一个WSF文件并在其中包含所有VBS库,包括主程序。

Something like this :

像这样的东西:

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

In Constants.vbs collect all constants you want to use later and in the other VBS files define your functions. In your main program file MainProgram.vbs, create a sub called MainProgram() and write your program there. In this subroutine, you can use all of the constants and functions defined in the other VBS files.

在Constants.vbs中,收集以后要使用的所有常量,并在其他VBS文件中定义您的函数。在主程序文件MainProgram.vbs中,创建一个名为MainProgram()的子程序并在那里编写程序。在此子例程中,您可以使用其他VBS文件中定义的所有常量和函数。

For example :

例如 :

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

Hope you get the idea and I could help some people write better VBS apps :)

希望你能得到这个想法,我可以帮助一些人写出更好的VBS应用:)

#4


5  

Building on the accepted answer, here's an include sub procedure which takes a path relative to the script location instead of the working directory:

在接受的答案的基础上,这是一个include子过程,它采用相对于脚本位置而不是工作目录的路径:

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

Note the you can additionally use . and .. parts in your path to include files in parent folders, etc. and it will not matter where you launch the script from. Example:

请注意,您可以另外使用。和路径中的部分包含父文件夹中的文件等,从哪里启动脚本无关紧要。例:

include "..\Lib\StringUtilities.vbs"

#5


1  

Is this VBScript being used locally, or served classic ASP style?

这个VBScript是在本地使用,还是经典的ASP风格?

If its classic ASP, you can use SSI todo it:

如果它的经典ASP,你可以使用SSI todo它:

<!-- #include virtual="/PathTo/MyFile.vbs" -->

#6


1  

You can use the ExecuteGlobal function to run arbitrary VBS code in the global namespace. An example can be found here : http://www.source-code.biz/snippets/vbscript/5.htm

您可以使用ExecuteGlobal函数在全局命名空间中运行任意VBS代码。可以在这里找到一个例子:http://www.source-code.biz/snippets/vbscript/5.htm

#7


0  

IIS 5 and up also allow a script tag for including other files from an ASP file. (Is your VBScript an ASP page or a Windows script?) Here's an example:

IIS 5及更高版本还允许脚本标记包含ASP文件中的其他文件。 (您的VBScript是ASP页面还是Windows脚本?)以下是一个示例:

<script language="VBScript" runat="server" src="include.asp"></script>

The behavior and rules are a bit different from server-side includes. Note: I have never actually tried using this syntax from classic ASP.

行为和规则与服务器端包含有点不同。注意:我从未尝试过使用经典ASP中的这种语法。

#8


0  

you can definately use the WSF script tag in cscript:

您可以在cscript中明确使用WSF脚本标记:

<script language="VBScript" src="ADOVBS.INC"/>

If you use ADOVBS.inc for an ADODB access make sure to remove the

如果您使用ADOVBS.inc进行ADODB访问,请确保删除

<% %>

tags from ADOVBS.INC.

来自ADOVBS.INC的标签。