Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 从批处理中浏览文件夹。我的混合批处理/vbscript_Batch File_Vbscript - Fatal编程技术网

Batch file 从批处理中浏览文件夹。我的混合批处理/vbscript

Batch file 从批处理中浏览文件夹。我的混合批处理/vbscript,batch-file,vbscript,Batch File,Vbscript,下面是我编写的混合批处理/vbscript,用于在运行批处理文件时获得browseforfolder对话框。我已经尽我目前的技能所能对它进行了调整,效果也很好,但我想我会把它扔掉,以供批评或改进/建议 @Echo off setlocal Call :BrowseFolder "Choose Source folder" "C:\scripts\batch\" Set SourceFolder=%Result% Call :BrowseFolder "C

下面是我编写的混合批处理/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
下面是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);
@如果(@code节==@Batch)@那么
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::脚本中的第一行是。。。
::在批处理中,一个有效的IF命令,它不执行任何操作。
::在JScript中,一种条件编译IF语句,该语句为false。
::因此,在下一个“[at]end”之前,将省略以下部分。
::注意:批处理需要“[at]then”以防止语法错误。
::如果无法成批完成任务,请将PowerShell与fallback J/WScript一起使用。
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::批处理部分
@回音
对于('CScript//E:JScript//Nologo“%~f0”FolderBox“Hello Temp”“%Temp%”中的/f“delims=”%%A,执行echo%%A
暂停
退出/b0
::批次结束
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@结束
////////////////////////////////////////////////////////////////////////////////
//JScript部分
尝试
{
开关(WScript.Arguments.Item(0))
{
案例“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);
如果(结果!=null)
{
var Items=Result.Items();
如果(项!=null)
{
对于(变量i=0;i
StackOverflow不是展示您作品的合适场所。为此,请使用博客或论坛。为什么不直接用vbscript编写所有内容?建议使用该方法。因为有时人们有大量批处理文件,只想为其添加一些功能,而使用长目录名的set/p是非常烦人的。这就是我当初写这本书的原因。请注意:
@then
部分是批量必需的,以避免语法错误,因为
if
命令不完整,但可以是任何单词。@Aacini谢谢!我没有意识到这一点,但现在你提到这一点,我可以清楚地看到。