2

The scenario I have in mind is as follow: - User drags a file (example text.txt) onto the .bat script.

  • Batch file opens the Save Dialog box, allowing the user to save this file under another name.

As I understand, I should define the file to be dragged and dropped, as %1 and what I am missing is how to open the Save Dialog box from the batch file.

My current code is:

<# : 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
)
goto :EOF

: end Batch portion / begin PowerShell hybrid chimera #>

Add-Type -AssemblyName System.Windows.Forms

$f = new-object Windows.Forms.OpenFileDialog
:: i tried to change $f = new-object Windows.Forms.SaveFileDialog

$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 }
Compo
  • 36,585
  • 5
  • 27
  • 39
  • Why the mix of batch and powershell? – Gerhard Sep 28 '19 at 19:29
  • can you only do batch? – Carmine Locorotondo Sep 28 '19 at 22:08
  • 2
    @GerhardBarnard Because you can't drag and drop directly onto a .ps1 file. To set that up [requires some registry editing](https://stackoverflow.com/questions/2819908/drag-and-drop-to-a-powershell-script) and has some side effects like causing the .ps1 file to execute when double clicked instead of opening in Notepad. Though, as you can see, this is a moderately common desired thing to do. The common alternative is to make a .bat file that allows drag and drop without all the messing around in the registry or making shortcuts and wrapper scripts. It's a mess. – Booga Roo Sep 29 '19 at 06:14
  • @BoogaRoo. You can do everything using batch, hence my comment. – Gerhard Sep 29 '19 at 15:52

1 Answers1

0

I think you should give a filename as input. Calling the member => FileName. It seems "Windows.Forms.SaveFileDialog()" should do.

  • 1
    Could you please provide an example of your answer in context of the OP's submitted code. As it stands, your answer seems more suited to the comment section, without such an edit. – Compo Feb 17 '23 at 15:21