VBscript (.vbs) 代码获得 cleared/removed
VBsscript (.vbs) code gets cleared/removed
在我的大多数 VBscript(.vbs 文件)的顶部,我有以下代码:
Option Explicit
Const ForReading = 1
Dim objFSO, objFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("Z:\somepath\somefile.vbs", ForReading)
Execute objFile.ReadAll()
此代码允许我使用另一个 vbs 文件作为库。在这种情况下,somefile.vbs
将是我的库,并且定义了我所有的子程序和函数,这些子程序和函数是从脚本调用的,上面的代码是从中调用的(我称之为调用脚本)。
这个问题:每隔一段时间,其中一个脚本似乎会删除 Z:\somepath\somefile.vbs
中的代码(调用脚本读取的库脚本)。
我认为这是因为如果 wscript.exe
列在我的任务管理器进程选项卡中并且我从备份位置恢复 Z:\somepath\somefile.vbs
文件,几乎是立即,当我打开 Z:\somepath\somefile.vbs
同样,该文件中没有代码。但是,如果我终止 wscript.exe
进程,该文件就可以了。我无法重现该行为,因为它只会在我们的网络出现某种问题时发生(我认为)。
我第一个想到的是create
设置错误当我使用这条线时:
Set objFile = objFSO.OpenTextFile("Z:\somepath\somefile.vbs", ForReading)
但是根据这个link,默认的create
值应该是false:
https://msdn.microsoft.com/en-us/library/aa265347(v=vs.60).aspx
请注意,巧合的是,我也在文件 somefile.vbs
中使用 objFile
和 objFSO
变量来处理与我在调用脚本中所做的事情无关的事情。例如,somefile.vbs
文件中的 objFile
具有完全不同的名称和位置,并且是这样创建的:
Set objFile = objFSO.OpenTextFile("z:\differentpath\differentname.vbs", ForAppending, True)
我猜这就是问题所在,但我不明白。 有人可以解释一下吗? create
或 append
设置是否在调用脚本中重置?它是如何工作的?
不知道还能做什么,我将 somefile.vbs
文件中的变量名称更改为 oFSO, oFile
,而在调用脚本中它们仍然是 objFSO, objFile
。我还更改了调用脚本中的代码行,将 false
用于 create
设置,如下所示:
Set objFile = objFSO.OpenTextFile("Z:\somepath\somefile.vbs", ForReading,false)
学习自https://ss64.com/vb/execute.html
Execute takes a group of statements and executes them in local scope, ExecuteGlobal executes them in global scope.
However, if the same Execute statement is invoked outside of a procedure (i.e., in global scope), not only does it inherit everything in global scope, but it can also be called from anywhere, since its context is global.
如果将执行表单称为过程,问题是否仍然存在?
冒险(因为你只发布了部分代码)我假设你在阅读后没有明确关闭你的库脚本,所以你的主脚本保持文件打开直到它终止。在 Execute
语句后添加一行 objFile.Close
,或者(更好)更改
Set objFile = objFSO.OpenTextFile("Z:\somepath\somefile.vbs", ForReading)
Execute objFile.ReadAll()
到
code = objFSO.OpenTextFile("Z:\somepath\somefile.vbs").ReadAll
Execute code
或者只是
Execute objFSO.OpenTextFile("Z:\somepath\somefile.vbs").ReadAll
以便文件在读取后自动关闭。
在我的大多数 VBscript(.vbs 文件)的顶部,我有以下代码:
Option Explicit
Const ForReading = 1
Dim objFSO, objFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("Z:\somepath\somefile.vbs", ForReading)
Execute objFile.ReadAll()
此代码允许我使用另一个 vbs 文件作为库。在这种情况下,somefile.vbs
将是我的库,并且定义了我所有的子程序和函数,这些子程序和函数是从脚本调用的,上面的代码是从中调用的(我称之为调用脚本)。
这个问题:每隔一段时间,其中一个脚本似乎会删除 Z:\somepath\somefile.vbs
中的代码(调用脚本读取的库脚本)。
我认为这是因为如果 wscript.exe
列在我的任务管理器进程选项卡中并且我从备份位置恢复 Z:\somepath\somefile.vbs
文件,几乎是立即,当我打开 Z:\somepath\somefile.vbs
同样,该文件中没有代码。但是,如果我终止 wscript.exe
进程,该文件就可以了。我无法重现该行为,因为它只会在我们的网络出现某种问题时发生(我认为)。
我第一个想到的是create
设置错误当我使用这条线时:
Set objFile = objFSO.OpenTextFile("Z:\somepath\somefile.vbs", ForReading)
但是根据这个link,默认的create
值应该是false:
https://msdn.microsoft.com/en-us/library/aa265347(v=vs.60).aspx
请注意,巧合的是,我也在文件 somefile.vbs
中使用 objFile
和 objFSO
变量来处理与我在调用脚本中所做的事情无关的事情。例如,somefile.vbs
文件中的 objFile
具有完全不同的名称和位置,并且是这样创建的:
Set objFile = objFSO.OpenTextFile("z:\differentpath\differentname.vbs", ForAppending, True)
我猜这就是问题所在,但我不明白。 有人可以解释一下吗? create
或 append
设置是否在调用脚本中重置?它是如何工作的?
不知道还能做什么,我将 somefile.vbs
文件中的变量名称更改为 oFSO, oFile
,而在调用脚本中它们仍然是 objFSO, objFile
。我还更改了调用脚本中的代码行,将 false
用于 create
设置,如下所示:
Set objFile = objFSO.OpenTextFile("Z:\somepath\somefile.vbs", ForReading,false)
学习自https://ss64.com/vb/execute.html
Execute takes a group of statements and executes them in local scope, ExecuteGlobal executes them in global scope.
However, if the same Execute statement is invoked outside of a procedure (i.e., in global scope), not only does it inherit everything in global scope, but it can also be called from anywhere, since its context is global.
如果将执行表单称为过程,问题是否仍然存在?
冒险(因为你只发布了部分代码)我假设你在阅读后没有明确关闭你的库脚本,所以你的主脚本保持文件打开直到它终止。在 Execute
语句后添加一行 objFile.Close
,或者(更好)更改
Set objFile = objFSO.OpenTextFile("Z:\somepath\somefile.vbs", ForReading)
Execute objFile.ReadAll()
到
code = objFSO.OpenTextFile("Z:\somepath\somefile.vbs").ReadAll
Execute code
或者只是
Execute objFSO.OpenTextFile("Z:\somepath\somefile.vbs").ReadAll
以便文件在读取后自动关闭。