1

So I have a subroutine function that opens a folder dialog I can output the folder path location to a txt file just fine. But Can't seem to store the location in the variable.

@echo off
setlocal
Call :OutputSelect
echo %location%
pause
endlocal
exit

:OutputSelect
setlocal enabledelayedexpansion
set "fchooser=%temp%\fchooser.exe"
if exist "!fchooser!" del "!fchooser!"
>"%temp%\c.cs" echo using System;using System.Windows.Forms;
>>"%temp%\c.cs" echo class dummy{[STAThread]
>>"%temp%\c.cs" echo public static void Main^(^){
>>"%temp%\c.cs" echo FolderBrowserDialog f=new FolderBrowserDialog^(^);
>>"%temp%\c.cs" echo f.SelectedPath=System.Environment.CurrentDirectory;
>>"%temp%\c.cs" echo f.Description="Select Output Folder";
>>"%temp%\c.cs" echo f.ShowNewFolderButton=true;
>>"%temp%\c.cs" echo if^(f.ShowDialog^(^)==DialogResult.OK^){Console.Write^(f.SelectedPath^);}}}
for /f "delims=" %%I in ('dir /b /s "%windir%\microsoft.net\*csc.exe"') do (
if not exist "!fchooser!" "%%I" /nologo /out:"!fchooser!" "%temp%\c.cs" 2>NUL
)
del "%temp%\c.cs"
setlocal disabledelayedexpansion    
for /f "delims=" %%I in ('%fchooser%') do set location=%%I\
goto:eof
  • 2
    Remove `setlocal disabledelayedexpansion` and change the next-to-last line to `for /f "delims=" %%I in ('%fchooser%') do endlocal & set "location=%%I\"`. [See this page](http://www.dostips.com/DtTutoFunctions.php#FunctionTutorial.ReturningLocalVariables) for full explanation. – rojo Apr 03 '16 at 00:14
  • 2
    By the way, the `Shell.Application` COM object `BrowseForFolder()` method is [much easier and faster](http://stackoverflow.com/a/15906994/1683264) than the .NET `FolderBrowserDialog()` method, I think. – rojo Apr 03 '16 at 01:10
  • 1
    The issue is that `setlocal` establishes a local environment, it is not a switch. To pass information out of a `setlocal/endlocal` bracket use `endlocal&set "var=%var%"` Note that reaching EOF is an implicit `endlocal` if a local environment is currently in-context. – Magoo Apr 03 '16 at 04:47

1 Answers1

0

Try like as he said rojo :

@echo off
Call :OutputSelect
echo %location%
pause
exit

:OutputSelect
setlocal enabledelayedexpansion
set "fchooser=%temp%\fchooser.exe"
if exist "!fchooser!" del "!fchooser!"
(
    echo using System;using System.Windows.Forms;
    echo class dummy{[STAThread]
    echo public static void Main^(^){
    echo FolderBrowserDialog f=new FolderBrowserDialog^(^);
    echo f.SelectedPath=System.Environment.CurrentDirectory;
    echo f.Description="Select Output Folder";
    echo f.ShowNewFolderButton=true;
    echo if^(f.ShowDialog^(^)==DialogResult.OK^){Console.Write^(f.SelectedPath^);}}}
)>"%temp%\c.cs"
for /f "delims=" %%I in ('dir /b /s "%windir%\microsoft.net\*csc.exe"') do (
if not exist "!fchooser!" "%%I" /nologo /out:"!fchooser!" "%temp%\c.cs" 2>NUL
)
del "%temp%\c.cs"   
for /f "delims=" %%I in ('%fchooser%') do endlocal & set "location=%%I\"
goto:eof

Second Method :

@echo off
Call :Browse4Folder "Choose Source folder" "C:\scripts\batch\"
echo "%Location%"
Pause
::***************************************************************************
:Browse4Folder
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=
(
    echo set shell=WScript.CreateObject("Shell.Application"^) 
    echo set f=shell.BrowseForFolder(0,%1,0,%2^) 
    echo if typename(f^)="Nothing" Then  
    echo wscript.echo "set Location=Dialog Cancelled" 
    echo WScript.Quit(1^)
    echo end if 
    echo set fs=f.Items(^):set fi=fs.Item(^) 
    echo p=fi.Path:wscript.echo "set Location=" ^& p
)>%vbs%
cscript //nologo %vbs% > %cmd%
for /f "delims=" %%a in (%cmd%) do %%a
for %%f in (%vbs% %cmd%) do if exist %%f del /f /q %%f
for %%g in ("vbs cmd") do if defined %%g set %%g=
goto :eof
::***************************************************************************

Third Method : Here's a hybrid batch + JScript example. Save it with a .bat extension.

@if (@a==@b) @end /*
@echo off
setlocal
for /f "delims=" %%I in ('cscript /nologo /e:jscript "%~f0"') do (
   Set Location=%%I
)
echo "%Location%"
pause
goto :EOF

:: JScript portion */
var shl = new ActiveXObject("Shell.Application");
var folder = shl.BrowseForFolder(0, "Please choose a folder.", 0, 0x00);
WSH.Echo(folder ? folder.self.path : '');
Hackoo
  • 18,337
  • 3
  • 40
  • 70