使用PowerShell/C#从cmd批处理保存文件对话框

Ter*_*rus 5 .net powershell user-interface dialog batch-file

我试图从Windows批处理脚本中获取基于gui的保存文件作为对话框.我想在保存和取消按钮左边有一个新的文件夹按钮.没有预定义的文件类型.所以它可能必须设置为所有文件.

与此类似的东西: 保存对话框

(忽略左上角的按钮)

我从最顶层的答案中找到了一个很好的解决方案,用于打开文件: Windows批处理脚本中的文件/文件夹选择器对话框

对于另存为对话框,有一个类似的解决方案会很好.最重要的是,我希望能够将路径和文件名设置为自己的变量.出于示例的目的或目的.我们可以输入"Hello World"作为输出.

特例:

echo Hello World > %variable.path%%variable.file%
Run Code Online (Sandbox Code Playgroud)

以下示例是链接帖子的直接引用.

:: 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
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我试图调整上面的例子来保存为.但我不太了解这样做.这就是为什么我首先使用批处理脚本而不是更高级的编程语言.

我对上述内容的理解是,Windows批处理脚本调用PowerShell(如果存在)或PowerShell不存在.net(如果存在).然后我将使用我现有的代码作为替代,如果两者都不存在.替代方案是让用户手动输入完整路径.

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
Run Code Online (Sandbox Code Playgroud)

我会问现有的帖子.但这在站点规则中是不允许的.所以我分开询问了.欢迎任何帮助或见解.

感谢您的时间和精力.

JPB*_*anc 5

以下是使用Windows窗体(SaveFileDialog类)的示例.

Rq:您不需要用于创建新目录的按钮,因为您可以右键单击文件面板来执行此操作.

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")
}
Run Code Online (Sandbox Code Playgroud)

小心,消息框,当它没问题时,不会出现在Windows的顶部.


Ter*_*rus 1

我自己设法让它发挥作用。虽然没有我想要创建一个新文件夹的按钮。右键单击涉及更多步骤并强制使用鼠标。该按钮可以编程为使用 ALT+N 进行调用。尽管如此,我基本上已经得到了我最初寻求的结果代码。

:: 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
Run Code Online (Sandbox Code Playgroud)

保存对话框