Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save File dialog with PowerShell/C# from cmd batch

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: Save Dialog

(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

enter image description here

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.

like image 880
Terus Avatar asked May 02 '13 22:05

Terus


People also ask

How do I save a dialog box save in C#?

To save a file using the SaveFileDialog component. Display the Save File dialog box and call a method to save the file selected by the user. Use the SaveFileDialog component's OpenFile method to save the file. This method gives you a Stream object you can write to.

How do I display Save As dialog box?

Use the shortcut F12 to display the Save As dialog box in Excel. Press CTRL + s to save an existing workbook. It's good practice to periodically save while you are working on your Excel file.

Which command is used to open the Save As dialogue box?

One is on the Quick Access Toolbar, or we can press F12 and display the save as option or press the keyboard shortcut CTRL+S, which opens the “Save As” dialog box to save the file in the desired format path.

How do I save SaveFileDialog in VB net?

The SaveFileDialog control prompts the user to select a location for saving a file and allows the user to specify the name of the file to save data. The SaveFileDialog control class inherits from the abstract class FileDialog.


2 Answers

Here is an example using Windows Forms (SaveFileDialog Class).

Rq : You don't need button for creating a new directory, because you can just right click on file panel to do so.

Clear-Host
Add-Type -AssemblyName system.Windows.Forms
$saveFile = New-Object System.Windows.Forms.SaveFileDialog

$saveFile.Filter = "All files (*.*)|*.*"
$saveFile.FilterIndex = 2
$saveFile.RestoreDirectory = $true

$rc = $saveFile.ShowDialog()
if ($rc -eq [System.Windows.Forms.DialogResult]::OK)
{
  $saveFile.FileName
  [System.Windows.Forms.MessageBox]::Show("You can save $($saveFile.FileName)", "Ok")
}

Be careful, the message box, when it's ok, does not appear on the top of Windows.

like image 185
JPBlanc Avatar answered Nov 15 '22 07:11

JPBlanc


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

Save Dialog

like image 29
Terus Avatar answered Nov 15 '22 09:11

Terus