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:
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?
A function is called in Batch Script by using the call command. Functions can work with parameters by simply passing them when a call is made to the function. Local variables in functions can be used to avoid name conflicts and keep variable changes local to the function.
For simple batch files, a single character such as %%f will work. You can use multiple values for variable in complex batch files to distinguish different replaceable variables.
batch-file Echo @Echo off @echo off prevents the prompt and contents of the batch file from being displayed, so that only the output is visible. The @ makes the output of the echo off command hidden as well.
%%a refers to the name of the variable your for loop will write to. Quoted from for /? : FOR %variable IN (set) DO command [command-parameters] %variable Specifies a single letter replaceable parameter. (set) Specifies a set of one or more files. Wildcards may be used.
You could use the call command :
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With