Save File dialog with PowerShell/C# from cmd batch

.net, batch-file, dialog, powershell, user-interface

Solution

I managed to get it working myself. Though without the button I wanted for making a new folder. Right clicking involves more steps & forces use of the mouse. Where as the button could be programmed for calling with ALT+N. Nevertheless I basically have the resulting code that I initially sought.

:: chooser.bat
:: launches a save file dialog and outputs choice to the console
@echo off
setlocal enabledelayedexpansion

:: Does powershell.exe exist within %PATH%?
for %%I in (powershell.exe) do if "%%~$PATH:I" neq "" (
    set chooser=powershell "Add-Type -AssemblyName System.windows.forms|Out-Null;$f=New-Object System.Windows.Forms.SaveFileDialog;$f.InitialDirectory='%cd%';$f.Filter='All Files (*.*)|*.*';$f.showHelp=$true;$f.ShowDialog()|Out-Null;$f.FileName"
) else (
rem :: If not, compose and link C# application to open file browser dialog
    set chooser=%temp%\chooser.exe
    >"%temp%\c.cs" echo using System;using System.Windows.Forms;
    >>"%temp%\c.cs" echo class dummy{
    >>"%temp%\c.cs" echo public static void Main^(^){
    >>"%temp%\c.cs" echo SaveFileDialog f=new SaveFileDialog^(^);
    >>"%temp%\c.cs" echo f.InitialDirectory=Environment.CurrentDirectory;
    >>"%temp%\c.cs" echo f.Filter="All Files (*.*)|*.*";
    >>"%temp%\c.cs" echo f.ShowHelp=true;
    >>"%temp%\c.cs" echo f.ShowDialog^(^);
    >>"%temp%\c.cs" echo Console.Write^(f.FileName^);}}
    for /f "delims=" %%I in ('dir /b /s "%windir%\microsoft.net\*csc.exe"') do (
        if not exist "!chooser!" "%%I" /nologo /out:"!chooser!" "%temp%\c.cs" 2>NUL
    )
    del "%temp%\c.cs"
    if not exist "!chooser!" (
        echo Error: Please install .NET 2.0 or newer, or install PowerShell.
        goto :EOF
    )
)

:: capture choice to a variable
for /f "delims=" %%I in ('%chooser%') do set "filename=%%I"

echo You chose %filename%
pause

:: Clean up the mess
del "%temp%\chooser.exe" 2>NUL
goto :EOF

Problem

I am trying to get a gui based save file as dialog from within a windows batch script. I would like to have a new folder button left of the save & cancel buttons. With no predefined filetype. So it would probably have to be set to all files. Something similar to this: (Ignore the buttons in the top left) I have found a nice soloution, from the top answer, for opening a file here: File / folder chooser dialog from a Windows batch script It would be nice to have a similar soloution for a save as dialog. Most importantly I would like the ability to set the path & filename to their own variables. For the purpose or sake of the example. We could insert "Hello World" as output. exempli gratia: ``` echo Hello World > %variable.path%%variable.file% ``` The example below is a direct quote from the linked post. ``` :: chooser.bat :: launches a File... Open sort of file chooser and outputs choice to the console @echo off setlocal enabledelayedexpansion :: Does powershell.exe exist within %PATH%? for %%I in (powershell.exe) do if "%%~$PATH:I" neq "" ( set chooser=powershell "Add-Type -AssemblyName System.windows.forms|Out-Null;$f=New- Object System.Windows.Forms.OpenFileDialog;$f.InitialDirectory='%cd%';$f.Filter='Text Files (*.txt)|*.txt|All Files (*.*)|*.*';$f.showHelp=$true;$f.ShowDialog()|Out-Null;$f.FileName" ) else ( rem :: If not, compose and link C# application to open file browser dialog set chooser=%temp%\chooser.exe >"%temp%\c.cs" echo using System;using System.Windows.Forms; >>"%temp%\c.cs" echo class dummy{ >>"%temp%\c.cs" echo public static void Main^(^){ >>"%temp%\c.cs" echo OpenFileDialog f=new OpenFileDialog^(^); >>"%temp%\c.cs" echo f.InitialDirectory=Environment.CurrentDirectory; >>"%temp%\c.cs" echo f.Filter="Text Files (*.txt)|*.txt|All Files (*.*)|*.*"; >>"%temp%\c.cs" echo f.ShowHelp=true; >>"%temp%\c.cs" echo f.ShowDialog^(^); >>"%temp%\c.cs" echo Console.Write^(f.FileName^);}} for /f "delims=" %%I in ('dir /b /s "%windir%\microsoft.net\*csc.exe"') do ( if not exist "!chooser!" "%%I" /nologo /out:"!chooser!" "%temp%\c.cs" 2>NUL ) del "%temp%\c.cs" if not exist "!chooser!" ( echo Error: Please install .NET 2.0 or newer, or install PowerShell. goto :EOF ) ) :: capture choice to a variable for /f "delims=" %%I in ('%chooser%') do set "filename=%%I" echo You chose %filename% :: Clean up the mess del "%temp%\chooser.exe" 2>NUL goto :EOF ``` I have tried to adapt the above example to work for save as. But I don't understand it well enough to do so. Which is why I'm using batch scripts and not a higher functioning programming language in the first place. My understanding of the above is that it is Windows batch script that invokes either PowerShell, if it exists or if PowerShell doesn't exist .net if it exists. I will then have it use my existing code as an alternative if neither exist. Alternative just has user input the full path manually. ``` echo Set path to file: echo Path and file cannot contain any spaces. e.g. c:\folder_name\file.ext echo Be sure you include the filename. SET /P path1=">"? cls echo path set to: %path1% pause cls goto :eof ``` I would have asked on the existing post. But that is not permissible in the site rules. So I asked separately. Any help or insight is welcome. Thank you for your time & effort.

Original source

Related problems