像批处理文件中的函数/方法?

ste*_*wpf 56 windows command-line batch-file

是否有任何模仿方法,如从Java,C#等知道它?我在批处理文件中有5行命令,这5行用于批处理文件中的多个位置.我不能使用goto,因为根据这5行创建的错误级别,我有不同的操作.我尝试将我的5行放在批处理文件5lines.bat中,但原始批处理文件original.bat只调用5lines.bat并且在调用5lines.bat后不执行命令):这就是我的original.bat看起来的样子喜欢:

5lines.bat
echo this gets never called, how to make sure this gets called?
Run Code Online (Sandbox Code Playgroud)

在5lines.bat中没有退出或类似的东西!如何确保调用5lines.bat之后的行?

Erw*_*ald 74

您可以使用call命令:

call:myDosFunc
Run Code Online (Sandbox Code Playgroud)

然后以这种方式定义函数:

:myDosFunc    - here starts the function
echo.  here the myDosFunc function is executing a group of commands
echo.  it could do a lot of things
goto:eof
Run Code Online (Sandbox Code Playgroud)

来源:批量功能

  • 但它是如何工作的?那个'goto:eof`的神奇之处是什么? (6认同)
  • `goto:eof`与`exit/b`相同,都在调用`CALL`之后返回 (5认同)

Shi*_*hah 25

为了完整起见,您还可以将参数传递给函数:

函数调用

call :myDosFunc 100 "string val"
Run Code Online (Sandbox Code Playgroud)

功能体

:myDosFunc
echo. Got Param#1 %~1
echo. Got Param#2 %~2
goto :eof
Run Code Online (Sandbox Code Playgroud)


Cod*_*ray 22

将可重用函数放入单独的批处理文件中肯定可以模拟函数.

问题是您必须使用该call命令以确保在第二个批处理文件完成执行后控制权返回给调用者.

call 5lines.bat
echo this will now get called
Run Code Online (Sandbox Code Playgroud)

  • 有趣的是,如果将“5lines.bat”重命名为“5lines.cmd”,您会发现“call 5lines”就足够了,无需指定扩展名。 (3认同)

Dmi*_*nov 15

解:

@ECHO OFF     

call:header Start Some Operation

... put your business logic here
... make sure EXIT below is present
... so you don't run into actual functions without the call

call:header Operation Finished Successfully

EXIT /B %ERRORLEVEL%

:: Functions

:header
ECHO ================================================= 
ECHO %*
ECHO ================================================= 
EXIT /B 0
Run Code Online (Sandbox Code Playgroud)

重要的是将EXIT/B放在每个函数的末尾,以及在函数定义开始之前,在我的例子中,这是:

退出/ B%ERRORLEVEL%


Leo*_*ian 8

由于具有 Java 背景,我在创建脚本过程时尝试合并一些熟悉的约定.bat

下面的脚本演示了两个过程的定义。

@ECHO OFF
SET firstInstanceVariable="Hello world!"
SET secondInstanceVariable="Good bye world!"
GOTO:MAIN

:firstMethodName
    SETLOCAL ENABLEDELAYEDEXPANSION
        SET firstArgumentPassedIn=%~1
        SET secondArgumentPassedIn=%~2
        
        ECHO %firstInstanceVariable%
        ECHO "The first argument passed in was %firstArgumentPassedIn%"
        ECHO "The second argument passed in was %secondArgumentPassedIn%"
    ENDLOCAL
EXIT /B 0

:secondMethodName
    SETLOCAL ENABLEDELAYEDEXPANSION
        SET firstArgumentPassedIn=%~1
        SET secondArgumentPassedIn=%~2
        
        ECHO %secondInstanceVariable%
        ECHO "The first argument passed in was %firstArgumentPassedIn%"
        ECHO "The second argument passed in was %secondArgumentPassedIn%"
    ENDLOCAL
EXIT /B 0


:MAIN
call:firstMethodName "The Quick Brown" "Fox Jumps Over"
call:secondMethodName "1 2 3 4" 3.14
Run Code Online (Sandbox Code Playgroud)

请注意,需要显式地GOTO:MAIN跳过过程定义。这是因为在决定阅读之前您必须跳过该过程。否则,将执行该过程。

下面的代码演示了与上述.bat脚本非常接近的 Java 等效项。

@ECHO OFF
SET firstInstanceVariable="Hello world!"
SET secondInstanceVariable="Good bye world!"
GOTO:MAIN

:firstMethodName
    SETLOCAL ENABLEDELAYEDEXPANSION
        SET firstArgumentPassedIn=%~1
        SET secondArgumentPassedIn=%~2
        
        ECHO %firstInstanceVariable%
        ECHO "The first argument passed in was %firstArgumentPassedIn%"
        ECHO "The second argument passed in was %secondArgumentPassedIn%"
    ENDLOCAL
EXIT /B 0

:secondMethodName
    SETLOCAL ENABLEDELAYEDEXPANSION
        SET firstArgumentPassedIn=%~1
        SET secondArgumentPassedIn=%~2
        
        ECHO %secondInstanceVariable%
        ECHO "The first argument passed in was %firstArgumentPassedIn%"
        ECHO "The second argument passed in was %secondArgumentPassedIn%"
    ENDLOCAL
EXIT /B 0


:MAIN
call:firstMethodName "The Quick Brown" "Fox Jumps Over"
call:secondMethodName "1 2 3 4" 3.14
Run Code Online (Sandbox Code Playgroud)


Att*_*ila 6

您可以尝试使用此页面上列出的示例

或者,您可以将公共行放入另一个从主目录调用的批处理文件中


npo*_*aka 5

这是一个“黑客”,它允许您在批处理文件中拥有“匿名”功能

@echo off
setlocal 
set "anonymous=/?"

:: calling the anonymous function
call :%%anonymous%% a b c 3>&1 >nul

:: here the anonymous function is defined
if "%0" == ":%anonymous%" (
  echo(
  echo Anonymous call:
  echo %%1=%1 %%2=%2 %%3=%3
  exit /b 0
)>&3
::end of the anonymous function
Run Code Online (Sandbox Code Playgroud)

匿名功能块应该放在 call 语句之后,并且必须以 exit 语句结束

诀窍是在CALL内部使用GOTO然后返回到CALL执行的行。随着双重扩展 GOTO 帮助消息被触发(带%%/?%%参数),然后继续脚本。但完成后它返回到CALL- 这就是为什么需要 if 语句。