Something like a function/method in batch files?

后端 未结 9 430
忘了有多久
忘了有多久 2020-12-08 00:05

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 insid

9条回答
  •  不思量自难忘°
    2020-12-08 00:47

    Coming from a Java background, I have tried to incorporate some familiar conventions when creating procedures for .bat scripts.

    The script below demonstrates the definition of two procedures.

    @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
    

    Notice that an explicit GOTO:MAIN is necessary to skip over the procedure definitions. This is because you must skip over the procedure before deciding to read it. Otherwise, the procedure will be executed.

    The code below demonstrates a close Java-equivalent of the above .bat script.

    public class MyObject {
        private String firstInstanceVariable = "Hello world!";
        private String secondInstanceVariable = "Good bye world!";
        public void firstMethodName(Object... arguments) {
            String firstArgumentPassedIn = arguments[0].toString();
            String secondArgumentPassedIn = arguments[1].toString();
            System.out.println(firstInstanceVariable);
            System.out.format("The first argument passed in was %s", firstArgumentPassedIn);
            System.out.format("The second argument passed in was %s", secondArgumentPassedIn);
        }
    
        public void secondMethodName(Object... arguments) {
            String firstArgumentPassedIn = arguments[0].toString();
            String secondArgumentPassedIn = arguments[1].toString();
            System.out.println(secondInstanceVariable);
            System.out.format("The first argument passed in was %s", firstArgumentPassedIn);
            System.out.format("The second argument passed in was %s", secondArgumentPassedIn);
        }
    
        public static void main(String[] args) {
            MyObject myObject = new MyObject();
            myObject.firstMethodName("The Quick Brown", "Fox Jumps Over");
            myObject.secondMethodName(new Integer[]{1,2,3,4}, 3.14);
        }
    }
    

提交回复
热议问题