3

这是我编写的混合批处理/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
4

1 回答 1

2

这是 Batch/JScript Hybrid 中此功能的类似示例

我没有彻底测试过,但想根据我的评论给你一个例子。此方法不需要任何额外的临时文件。

@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);
于 2013-10-23T14:13:51.100 回答