windows 类似于批处理文件中的函数/方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10149194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Something like a function/method in batch files?
提问by stefan.at.wpf
is there anything that mimicks a method like one knows it from Java, C# etc.? I have 5 lines of commands in a batch file, those 5 lines are used at more than one place inside the batch file. I can't use a goto, because depending on the errorlevel created by those 5 lines I have different actions that follow. I tried putting my 5 lines inside a batch file 5lines.bat, but the original batch file original.bat only calls 5lines.bat and doesn't execute the commands after the call to 5lines.bat ): That's how my original.bat looks like:
有什么东西可以模仿从 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?
There's no exit or something like this in 5lines.bat! How can I make sure the line after 5lines.bat gets called?
5lines.bat 中没有出口或类似的东西!如何确保调用 5lines.bat 之后的行?
采纳答案by Cody Gray
Placing the reusable functions into a separate batch file would certainly work to simulate a function.
将可重用函数放入单独的批处理文件中肯定可以模拟函数。
The catch is that you have to use the call
command in order to ensure that control returns to the caller after the second batch file finishes executing.
问题是您必须使用该call
命令来确保在第二个批处理文件执行完毕后控制权返回给调用者。
call 5lines.bat
echo this will now get called
回答by Erwald
You could use the call command :
您可以使用 call 命令:
call:myDosFunc
And then define the function this way :
然后以这种方式定义函数:
: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
Source : Batch Functions
来源:批处理函数
回答by Shital Shah
Just for completeness, you can also pass parameters to the function:
为了完整起见,您还可以将参数传递给函数:
Function call
函数调用
call :myDosFunc 100 "string val"
Function body
函数体
:myDosFunc
echo. Got Param#1 %~1
echo. Got Param#2 %~2
goto :eof
回答by DmitrySemenov
Solution:
解决方案:
@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
Important to put EXIT /B at the end of each function, as well as before function definitions start, in my example this is:
将 EXIT /B 放在每个函数的末尾以及函数定义开始之前很重要,在我的示例中,这是:
EXIT /B %ERRORLEVEL%
退出 /B %ERRORLEVEL%
回答by Attila
回答by npocmaka
Here's a 'hack' that will allow you to have "anonymous" functionsin batch files:
这是一个“黑客”,它允许您在批处理文件中拥有“匿名”功能:
@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
The anonymous function block should be placed right after the call statement and must end with exit statement
匿名功能块应该放在 call 语句之后,并且必须以 exit 语句结束
the trick is that CALL
internally uses GOTO
and then returns to the line where the CALL
was executed. With the double expansion GOTO help message is triggered (with %%/?%%
argument) and then continues the script. But after it is finished it returns to the CALL
- that's why the if statement is needed.
诀窍是在CALL
内部使用GOTO
然后返回到CALL
执行的行。随着双重扩展 GOTO 帮助消息被触发(带%%/?%%
参数),然后继续脚本。但是完成后它返回到CALL
- 这就是为什么需要 if 语句。
回答by pasha
I'm not sure if it was obvious from other answers but just to be explicit I'm posting this answer. I found other answers helpful in writing below code.
我不确定其他答案是否显而易见,但为了明确起见,我发布了这个答案。我发现其他答案有助于编写下面的代码。
echo what
rem the third param gives info to which label it should comeback to
call :myDosFunc 100 "string val" ComeBack
:ComeBack
echo what what
goto :eof
:myDosFunc
echo. Got Param#1 %~1
echo. Got Param#2 %~2
set returnto=%~3
goto :%returnto%
回答by jwfearn
For another great tutorial on writing reusable batch file code -- see Richie Lawrence's excellent library.
有关编写可重用批处理文件代码的另一个很棒的教程——请参阅Richie Lawrence 的优秀库。