Bulk file line processing

This is a very specific question, however,

Say I have a batch file running from \ located in a directory c:\data\src\branch_1

How to set environment variable %buildpath%in c:\build\bin\branch_1in batch file?

(To be more clear, if the same batch file is in c:\foo\bar\branch_2, I want it to set %buildpath%to c:\build\bin\branch_2)

+3
source share
2 answers

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 \, .

, , , \. , , , .

+6

, ...

.

@echo off
cd > dir.tmp
set /p directory= <dir.tmp
echo %directory%    <-- do whatever you want to the variable. I just did a echo..
del dir.tmp > nul
+1

Source: https://habr.com/ru/post/1790905/


All Articles