此脚本有什么问题
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&"用法错误但无法纠正
答案 0 :(得分:2)
(1)要获取当前目录,请在。\或.CurrentDirectory上使用.GetAbsolutePathName:
>> 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” WScript.Echo“head& body& tail”
head&身体和tail'< --- string literal unchanged
串联运算符&必须使用文字:
>> body = "BODY"
>> WScript.Echo "head" & body & "tail"
>>
headBODYtail
更新wrt tarkan的评论“真的很有趣我总是会收到错误,有人可以更正代码的最后一行”:
要证明Serenity的代码根本不好笑:
>> WScript.Echo "Notepad " & Chr(34) & "c:\windows\win.ini" & Chr(34)
>>
Notepad "c:\windows\win.ini"
答案 1 :(得分:0)
引号括起您键入的文字。 &安培;将字符串连接在一起。不引用包含字符串的变量。
制作完整字符串当前目录必须在引号之外。
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)
答案 2 :(得分:0)
"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)
如果有人想看一个字符串,那就是制作一个msgbox。
答案 3 :(得分:0)
这是我的最终剧本(作品)当然没有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:)
Set WshShell = CreateObject("WScript.Shell")
sCurPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
WshShell.Run "C:\Windows\System32\diskpart.exe /s " & sCurPath & "\vhd.txt""", 1, True