我正在尝试从Windows批处理脚本中获取基于GUI的保存文件对话框。我希望在保存和取消按钮左侧有一个新建文件夹按钮,而且没有预定义的文件类型。因此,它可能需要设置为所有文件。
类似于这个示例:
(忽略左上角的按钮)
我已经找到了一个不错的解决方案,来自顶部答案,在这里打开文件: File / folder chooser dialog from a Windows batch script 最重要的是,我希望能够设置路径和文件名为它们自己的变量。以"Hello World"作为输出示例。
下面的例子是从链接帖子中直接引用的。
我已经尝试将上面的示例调整为“另存为”工作,但我不理解得足够好。这就是为什么我一开始使用批处理脚本而不是更高级的编程语言的原因。
我的理解是它是Windows批处理脚本,如果PowerShell存在,它会调用PowerShell,否则调用.net。然后,如果两者都不存在,我将使用我的现有代码作为替代方案。替代方案只需手动输入完整路径。
我本来想在现有的帖子上提问。但是根据网站规则,这是不允许的。所以我另外发了一个问题。欢迎任何帮助或见解。
感谢您的时间和努力。
类似于这个示例:
![Save Dialog](https://istack.dev59.com/CpnYo.gif)
我已经找到了一个不错的解决方案,来自顶部答案,在这里打开文件: File / folder chooser dialog from a Windows batch script 最重要的是,我希望能够设置路径和文件名为它们自己的变量。以"Hello World"作为输出示例。
echo Hello World > %variable.path%%variable.file%
下面的例子是从链接帖子中直接引用的。
:: 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
我已经尝试将上面的示例调整为“另存为”工作,但我不理解得足够好。这就是为什么我一开始使用批处理脚本而不是更高级的编程语言的原因。
我的理解是它是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
我本来想在现有的帖子上提问。但是根据网站规则,这是不允许的。所以我另外发了一个问题。欢迎任何帮助或见解。
感谢您的时间和努力。