【问题标题】:Batch script open current directory or directory of choice with powershell / vbscrpt command批处理脚本打开当前目录或使用 powershell / vbscrpt 命令选择的目录
【发布时间】:2018-01-21 22:31:15
【问题描述】:

我有以下批处理脚本来打开一个文件选择对话框,我需要从中获取所选文件的名称和路径(我的操作系统是Windows 7 64Bit):

@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

但是这个 sn-p 只允许在我触发它时打开基本的“计算机”路径作为初始目录,而我需要将我的当前目录(正在执行脚本的目录)作为初始目录当文件选择被触发时。

有人可以通过 Vbscript/Jscript 或 Powershell 帮助我实现这一目标吗?

【问题讨论】:

    标签: powershell batch-file file-io vbscript jscript


    【解决方案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。


    Rob van der woude 提供了适用于vbscript 的有效解决方案,但请注意,它是页面下方的最后一个版本。

    【讨论】:

    • 这个脚本,当我点击它执行它时,它只是打开命令窗口并保持空白,文件对话框根本不会打开。
    • 更改批处理以显示当前目录,请检查运行时显示的内容。我假设你做了一个完整的复制/粘贴。您是否在本地驱动器上尝试过?
    • Yes Itried 在本地驱动器上,但 GUI 文件对话框从未出现,正如我之前所说,初始目录路径是正确的,但我不明白为什么它不打开 GUI 文件选择类似于 Windows OS NATIVE 的对话框?
    • 我已经在问题中更新了我的操作系统信息,如果有帮助的话。
    • 看起来是版本问题,Win 7 自带 PowerShell 2。试试这个version from rojo
    【解决方案2】:

    尝试将“17^”替换为“”,如下所示:

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

    【讨论】:

    • 但是注释掉批处理(不适用于 vbs - 将返回错误)它没有用。 Rem 在批 vbscript 中有效,用于注释掉。您的建议缺少转义的右括号
    • 当然我删除了rem:: 并检查了。否则我将无法选择文件,rem 使命令禁用,无论是双 qwotes 还是没有双引号,我已经赞扬了该行,因为它不起作用。
    • 请检查我在问题中更新的代码,让我知道如何默认打开我当前的脚本执行目录?
    • 我已经在问题中更新了我的操作系统信息,如果有帮助的话。
    【解决方案3】:

    看看下面的 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

    【讨论】:

    • 这只是在文件选择对话框中打开脚本当前目录的父目录,我需要在文件选择对话框中将当前脚本目录作为默认打开路径。
    • 我已经在问题中更新了我的操作系统信息,如果有帮助的话。
    猜你喜欢
    • 1970-01-01
    • 2013-07-27
    • 1970-01-01
    • 1970-01-01
    • 2015-11-24
    • 2011-06-11
    • 1970-01-01
    • 2022-08-21
    • 1970-01-01
    相关资源
    最近更新 更多