Variables 使用「;当前目录&&引用;路径(.Vbs)中的变量

Variables 使用「;当前目录&&引用;路径(.Vbs)中的变量,variables,vbscript,Variables,Vbscript,这个脚本有什么问题 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\

这个脚本有什么问题

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)中的双引号集

(1) 要获取当前目录,请在上使用.GetAbsolutePathName。\or.CurrentDirectory:

>> WScript.Echo goFS.GetAbsolutePathName(".\")
>> WScript.Echo CreateObject("WScript.Shell").CurrentDirectory
>>
E:\trials\SoTrials\answers\28892856\vbs
E:\trials\SoTrials\answers\28892856\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
发明自己的黑客会让你处于危险之中(哪些本机或更邪恶的用户定义函数/子函数将(不)容忍
\\
?)而没有任何好处

(4) 与其他语言(如Perl)不同,VBScript既不将变量内容插入/替换为字符串文字,也不计算其中的函数或运算符:

body=“body” Echo“头部、身体和尾部”

头、身、尾' 如果需要报价

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

如果你想看到一个字符串,你可以制作一个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
最后是Currentdirectory:)


真的很有趣,我总是遇到一个错误。有人能纠正代码的最后一行吗?方法仍然缺失设置WshShell=CreateObject(“WScript.Shell”)设置objFSO=CreateObject(“Scripting.FileSystemObject”)currentDirectory=CreateObject(“WScript.Shell”)。currentDirectory WshShell。运行“C:\Windows\System32\diskpart.exe/s”&CurrentDirectory&“\vhd.txt”,1,trueemployee someone。你显然不知道如何编程或操作windows。所有的答案都帮助了我,谢谢你提供了这样一个简单的脚本,我不需要雇佣任何人。我可以只使用“精确路径”(已测试并已工作),但使用currentDirectory&variable“它不工作”
>> 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)
 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