I know that I can call another batch file using call path_to_other_batch_file.bat
.
However, I don't know how can I call functions inside that file.
I have this batch file called Message.bat
:
@echo off
EXIT /B %ERRORLEVEL%
:Error
echo [31m %* [0m
EXIT /B 0
:Warning
echo [33m %* [0m
EXIT /B 0
:Info
echo [34m %* [0m
EXIT /B 0
:Success
echo [32m %* [0m
EXIT /B 0
:Reset
echo [37m %* [0m
EXIT /B 0
And I want to use these functions in my other batch files so that I can simply write call:Error something went wrong
without worrying about colors all the time.
I use it this way in Other.bat
, but it does not work:
call C:\Infra\Message.bat
call:Error something went wrong
I receive this error:
The system cannot find the batch label specified - Error
So, how can I call those methods defined in my Message.bat
file?
CodePudding user response:
There's no built-in way to do that. call
will either call an external file, an internal command or a label in the current file.
But, if you can change message.bat
, you can make it take an additional argument, and call
it. Then call
will search in its own labels.
You'll have to take extra care to not pass the first argument to the label. For that, you can use the code from this answer:
@echo off
set "fn=%1"
shift
::"exit /b" is the same as "exit /b %errorlevel%"
if "%fn%"=="" exit /b
set "line=%1"
:loop
shift
if not "%1"=="" (
set "line=%line% %1"
goto :loop
)
call :%fn% %line%
exit /b
:Error
echo [31m %* [0m
EXIT /B 0
:Warning
echo [33m %* [0m
EXIT /B 0
:Info
echo [34m %* [0m
EXIT /B 0
:Success
echo [32m %* [0m
EXIT /B 0
:Reset
echo [37m %* [0m
EXIT /B 0
Then, you can call it like:
call message.bat error Something went wrong
CodePudding user response:
In your main batch(es)
call message error something went wrong
In message
@echo off
goto %1
:error
for /f "tokens=1*" %%L in ("%*") do echo %%M
exit /b 0
really not that hard...
or, better in message.bat
@echo off
FOR /f "tokens=1*" %%L IN ("%*") DO CALL :%%L %%M
EXIT /B %ERRORLEVEL%
:Error
echo [31m %* [0m
EXIT /B 0
:Warning
...
CodePudding user response:
Labels can sometimes become cluttered if you have multiple options. It is good for multiple tasks if some meets the criteria, but you seem to only want to change colors on events. So I will simply say do not use labels at all. The content of message.bat
add:
@echo off
for /F %%a in ('echo prompt $E ^| cmd') do set "e=%%a"
if "%1" == "" exit /b 1
set "line=%*"
set "label=%1"
call set "rest=%%line:%label% =%%"
if /i "%1" == "Error" set "severity=[31m%rest%"
if /i "%1" == "Warning" set "severity=[33m%rest%"
if /i "%1" == "Info" set "severity=[34m%rest%"
if /i "%1" == "Success" set "severity=[32m%rest%"
if /i "%1" == "Reset" set "severity=[37m%rest%"
echo %e%%severity%%e%[0m
exit /b 0
To call this from another batch, simply do:
call messages.bat warning something went wrong
There is an odd chance that you want to add the actual severities into the message as well, and not just change the text color, then simply narrow it down to:
@echo off
for /F %%a in ('echo prompt $E ^| cmd') do set "e=%%a"
if "%1" == "" exit /b 1
if /i "%1" == "Error" set "severity=[31m%*"
if /i "%1" == "Warning" set "severity=[33m%*"
if /i "%1" == "Info" set "severity=[34m%*"
if /i "%1" == "Success" set "severity=[32m%*"
if /i "%1" == "Reset" set "severity=[37m%*"
echo %e%%severity%%e%[0m
exit /b 0