批处理脚本使用powershell / vbscrpt命令打开当前目录或目录

时间:2017-08-14 06:22:22

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

我有以下批处理脚本打开文件选择对话框,我需要从中获取所选文件的名称和路径(我的操作系统为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

但是这个片段只允许基本的&#34;计算机&#34; path作为我触发它时要打开的初始目录,而我需要将当前目录(执行脚本的目录)作为触发文件选择时的初始目录。

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

3 个答案:

答案 0 :(得分: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提供 { "name": "carweb", "version": "0.1.0", "private": true, "dependencies": { "async": "^2.5.0", "body-parser": "^1.17.2", "cors": "^2.8.4", "crypto-js": "^3.1.9-1", "dotenv": "^4.0.0", "expressjs": "^1.0.1", "jquery": "^3.2.1", "jwt-simple": "^0.5.1", "nodemon": "^1.11.0", "react": "^15.6.1", "react-countup": "^2.1.1", "react-document-title": "^2.0.3", "react-dom": "^15.6.1", "react-materialize": "^1.0.6", "react-rethinkdb": "^0.6.0", "react-router-dom": "^4.1.2", "react-scripts": "1.0.10", "rethinkdb": "^2.3.3", "spicy-datatable": "^0.4.0", "web-storage": "^1.0.0" }, "scripts": { "start": "react-scripts start", "backend": "nodemon ./src/js/backend/app.js", "build": "react-scripts build", "test": "react-scripts test --env=jsdom", "eject": "react-scripts eject" } } 的工作解决方案,但请注意,它是页面下的最后一个版本。

答案 1 :(得分:0)

尝试更换&#34; 17 ^&#34;与&#34;&#34;,像这样:

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

答案 2 :(得分: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