循环的批处理文件在goto:eof之后保留变量数据

时间:2016-04-03 00:05:42

标签: loops batch-file for-loop

所以我有一个子程序函数打开一个文件夹对话框我可以将文件夹路径位置输出到txt文件就好了。但似乎无法将位置存储在变量中。

@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

1 个答案:

答案 0 :(得分:0)

像他说的那样 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

第二种方法:

@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
::***************************************************************************

第三种方法:这是一个混合批处理+ JScript示例。用.bat扩展名保存。

@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 : '');