You should be able to use the environment variable %~dp0to get the drive and path to the executable file. From there, this is not a very effective method of trimming the end of this line character with a character and building a new line.
For example, a batch file:
@setlocal enableextensions enabledelayedexpansion
@echo off
set olddir=%~dp0
echo Current directory is: !olddir!
if "!olddir:~-1!" == "\" (
set olddir=!olddir:~0,-1!
)
set lastbit=
:loop
if not "!olddir:~-1!" == "\" (
set lastbit=!olddir:~-1!!lastbit!
set olddir=!olddir:~0,-1!
goto :loop
)
set newdir=c:\build\bin\!lastbit!
echo New directory is: !newdir!
endlocal
c:\data\src\branch1\qq.cmd :
Current directory is: C:\data\src\branch1\
New directory is: c:\build\bin\branch1
, , !xyz:~n,m! , m n - , . if \, .
, , , \. , , , .