2015-03-08 15 views
0

有什麼不對這個腳本使用了「currentDirectory所和」在路徑變量(.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所&」的用法是錯誤的,但不能糾正

回答

2

(1)爲了得到當前目錄,上使用.GetAbsolutePathName \或.CurrentDirectory:

>> WScript.Echo goFS.GetAbsolutePathName(".\") 
>> WScript.Echo CreateObject("WScript.Shell").CurrentDirectory 
>> 
E:\trials\SoTrials\answers\28892856\vbs 
E:\trials\SoTrials\answers\28892856\vbs 

(2)爲了得到腳本目錄,使用.GetParentFolderName上的WScript .Script全名:

>> 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」 WScript的。回聲 「頭&體&尾巴」

頭&體&尾巴'< ---字符串常量不變

級聯運算符&已被使用了文字的:

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

更新wrt tarkan的評論「really funn Ÿ我總是得到一個錯誤,可能會有人糾正代碼請「的最後一行:

爲了證明寧靜的代碼是不好笑:

>> WScript.Echo "Notepad " & Chr(34) & "c:\windows\win.ini" & Chr(34) 
>> 
Notepad "c:\windows\win.ini" 
+0

東西仍然丟失 Set WshShell = CreateObject(「WScript.She 「) Set objFSO = CreateObject(」Scripting.FileSystemObject「) currentDirectory = CreateObject(」WScript.Shell「)。CurrentDirectory WshShell.Run」C:\ Windows \ System32 \ diskpart.exe/s「&CurrentDirectory&」\ vhd .txt「,1,真 – 2015-03-08 22:50:07

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) 
+0

真的很有趣我總是收到一個錯誤,有人可能會糾正代碼的最後一行 – 2015-03-08 21:01:04

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) 

如果你想看到一個字符串,那麼它就是一個msgboxes。

+0

謝謝,但他們都沒有工作 – 2015-03-09 00:45:28

+0

僱用一個人。你顯然不知道編程或操作窗口。 – Serenity 2015-03-09 01:49:47

+0

所有的答案幫助了我,謝謝對於這樣一個簡單的腳本,我不需要僱用某人。我可以使用'精確路徑'(測試和工作),但與currentDirectory和變量「它不工作」 – 2015-03-09 09:13:19

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