Here is a hybrid batch/vbscript I wrote to get a browseforfolder dialog when running a batch file. I've tweaked it as much as my current skill allows and it works pretty well as is but I thought I'd throw it up for critiques or improvements/suggestions.
@Echo off
setlocal
Call :BrowseFolder "Choose Source folder" "C:\scripts\batch\"
Set SourceFolder=%Result%
Call :BrowseFolder "Choose Destination folder" "C:\scripts\"
Set DestinationFolder=%Result%
Echo %SourceFolder%
Echo %DestinationFolder%
cmd /k
endlocal
Goto :EOF
:BrowseFolder
set Result=
set vbs="%temp%\_.vbs"
set cmd="%temp%\_.cmd"
for %%f in (%vbs% %cmd%) do if exist %%f del %%f
for %%g in ("vbs cmd") do if defined %%g set %%g=
>%vbs% echo set WshShell=WScript.CreateObject("WScript.Shell")
>>%vbs% echo set shell=WScript.CreateObject("Shell.Application")
>>%vbs% echo set f=shell.BrowseForFolder(0,%1,0,%2)
>>%vbs% echo if typename(f)="Nothing" Then
>>%vbs% echo wscript.echo "set Result=Dialog Cancelled"
>>%vbs% echo WScript.Quit(1)
>>%vbs% echo end if
>>%vbs% echo set fs=f.Items():set fi=fs.Item()
>>%vbs% echo p=fi.Path:wscript.echo "set Result=" ^& p
cscript //nologo %vbs% > %cmd%
for /f "delims=" %%a in (%cmd%) do %%a
for %%f in (%vbs% %cmd%) do if exist %%f del %%f
for %%g in ("vbs cmd") do if defined %%g set %%g=
goto :eof
I have not thoroughly tested this, but wanted to give you an example based on my comment. This method does not require any additional temporary files.
@if (@CodeSection == @Batch) @then
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: The first line in the script is...
:: in Batch, a valid IF command that does nothing.
:: in JScript, a conditional compilation IF statement that is false.
:: So the following section is omitted until the next "[at]end".
:: Note: the "[at]then" is required for Batch to prevent a syntax error.
:: If the task cannot be done in batch, use PowerShell with fallback J/WScript.
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Batch Section
@echo off
for /f "delims=" %%A in ('CScript //E:JScript //Nologo "%~f0" FolderBox "Hello Temp" "%Temp%"') do echo %%A
pause
exit /b 0
:: End of Batch
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@end
////////////////////////////////////////////////////////////////////////////////
// JScript Section
try
{
switch(WScript.Arguments.Item(0))
{
case 'FolderBox':
{
var Title = WScript.Arguments.Item(1);
var StartPath = WScript.Arguments.Item(2);
var Shell = WScript.CreateObject('Shell.Application');
var Result = Shell.BrowseForFolder(0, Title, 0, StartPath);
if (Result != null)
{
var Items = Result.Items();
if (Items != null)
{
for (var i = 0; i < Items.Count; i++)
{
WScript.Echo(Items.Item(i).Path);
}
WScript.Quit(0);
}
}
WScript.Quit(1);
}
break;
default:
{
WScript.Echo('Invalid Command: ' + WScript.Arguments.Item(0));
}
break;
}
}
catch(e)
{
WScript.Echo(e);
WScript.Quit(1);
}
WScript.Quit(0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With