This article shows how to program OpenFileDialog window in a batch file i.e. BAT or CMD. It will be more convenient for users to be able to select a file with mouse in graphical dialog box rather than typing exact path and name in the command line.
How to create OpenFile dialog box in bat file
As there are no built-in dialog boxes in bat files I’m going to use an external software component.
That’s PowerShell which nowadays is always pre-installed on Windows giving access to System.Windows.Forms.OpenFileDialog component:
PowerShell script to display OpenFIle dialog box
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | out-null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = "C:\"
$OpenFileDialog.filter = "Excel workbooks (*.xlsx)|*.xlsx|All files (*.*)|*.*"
$OpenFileDialog.ShowDialog()
$OpenFileDialog.filename
This snippet sets file type filter (either Excel workbooks or all files for example), opens dialog box and accept user choice returning OK followed by file name:
OK
C:\Users\user\Documents\calendar 2020.xlsx
If no file was selected just Cancel will be returned.
Cancel
How to call PowerShell script from bat file and process returned text
Command line to run the script is
powershell -executionpolicy bypass -file openfiledialog-xlsx.ps1
Bat file should run the script and process its output line-by-line.
If a file was selected %filename% variable will contain path and name of the selected file otherwise the variable will be empty.
@echo off
for /F "tokens=* usebackq" %%a in (`powershell -executionpolicy bypass -file openfiledialog-xlsx.ps1`) do if not "%%a" == "Cancel" if not "%%a" == "OK" set filename=%%a
if not "%filename%"=="" echo %filename%
Bat file with embedded PowerShell script
It is actually rather creating PowerShell script dynamically than usual embedding.
Anyway it eliminates keeping extra script file (PowerShell). Also it allows to set initialDirectory and filter values within bat file.
Bat file
This bat file firstly creates PowerShell script. Values for OpenFileDialog properties can be set dynamically.
After that the bat file runs the created script and processes its output.
Finally the bat file deletes the script because it is not needed anymore and prints file name if a file was selected.
@echo off
set ps_fn=ofd.ps1
echo [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") ^| out-null > %ps_fn%
echo $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog >> %ps_fn%
echo $OpenFileDialog.initialDirectory = "%USERPROFILE%\Downloads" >> %ps_fn%
echo $OpenFileDialog.filter = "Excel workbooks (*.xlsx)|*.xlsx|All files (*.*)|*.*" >> %ps_fn%
echo $OpenFileDialog.ShowDialog() >> %ps_fn%
echo $OpenFileDialog.filename >> %ps_fn%
for /F "tokens=* usebackq" %%a in (`powershell -executionpolicy bypass -file %ps_fn%`) do if not "%%a" == "Cancel" if not "%%a" == "OK" set filename=%%a
del %ps_fn%
if not "%filename%"=="" echo %filename%
OpenFileDialog properties
initialDirectory
property can be a folder in user’s profile (“%USERPROFILE%\Downloads
“), directory of bat file (“%~dp0
“) or current directory (“%cd%
“).
filter
property can be configured depending on file type. The example above shows fiter for Excel workbook XLSX or all files..
Here are examples for txt and csv files. .
Text files (*.txt)|*.txt|All files (*.*)|*.*
CSV files (*.csv)|*.csv|All files (*.*)|*.*
Frequently asked questions
Does it support long file path and name with spaces?
Yes, the script processes correclty file names containing spaces.
Does it allow to select multiple files at once?
No, this version for selecting one file only.
Does it support Unicode characters in file path or name?
No, any character that does not fall in ANSI character set will be converted to question mark.
Actually PowerShell script does support Unicode but its output is converted to ANSI within bat file.
Files to download
See also
System.Windows.Forms.OpenFileDialog class
Leave a Reply