title: A windows batch script open a file chooser dialog or drag-and-drop file to it
body: <p>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.</p>
  <p>For the first part, it's easy:</p>
  <pre><code>@echo off
  bin\dosomething &quot;%~1&quot;
  </code></pre>
  <p>For the second part, I googled this thread: <a href="https://stackoverflow.com/a/15885133/1683264">https://stackoverflow.com/a/15885133/1683264</a></p>
  <p>It also works.</p>
  <p>But, I can't combine these two to one. I've tried</p>
  <pre><code>if &quot;%~1&quot; == [] goto select
  </code></pre>
  <p>then add <code>:select</code> before the second part, it just don't work. Codes below:</p>
  <pre><code>@ECHO OFF
  if &quot;%~1&quot; == [] goto select
  bin\dosomething &quot;%~1&quot;
  goto :EOF
  
  :select
  
  &lt;# : 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 &quot;delims=&quot; %%I in ('powershell -noprofile &quot;iex (${%~f0} | out-string)&quot;') do (
      echo You chose %%~I
      bin\dosomething &quot;%%~I&quot;
  )
  goto :EOF
  
  : end Batch portion / begin PowerShell hybrid chimera #&gt;
  
  Add-Type -AssemblyName System.Windows.Forms
  $f = new-object Windows.Forms.OpenFileDialog
  $f.InitialDirectory = pwd
  $f.Filter = &quot;Text Files (*.txt)|*.txt|All Files (*.*)|*.*&quot;
  $f.ShowHelp = $true
  $f.Multiselect = $true
  [void]$f.ShowDialog()
  if ($f.Multiselect) { $f.FileNames } else { $f.FileName }
  </code></pre>
  <hr />
  <p>I tried <code>If &quot;%~1&quot;==&quot;&quot;</code>, it jumped as purpose, but the dialog windows still don't appear, CMD directly output error lines as:</p>
  <pre><code>You chose + iex (${D:\Program Files (x86)\BBB\choose list file.bat} | out-strin ...
  </code></pre>
  <hr />
  <h1>Solved</h1>
  <p>It's solved. Only &quot;%~1&quot; works right.</p>
  <p>I paste code here:</p>
  <pre><code>&lt;# : 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 &quot;%~1&quot; == &quot;&quot; goto SELECT
  bin\dosomething &quot;%~1&quot;
  goto :EOF
  
  :SELECT
  
  setlocal
  for /f &quot;delims=&quot; %%I in ('powershell -noprofile &quot;iex (${%~f0} | out-string)&quot;') do (
      echo You chose %%~I
      bin\dosomething &quot;%%~I&quot;
  )
  goto :EOF
  
  : end Batch portion / begin PowerShell hybrid chimera #&gt;
  
  Add-Type -AssemblyName System.Windows.Forms
  $f = new-object Windows.Forms.OpenFileDialog
  $f.InitialDirectory = pwd
  $f.Filter = &quot;Text Files (*.txt)|*.txt|All Files (*.*)|*.*&quot;
  $f.ShowHelp = $true
  $f.Multiselect = $true
  [void]$f.ShowDialog()
  if ($f.Multiselect) { $f.FileNames } else { $f.FileName }
  </code></pre>