从批处理浏览文件夹。我的杂交批次/ vbscript

时间:2013-10-23 12:59:31

标签: batch-file vbscript

这是我编写的混合批处理/ vbscript,用于在运行批处理文件时获取browseforfolder对话框。我已经调整了它,就像我目前的技能所允许的那样,并且它的工作原理很好,但我认为我会把它用于批评或改进/建议。

    @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

1 个答案:

答案 0 :(得分: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);