I want to get output in simple txt file inside git repo folder:
About:
date = (2022_06_04)
version = (1d34a1b1)
And I stuck with formatting data in ( ) brackets.
Here is my attempt:
@echo off
set dt=%DATE:~6,4%_%DATE:~3,2%_%DATE:~0,2%%
set dt=%dt: =0%
set wrap=About:
set file_name="vers.txt"
set git_cmd=git describe --abbrev=8 --always
echo %wrap% >%file_name%
echo.date = (%dt%)>>%file_name%
echo|set /p ="version = (" >>%file_name%
%git_cmd% >>%file_name%
echo.) >>%file_name%
%NL%>>%file_name%
Problem is with ')' for git output formatting. Here is what I'm getting:
About:
date = (2022_06_04)
version = (1d34a1b1
)
I've tried to play with echo -n, but it doesn't work on Windows.
UPDATE
NL is just new line:
set NL=echo.
Here is example of command line with whole git command output:
USR@DESKTOP-N1 MINGW64 /c/project/prj (v1.1.2)<CR><LF>
$ git describe --abbrev=8 --always<CR><LF>
1d34a1b1<CR><LF>
<CR><LF>
USR@DESKTOP-N1 MINGW64 /c/project/prj (v1.1.2)<CR><LF>
And here is how my output text file looks like:
About:<CR><LF>
date = (2022_06_06)<CR><LF>
version = (1d34a1b1<LF>
) <CR><LF>
Seems <LF> appeared at some stage. For me it looks like <CR> was cutted but <LF> stayed because of some Windows standard formatting or something like that, I'm no sure.
Two possibilities leap to mind
and
but I've no
gitapplications, so this is a guess...This worked for me to give the output desired:
Noting:
My
%date%format is YYYYMMDDI've removed the
%NL%...line.NLis not defined in the original post. I'd guess it's supposed to add a new line.q72496859sis a batch file that simply responds1d34a1b1on a single line.I've no idea what
gitactually generates - does it contain empty lines or perhaps<CR><LF><CR>sequences?It's SOP for me to use
set "var=value"for setting string values - this avoids problems caused by trailing spaces and not assign a terminal\, Space or"- but build pathnames from the elements - counterintuitively, it is likely to make the process easier.Using these principles, this batch becomes
Edit to second version of above.
for /f "delims=" %%e in ('%git_cmd% ^|findstr /v "/ -"') do echo version = (%%e)>>"%file_name%"
I set
q72496859sto deliver thegitresponse described.The pipe needs to be escaped (by a caret) to tell
cmdthat it's part of the command-to-be-executed, not of the actualforcommand.The
findstrignores empty lines and reports those lines that do not (/v) contain either/or-- the/needs to be escaped (using backslash this time), which should isolate the required data.The result I obtained was