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 }
4
  • Why the mix of batch and powershell? Commented Sep 28, 2019 at 19:29
  • can you only do batch? Commented Sep 28, 2019 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 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. Commented Sep 29, 2019 at 6:14
  • @BoogaRoo. You can do everything using batch, hence my comment. Commented Sep 29, 2019 at 15:52

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.