Page 1 of 1

Need Help: CALL subroutine in Batch dont worl like expected

Posted: 16 Mar 2024 11:20
by Chronofos
Hi1

I dont understand why my bachtfile dont work.

-----BATCH FILE -----
@ECHO OFF
CLS

ECHO AUSGABE "Normal"
ECHO %1
ECHO %~d1
ECHO %~p1
ECHO %~n1
ECHO %~x1
ECHO Zusammengesetzt
ECHO %~d1%~p1%~n1%~x1
ECHO.

CALL :get_drive_path_name_extension_from_file

GOTO :EOF

:get_drive_path_name_extension_from_file
ECHO AUSGABE via "call"
ECHO %1
ECHO %~d1
ECHO %~p1
ECHO %~n1
ECHO %~n1
ECHO Zusammengesetzt
ECHO %~d1%~p1%~n1%~x1
GOTO :EOF
-------------------------

----- RESULT -----
AUSGABE "Normal"
textfile.txt
O:
\
textfile
.txt
Zusammengesetzt
O:\textfile.txt

AUSGABE via "call"
ECHO ist ausgeschaltet (OFF).
ECHO ist ausgeschaltet (OFF).
ECHO ist ausgeschaltet (OFF).
ECHO ist ausgeschaltet (OFF).
ECHO ist ausgeschaltet (OFF).
Zusammengesetzt
ECHO ist ausgeschaltet (OFF).
---------------------

Re: Need Help: CALL subroutine in Batch dont worl like expected

Posted: 16 Mar 2024 15:11
by OJBakker
You are using the first parameter supplied to your subroutine in your subroutine.
But in the main batchfile you are not calling the subroutine with a parameter.

Re: Need Help: CALL subroutine in Batch dont worl like expected

Posted: 17 Mar 2024 00:39
by Chronofos
Hi!

Thanks for your answer!

This was one of my errors. I have found one more and the batchfile rewritten. Now it works.

Code: Select all

@ECHO OFF
CLS

ECHO ------------------------------------------------------------------
ECHO Output "Normal"
ECHO ------------------------------------------------------------------
ECHO %0
ECHO %1
ECHO ------------------------------------------------------------------
ECHO %~d1
ECHO %~p1
ECHO %~n1
ECHO %~x1
ECHO Concat
ECHO %~d1%~p1%~n1%~x1
ECHO.
REM ********************************************************************************************
REM * CALL must be called with parameters!                                                     *
REM * But here the parameters are renumbered!                                                  *
REM *                                                                                          *
REM * The parameters are passed:                                                               *
REM ™   %0 = name of batch file                                                                *
REM ™   %1 = name oftext file                                                                  *
REM *                                                                                          *
REM * But, because the CALL command was originally there to call another BATCH file            *
REM * and first later in the sense of calling a subroutine has been extended,                  *
REM * the semantics of call remained the same                                                  *
REM *                                                                                          *
REM * Essentially, another batch file is called here:                                          *
REM * get_drive_path_name_extension_from_file.bat with the parameters %1 %2                    *
REM *                                                                                          *
REM * However, this call redistributes the parameters!                                         *
REM ™   %0 = subroutine name                                                                   *
REM =   %1 = the "previous" %0 = Batchfile name                                                *
REM ™   %2 = the "previous  %1 = Texfile name                                                  *
REM ********************************************************************************************
CALL :get_drive_path_name_extension_from_file %0 %1
GOTO :EOF

:get_drive_path_name_extension_from_file
ECHO ------------------------------------------------------------------
ECHO Output via "call"
ECHO ------------------------------------------------------------------
ECHO %0
ECHO %1
ECHO %2
ECHO ------------------------------------------------------------------
ECHO %~d2
ECHO %~p2
ECHO %~n2
ECHO %~x2
ECHO Concat
ECHO %~d2%~p2%~n2%~x2
GOTO :EOF