Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Something like a function/method in batch files?

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?

like image 944
stefan.at.wpf Avatar asked Apr 13 '12 22:04

stefan.at.wpf


People also ask

How do you call a method in a batch file?

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.

What is %% f in batch file?

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.

What is @echo off in batch script?

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.

What does %% A mean in batch file?

%%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.


1 Answers

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

like image 174
Erwald Avatar answered Oct 06 '22 12:10

Erwald