简体   繁体   中英

A windows batch script open a file chooser dialog or drag-and-drop file to it

I try to write a batch script that, when you drag and drop another file to it, it will do something. If you don't drop anything, just double click it, it will open a file selection dialog window.

For the first part, it's easy:

@echo off
bin\dosomething "%~1"

For the second part, I googled this thread: https://stackoverflow.com/a/15885133/1683264

It also works.

But, I can't combine these two to one. I've tried

if "%~1" == [] goto select

then add :select before the second part, it just don't work. Codes below:

@ECHO OFF
if "%~1" == [] goto select
bin\dosomething "%~1"
goto :EOF

:select

<# : chooser.bat
:: launches a File... Open sort of file chooser and outputs choice(s) to the console
:: https://stackoverflow.com/a/15885133/1683264

@echo off
setlocal

for /f "delims=" %%I in ('powershell -noprofile "iex (${%~f0} | out-string)"') do (
    echo You chose %%~I
    bin\dosomething "%%~I"
)
goto :EOF

: end Batch portion / begin PowerShell hybrid chimera #>

Add-Type -AssemblyName System.Windows.Forms
$f = new-object Windows.Forms.OpenFileDialog
$f.InitialDirectory = pwd
$f.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
$f.ShowHelp = $true
$f.Multiselect = $true
[void]$f.ShowDialog()
if ($f.Multiselect) { $f.FileNames } else { $f.FileName }

I tried If "%~1"=="" , it jumped as purpose, but the dialog windows still don't appear, CMD directly output error lines as:

You chose + iex (${D:\Program Files (x86)\BBB\choose list file.bat} | out-strin ...

Solved

It's solved. Only "%~1" works right.

I paste code here:

<# : chooser.bat

:: drop file to execute, or open a file chooser dialog window to execute.
:: code mostly comes from https://stackoverflow.com/a/15885133/1683264

@ECHO OFF

if "%~1" == "" goto SELECT
bin\dosomething "%~1"
goto :EOF

:SELECT

setlocal
for /f "delims=" %%I in ('powershell -noprofile "iex (${%~f0} | out-string)"') do (
    echo You chose %%~I
    bin\dosomething "%%~I"
)
goto :EOF

: end Batch portion / begin PowerShell hybrid chimera #>

Add-Type -AssemblyName System.Windows.Forms
$f = new-object Windows.Forms.OpenFileDialog
$f.InitialDirectory = pwd
$f.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
$f.ShowHelp = $true
$f.Multiselect = $true
[void]$f.ShowDialog()
if ($f.Multiselect) { $f.FileNames } else { $f.FileName }

The trick with those Hybrid scripts is, to hide the batch code for the Powershell parser and to hide the Powershell code for the batch parser.

For Powershell, the part between <# and #> is a comment. Thanksfully <# : comment does no harm for the batch parser. So your batch code is supposed to be inside that Powershell comment.

On the other hand, the last batch command is goto :EOF , which means, all below ( the "end of comment"-line" for Powershell and the Powershell code itself ) will be ignored by the Batch parser.

So just move up your line <# : chooser.bat as very first line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM