Batch file how to call another batch file at a specified label or call and immediately goto a certain label?

前端 未结 5 1440
不知归路
不知归路 2021-01-02 00:57

I am trying to figure out how file1.bat can call file2.bat at a specified label.

I figured I can do it like this:

File1.bat

:config
         


        
5条回答
  •  忘掉有多难
    2021-01-02 01:54

    I just created a script that might prove useful to you. I think it's strength is that you don't have to modify your called script in any way. I'm sure with a bit of time you can perfect it and make it fit your purpose. I also can figure that it isn't fast enough for production use when the called script is large. This method consists on placing my script to the folder of your own scripts, and using it like so:

    EDIT

    Now this script works from any directory and can be input with either full or relative paths to the called script, with or without file extension (C:\scripts\script, C:\scripts\script.bat, .\scripts\script.cmd, etc)

    Usage:

    Given a sample script script.bat:

    :LABEL_1
    ECHO This is label number one!
    GOTO :EOF
    
    :LABEL_2
    ECHO This is label number two!
    GOTO :EOF
    

    The following input:

    CALLAT script :LABEL_2
    

    Should output:

    This is label number two!
    

    Source code of CALLAT.bat:

    @ECHO OFF
    SETLOCAL EnableDelayedExpansion
    @ECHO OFF
    
    rem Create a folder in the temporary files directory
    MKDIR "%TEMP%\CALLAT\"> NUL
    
    rem Chose a name for a temporal script
    SET TEMP_SCRIPT="%TEMP%\CALLAT\temp-script.bat"
    
    rem Get the full path to the called script
    FOR /F "delims=" %%F IN ('DIR /B /S "%1*" 2^>NUL') DO SET ORIGINAL_SCRIPT=%%F
    
    rem Add this lines to go directly to the desired label in the temp script
    ECHO @ECHO OFF> %TEMP_SCRIPT%
    ECHO GOTO %2>> %TEMP_SCRIPT%
    
    rem Concatenate the called script to the end of the temp script
    COPY /B %TEMP_SCRIPT%+%ORIGINAL_SCRIPT% %TEMP_SCRIPT%>NUL
    
    rem Call the modified script
    CALL %TEMP_SCRIPT%
    
    rem Clean up
    RD "%TEMP%\CALLAT\" /S /Q>NUL
    
    ENDLOCAL
    

提交回复
热议问题