batch-filecallspacesubroutine

Call subroutine inside another batch file that has spaces in the filename?


I've been looking at some examples on how to call a subroutine inside another batch file (i.e. Call a subroutine in a batch from another batch file), but somehow the external batch file is executed in the regular way i.e. from beginning to end, with the called function ignored/skipped.

I think it has to do something with the spaces in the external batch file:

:: main batch file.cmd where the subroutine is called from
CALL "second batch file.cmd" :SUBROUTINE
goto :EOF

and:

:: second batch file.cmd contents

ECHO IGNORE THIS
goto :EOF

:SUBROUTINE
ECHO SUBROUTINE CALLED FROM 'main batch file.cmd'!
goto :EOF

Isn't this supposed to work?


Solution

  • Enter as the first line of the second batch file:

    IF "%1"==":SUBROUTINE" GOTO :SUBROUTINE