如果我从Mapped Network Drive输入Function,则无法从虚拟机获取绝对路径

时间:2015-01-22 00:39:41

标签: vbscript

方案: 我正在运行我的VBScript'测试完成'这是一个来自虚拟机的自动化测试工具。 我的脚本驻留在文件夹E:\TC from VM\TC中(在物理机中)。 我已将我的VM映射到此共享文件夹,因此从VM我的目录结构为:Z:\E\TC from VM\TC

如果我把代码放在这里 - 那么它会正确地显示路径 - 但是如果我将这段代码放在一个函数中并调用该函数它会显示一条不同的路径。为什么,以及我如何纠正它?

Dim fso, ShowAbsolutePath, objFile, GetParentDir
Dim strFile : strFile = "..\..\Variables.txt"

Set fso = CreateObject("Scripting.FileSystemObject")
ShowAbsolutePath = fso.GetAbsolutePathName(strFile)
Log.Message("The File is:"&ShowAbsolutePath)
GetParentDir = fso.GetParentFolderName(ShowAbsolutePath)
Log.Message("The parent directory of the File is:"&GetParentDir)
Set objFile = fso.GetFile(ShowAbsolutePath)

输出:

The File is:Z:\E\TC from VM\TC\Variables.txt
The parent directory of the File is:Z:\E\TC from VM\TC  

输出(如果我将此代码放入函数然后调用该函数):

The File is:Z:\E\TC from VM\TC\MMSDemo\MMSDemo
The parent directory of the File is:Z:\E\TC from VM\TC\MMSDemo

为何与众不同? 我只是将变量strFile传递给函数strFile = "..\..\Variables.txt"

1 个答案:

答案 0 :(得分:0)

.GetAbsolutePathName()将当前目录与作为参数给出的相对路径组合在一起,而不考虑文件或文件夹的存在:

>> WSCript.Echo goFS.GetAbsolutePathName("..\nosuchdir\nosuchfile.txt")
>>
E:\trials\SoTrials\answers\27749208\nosuchdir\nosuchfile.txt

因此给定的参数必须是结果的一部分。如果你得到

The File is:Z:\E\TC from VM\TC\MMSDemo\MMSDemo
The parent directory of the File is:Z:\E\TC from VM\TC\MMSDemo

然后.GetAbsolutePathName()使用" .... \ Variables.txt"来调用 ,但是使用空值/字符串。你使用" Option Explicit"?

相关问题