2014-06-25 51 views
0

我試圖從一個VBS到BAT傳遞一個變量,但我得到「系統找不到指定文件」傳遞參數變量爲.bat

這裏是我的vbs:

Option Explicit 

Dim strFile 

strFile = SelectFile() 

If strFile = "" Then 
    WScript.Echo "No file selected." 
Else 
    WScript.Echo """" & strFile & """" 
End If 


Function SelectFile() 

    Dim objExec, strMSHTA, wshShell 

    SelectFile = "" 


    strMSHTA = "mshta.exe ""about:" & "<" & "input type=file id=FILE>" _ 
      & "<" & "script>FILE.click();new ActiveXObject('Scripting.FileSystemObject')" _ 
      & ".GetStandardStream(1).WriteLine(FILE.value);close();resizeTo(0,0);" & "<" & "/script>""" 

    Set wshShell = CreateObject("WScript.Shell") 
    Set objExec = wshShell.Exec(strMSHTA) 

    SelectFile = objExec.StdOut.ReadLine() 


Dim wshShelll 
Set WshShelll = Wscript.CreateObject("WScript.Shell") 
WshShelll.Run "C:\Users\nbendjelida\Desktop\email.bat" & SelectFile 


    Set objExec = Nothing 
    Set wshShell = Nothing 
    Set wshShelll = Nothing 
End Function 

這裏是我的蝙蝠:

"C:\Program Files\Microsoft Office\Office12\Outlook.exe" /eml %1 

你有什麼想法?

+4

在'WshShelll.Run「的結尾用空格試試C:\ Users \ nbendjelida \ Desktop \ email.bat「' – SachaDee

+0

驚人的,那工作:)我一直堅持2天!謝謝 :) – Nazim

回答

0

我重複sachadee的正確答案和更多詳細信息,以便從未回答的問題列表中刪除此問題。

Run Method必須使用第一個參數作爲執行參數的命令來調用,這些參數與在命令行窗口中輸入命令時完全相同。引用的Microsoft幫助頁面上的示例在命令Notepad後面也有一個空格字符。

調用批處理文件與文件名作爲第一個參數所需的命令行是:

C:\Users\nbendjelida\Desktop\email.bat name_of_selected_file 

但Windows腳本宿主代碼行

WshShelll.Run "C:\Users\nbendjelida\Desktop\email.bat" & SelectFile 

建立的命令字符串以

C:\Users\nbendjelida\Desktop\email.bat name_of_selected_file 

由於缺少空格字符。

的問題的解決,是正確的Windows腳本宿主代碼行

WshShelll.Run "C:\Users\nbendjelida\Desktop\email.bat " & SelectFile 

因爲批處理文件的名稱和所選文件的名稱之間的空間系統字符的。


如果選擇的文件名中包含一個或多個空格,這是必要的,任何一個變量SelectFile已經包含了雙引號,在開始和結束處,或必要的雙引號是在串聯的命令字符串添加。

與整個批處理文件名還含有一個空格字符示例:

Dim FileName 
FileName = "%TEMP%\Any File.txt" 
Set WshShell = WScript.CreateObject("WScript.Shell") 
WshShell.Run """%USERPROFILE%\Desktop\My Batch File.bat"" """ & FileName & """" 

批處理文件My Batch File.bat在含有

@echo %0 %* 
@pause 

例如輸出當前用戶的桌面上的Windows 7

"C:\Users\username\Desktop\My Batch File.bat" "C:\User\username\AppData\Local\Temp\Any File.txt" 

或英文Windows XP

"C:\Documents and Settings\user name\Desktop\My Batch File.bat" "C:\Documents and Settings\user name\Local Settings\Temp\Any File.txt" 

哪些是命令字符串的預期結果。

(是的,一個用戶名可以包含空格字符雖然微軟建議不要使用用戶名空格字符,請參閱Microsoft頁Creating User and Group Accounts。)