Batch file string manipulation -


this specific question, however;

say have batch file running from\located in directory c:\data\src\branch_1

how set environment variable %buildpath% c:\build\bin\branch_1 in batch file?

(to clear, if same batch file located in c:\foo\bar\branch_2 want set %buildpath% c:\build\bin\branch_2)

you should able use environment variable %~dp0 drive , path of batch file running. there, it's not-very-efficient method of stripping off end of string character character , building new string.

for example, 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 

running c:\data\src\branch1\qq.cmd returns following:

current directory is: c:\data\src\branch1\ new directory is: c:\build\bin\branch1 

as how works, can use !xyz:~n,m! doing substring of environment variable, , negative m or n means end rather beginning. first if block strips off trailing \ if it's there.

the loop similar transfers characters end of path new variable, until point find \. have last bit of path, , it's simple matter append fixed new path.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -