在路径 (.Vbs) 中使用 "currentDirectory&" 变量

Usage the "currentDirectory&" variable in a path (.Vbs)

这个脚本有什么问题

Set WshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject") 
currentDirectory = left(WScript.ScriptFullName,(Len(WScript.ScriptFullName))-(len(WScript.ScriptName)))
WshShell.Run "C:\Windows\System32\diskpart.exe /s currentDirectory&"\vhd.txt", 1, True

我的意思是我知道 "currentDirectory&" 用法是错误的但无法更正它

引号将您键入的文本括起来。 & 将字符串连接在一起。包含字符串的变量不被引用。

要生成完整的字符串当前目录必须在引号之外。

x = "I'm a string" & Im_a_variable_containing_a_string & "I'm another string"

经常需要引用路径。双引号内引号或 Chr(34) 连接。

x = """C:\windows\win.ini""" 'or
x = "Notepad ""C:\windows\win.ini""" 'or

x = Chr(34) & "c:\windows\win.ini" & Chr(34)
x = "Notepad " & Chr(34) & "c:\windows\win.ini" & Chr(34)

(1) 要获取 当前目录 ,请在 .\ 或 .CurrentDirectory:

上使用 .GetAbsolutePathName
>> WScript.Echo goFS.GetAbsolutePathName(".\")
>> WScript.Echo CreateObject("WScript.Shell").CurrentDirectory
>>
E:\trials\SoTrials\answers892856\vbs
E:\trials\SoTrials\answers892856\vbs

(2) 要获取 脚本的目录 ,请在 WScript.ScriptFullName:

上使用 .GetParentFolderName
>> WScript.Echo goFS.GetParentFolderName(WScript.ScriptFullName)
>>
M:\bin

(3) 要从路径和文件名构建文件规范,请使用 .BuildPath:

>> WScript.Echo goFS.BuildPath("a\", "\b")
>>
a\b

比较
>> WScript.Echo left(WScript.ScriptFullName,(Len(WScript.ScriptFullName))-(len(WScript.ScriptName))) & "\vhd.txt"
>>
M:\bin\vhd.txt

发明你自己的 hack 会让你处于危险之中(哪个原生的或 - 更邪恶的 - 用户定义的 functions/subs 会(不)容忍 \?)没有任何好处。

(4) 不同于其他语言,例如Perl、VBScript 既不 interpolates/substitutes 将变量内容转换为字符串文字,也不计算其中的函数或运算符:

body = "BODY" WScript.Echo "head & body & tail"

head & body & tail ' <--- string literal unchanged

连接运算符 & 必须在文字之外使用:

>> body = "BODY"
>> WScript.Echo "head" & body & "tail"
>>
headBODYtail

更新 tarkan 的评论 "really funny I'm always getting an error could someone correct the last line of the code please":

证明Serenity的代码一点都不好笑:

>> WScript.Echo "Notepad " & Chr(34) & "c:\windows\win.ini" & Chr(34)
>>
Notepad "c:\windows\win.ini"
 "C:\Windows\System32\diskpart.exe /s " & CurrentDirectory & "\vhd.txt"

如果需要报价

"C:\Windows\System32\diskpart.exe /s " & """" & CurrentDirectory & "\vhd.txt"""

"C:\Windows\System32\diskpart.exe /s " & Chr(34) & CurrentDirectory & "\vhd.txt" & Chr(34)

如果一个人想看到一个字符串,那么就制作一个 msgboxes 吧。

这是我的最终脚本(有效)当然没有 currentDirectory& :),但我学到了关于 "currentDirectory" 的宝贵信息再次感谢大家

 If WScript.Arguments.length =0 Then
  Set objShell = CreateObject("Shell.Application")


  objShell.ShellExecute "wscript.exe", Chr(34) & _
  WScript.ScriptFullName & Chr(34) & " uac", "", "runas", 1
Else
'--------------
Dim objFSO, outFile
Set objFSO = CreateObject("Scripting.FileSystemObject") 
'Open write stream
Set outFile = objFSO.CreateTextFile("C:\vhd.txt", True)

'Write each command line
outFile.WriteLine "select vdisk file=d:\win_10.vhd"
outFile.WriteLine "attach vdisk"
outFile.WriteLine "select vdisk file=d:\win_8.1.vhd"
outFile.WriteLine "attach vdisk"

'Close write stream
outFile.Close

Set WshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject") 
WshShell.Run "C:\Windows\System32\diskpart.exe /s C:\vhd.txt", 1, True

Set obj = CreateObject("Scripting.FileSystemObject")
obj.DeleteFile("C:\vhd.txt")
'--------------
'End of UAC workaround code
End If

最后是当前目录:)

Set WshShell = CreateObject("WScript.Shell")
sCurPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
WshShell.Run "C:\Windows\System32\diskpart.exe /s " & sCurPath & "\vhd.txt""", 1, True