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 }