2017-08-14 97 views
0

我有以下批處理腳本打開從中我需要的姓名和所選文件的路徑的文件選擇對話框(我的操作系統是Windows 7 64Bit):批處理腳本打開當前目錄或選擇使用PowerShell/vbscrpt命令的目錄

@echo off 
setlocal 

> "%temp%\fileSelectorDialog.vbs" (
    echo DIM objFile 
    echo Set objShell = CreateObject^("Shell.Application" ^) 
    echo Do 
    echo Set objFile = objShell.BrowseForFolder^(0,"Select a file",^&H4000,""^) 
    echo on error resume next 
    echo if objFile.Items.Item.Path = Null OR objFile is nothing OR err.number ^<^> 0 then 
    echo wscript.echo "ERROR" 
    echo wscript.quit 
    echo end if 
    echo wscript.echo objFile.ParentFolder.ParseName^(objFile.Title^).path 
    echo if instr^(objFile.items.item.path,"."^)^>0 then 
    echo wscript.echo objFile.Items.Item.Path 
    echo wscript.quit 
    echo end if 
    echo Msgbox "Please try again to choose a file rather than a folder. " ^& objFile.items.item.path 
    echo Loop 
) 

set file=ERROR 
for /f "tokens=*" %%a in ('cscript //nologo "%temp%\fileSelectorDialog.vbs"') do set file=%%a 
if "%file%"=="ERROR" (
echo There was an error or you cancelled 
) ELSE (
echo Path chosen was %file% 
) 
pause 

但是這段代碼只允許基本的「Computers」路徑作爲我觸發時打開的初始目錄,而我需要將我當前的目錄(執行腳本的目錄)作爲初始目錄當文件選擇被觸發時。

有人可以幫助我通過Vbscript/Jscript或Powershell來實現嗎?

回答

1

一個批 - PowerShell混合文件,不使用臨時文件。
此批處理需要比Win7提供的PsV2更新的PowerShell版本。

<# : batch portion (begins with PowerShell multi-line comment block) 
:: from rojo/npocmaka http://stackoverflow.com/a/41195176/1683264 
@echo off & setlocal 
Set "InitialDir=%CD%" 
Echo InitialDir=%InitialDir% 
For /f "delims=" %%A in (
'powershell -noprofile -NoLogo "iex (${%~f0} | out-string)"' 
) Do Set "File=%%A 

Echo You selected file %file% 
Pause 

Exit /b 

: ---------------- end batch/begin PowerShell hybrid --------------------#> 

[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null 
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog 
$OpenFileDialog.initialDirectory = $Env:initialDir 
#$OpenFileDialog.filter = "Text (*.txt) | *.txt | All Files| *.*" 
$OpenFileDialog.ShowDialog() | Out-Null 
$OpenFileDialog.filename 

保存與.bat.cmd擴展和運行 - 將使用當前目錄。

this question還有另一個batch-powershell hybrid by rojo,它也與PowerrShell V2一起使用。


vbscript A工作液是由Rob van der woude提供,但照顧,它的最後一個版本下跌的一頁。

+0

這個腳本,當我通過點擊它來執行它時,它只是打開命令窗口並保持空白,並且文件對話框根本不會打開。 –

+0

更改批次以顯示當前目錄,請檢查運行時顯示的內容。我打算你做了一個完整的複製/粘貼。你嘗試使用本地驅動器嗎? – LotPings

+0

是在本地驅動器上試過,但GUI文件對話框從不出現,正如我之前所說的,初始目錄路徑是正確的,但我不明白它爲什麼不打開GUI文件選擇對話框,如Windows操作系統本地文件? –

0

嘗試用 「」 代替 「17 ^」,像這樣:

:: echo Set objFile = objShell.BrowseForFolder^(0,"Select a file",^&H4000,"") 
+0

仍然給出錯誤'預期')'' –

+0

但批註出來(不適用於vbs - 會重新發生錯誤),這是沒有用的。 Rem在批處理**和** vbscript中有效用於註釋。您的建議缺少逃脫的右括號 – LotPings

+0

Ofcourse我刪除了'rem'或'::'並進行了檢查。否則我不能選擇文件,'rem'使命令被禁用,無論是雙qwotes還是沒有雙引號,我已經表示該行因爲它不工作而被稱讚。 –

0

看看下面的VBScript示例:

Option Explicit 
Dim sIniDir, sFilter, sTitle, sShowInTaskBar 
  
sIniDir = "C:\*" 
sFilter = "All files (*.*)|*.*|Microsoft Word (*.doc;*.docx)|*.doc;*.docx" 
sTitle = "GetFileDlg" 
sShowInTaskBar = "yes" 

MsgBox GetFileDlg(sIniDir, sFilter, sTitle, sShowInTaskBar) 

Function GetFileDlg(sIniDir, sFilter, sTitle, sShow) 
   ' source http://forum.script-coding.com/viewtopic.php?pid=75356#p75356 
    Dim sSignature, oShellWnd, oWnd, oProc 
    sSignature = Left(CreateObject("Scriptlet.TypeLib").Guid, 38) 
    Set oProc = CreateObject("WScript.Shell").Exec("mshta ""about:<script>moveTo(-32000,-32000);document.title=' '</script><object id=d classid=clsid:3050f4e1-98b5-11cf-bb82-00aa00bdce0b></object><object id=s classid='clsid:8856F961-340A-11D0-A96B-00C04FD705A2'><param name=RegisterAsBrowser value=1></object><script>s.putproperty('" & sSignature & "',document.parentWindow);function q(i,f,t){return d.object.openfiledlg(i,null,f,t)};</script><hta:application showintaskbar=" & sShow & "/>""") 
   On Error Resume Next 
    Do 
        If oProc.Status > 0 Then 
            GetFileDlg = "" 
           Exit Function 
        End If 
       For Each oShellWnd In CreateObject("Shell.Application").Windows 
           Err.Clear 
           Set oWnd = oShellWnd.GetProperty(sSignature) 
           If Err.Number = 0 Then Exit Do 
        Next 
    Loop 
    On Error GoTo 0 
    oWnd.Document.Title = sTitle 
    GetFileDlg = oWnd.q(sIniDir, sFilter, sTitle) 
    oWnd.Close 
End Function 

Source

+0

這只是在文件選擇對話框中打開腳本當前目錄的父目錄,我需要腳本的當前目錄作爲文件選擇對話框中的默認打開路徑。 –

+0

我已經在問題中更新了我的操作系統信息,如果這可能會有所幫助。 –

相關問題