2014-02-04 4 views
5

В настоящее время я открытие файла с моей VBScript следующим образом:VBScript, чтобы открыть диалоговое окно для выбора FilePath

strFile = "C:\Users\test\file.txt" 
Set objFile = objFSO.OpenTextFile(strFile) 

Я хотел бы изменить это так, что файл может быть выбран/навигацией, на которую пользователь и этот файл используется в скрипте. Как я могу добавить эту способность? Я попытался найти, как загрузить диалог файла/пригласить пользователя для файла и т. Д., Просто не уверен, как закончить в VBScript.

ответ

17

Существует еще одно решение, которое я нашел интересным из MS TechNet меньше настройки, но получает то, что вы хотите достичь. Это возвращает полный путь к выбранному файлу.

Set wShell=CreateObject("WScript.Shell") 
Set oExec=wShell.Exec("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>""") 
sFileSelected = oExec.StdOut.ReadLine 
wscript.echo sFileSelected 
+0

Спасибо. Протестировано на 64-битной Windows 10 Pro. – kol

3

Здесь вы идете:

http://www.robvanderwoude.com/vbstech_ui_fileopen.php

strFile = GetFileName("C:\Users\test\", "Text files|*.txt") 
Set objFile = objFSO.OpenTextFile(strFile) 

Function GetFileName(myDir, myFilter) 
    ' Written by Rob van der Woude 
    ' http://www.robvanderwoude.com 

    ' Standard housekeeping 
    Dim objDialog 

    ' Create a dialog object 
    Set objDialog = CreateObject("UserAccounts.CommonDialog") 

    ' Check arguments and use defaults when necessary 
    If myDir = "" Then 
    ' Default initial folder is "My Documents" 
    objDialog.InitialDir = CreateObject("WScript.Shell").SpecialFolders("MyDocuments") 
    Else 
    ' Use the specified initial folder 
    objDialog.InitialDir = myDir 
    End If 
    If myFilter = "" Then 
    ' Default file filter is "All files" 
    objDialog.Filter = "All files|*.*" 
    Else 
    ' Use the specified file filter 
    objDialog.Filter = myFilter 
    End If 

    ' Open the dialog and return the selected file name 
    If objDialog.ShowOpen Then 
    GetFileName = objDialog.FileName 
    Else 
    GetFileName = "" 
    End If 
End Function 
+4

Помогает протестировать или хотя бы упомянуть о том, что UserAccounts.CommonDialog недоступен на любом 64-битном хосте. – SilverbackNet

+2

UserAccounts.CommonDialog работает только в Windows XP. –

Смежные вопросы