\
Tips and Tricks
Batch Files
Batch File Tips and Tricks
Subroutines (common code)
Subroutines (common code) |
The following is a common way of having some common code
(parameters are passed as environment variables):
...
set ReturnAddress=Return1
goto SomeSubroutine
:Return1
set ReturnAddress=Return2
goto SomeSubroutine
:Return2
...
goto :EOF
::::::::::::::::::::::::::
:SomeSubroutine
::::::::::::::::::::::::::
echo *** In Subroutine ***
goto %ReturnAddress%
A newer method to do the same thing is:
...
call :SomeSubroutine
call :SomeSubroutine
...
goto :EOF
::::::::::::::::::::::::::
:SomeSubroutine
::::::::::::::::::::::::::
echo *** In Subroutine ***
goto :EOF
Note that you may wish to read/write to an environment variable you
have the name for, if so have a look at the
Indirect Access to Environment Variables (by reference)
tip.
@echo off
setlocal
set BatchFileDir=%~dp0
cls
CALL :FIND_EXE "Exiv2Exe" "exiv2.exe"
if "%Exiv2Exe%" == "" echo "exiv2.exe" not found
if not "%Exiv2Exe%" == "" echo "exiv2.exe" found full name: "%Exiv2Exe%"
goto :EOF
@rem ++++++++++++++++++++++++++ v11.366
:FIND_EXE
@rem ++++++++++++++++++++++++++
set Look4Exe=%~2
set FndExe=
if exist "%BatchFileDir%%Look4Exe%" set FndExe=%BatchFileDir%%Look4Exe%
if "%FndExe%" == "" if exist "%Look4Exe%" for %%f in ("%Look4Exe%") do set FndExe=%%~ff
if "%FndExe%" == "" for %%f in ("%Look4Exe%") do set FndExe=%%~$PATH:f
set %~1=%FndExe%
goto :EOF