这是我编写的混合批处理/vbscript,用于在运行批处理文件时获取浏览文件夹对话框。我已经在我目前的技能允许的范围内对其进行了尽可能多的调整,并且效果很好,但我想我会把它扔给批评或改进/建议。
@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