我想從基於gui的保存文件作爲對話框從一個Windows批處理腳本。我想在保存&取消按鈕左側有一個新的文件夾按鈕。沒有預定義的文件類型。所以它可能必須設置爲所有文件。使用PowerShell/C#從cmd批保存文件對話框
事情與此類似:
(忽略左上角的按鈕)
我已經找到了一個不錯soloution,從頂部的答案,在這裏打開一個文件: File/folder chooser dialog from a Windows batch script
將對話框另存爲類似的解決方案會很好。最重要的是,我希望能夠將路徑&文件名設置爲它們自己的變量。出於該示例的目的或緣故。我們可以插入「Hello World」作爲輸出。
exempli特惠:
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
我會問在現有的職位。但是這在站點規則中是不允許的。所以我分開問。歡迎任何幫助或見解。
謝謝你的時間&的努力。
我的解決方案更短,我有兩個問題:爲什麼要將您的PowerShell代碼封裝到CMD代碼中?你爲什麼要在PowerShell中編譯C#,而你只是使用它內聯(Add-Type),或者在你的情況下只使用沒有C#的.NET類? – JPBlanc 2013-05-03 05:33:18
這樣做的目的是在windows批處理腳本,.bat文件中打開一個對話框。我不是那個將標題改爲狀態powershell而不是批處理的人。我的結果幾乎是我從鏈接帖子中找到的代碼的直接副本,在這裏引用。所以對於C#位。我不太確定。但我確實喜歡有第二個功能選擇。 – Terus 2013-05-03 14:05:25