0

I am trying to make a batch file which is required to:

  1. run git describe and store the output in a file.txt;
  2. modify the content of file either in the same or other file, e.g. v1.0 as "v1.0";
  3. or just simply add " before and after the v1.0 in the same file;

I have written a batch file but it is not giving output according to requirement. Here is what I have written:

cd C:path to project directory
set /p=^"<nul > file.txt
start cmd.exe /k "git describe >>"file.txt" & exit"
set /p= ^"<nul >> file.txt

And the output is v2.9 although I want "v2.9". Why lines 2 and 4 are not working?

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Learner
  • 67
  • 7
  • 1
    Why are you using `start` and `cmd /K` to run `git describe`? I think you can remove both... – aschipfl Dec 29 '16 at 09:57
  • 3
    I do not understand the 2 down votes or the close vote. The OP stated the overall goal, provided existing code, and asked a specific programming question about one aspect of the code that is not working. All perfectly reasonable to me, and exactly what I thought we are looking for in this community. – dbenham Dec 29 '16 at 11:21

2 Answers2

2

You could also consider writing an actual bash script (even on Windows): name it git-descr (no extension), save it in your %PATH%, and call it with the command git descr: any git-xxx script in %PATH% will be executed by the git bash.

Adding double-quotes is then easy.

#!/bin/bash
cd /c/path/to/local/repo
d=$(git describe)
echo "\"${d}\"">file.txt

Again, this will work on Windows.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

I'm pretty sure git will include a newline in the output, so even if you got the SET /P to print your ", your output would be:

"v1.0
"

I believe the simplest solution to your problem is to capture the output of your git command with FOR /F, and then write your quoted value within the loop.

@echo off
cd /d "c:path to project directory"
for /f "delims=" %%A in ('git describe') echo "%%A" >file.txt



Below are some issues with your posted code:

You wanted to launch git with START, and then exit. You used the /K option, which keeps the the new cmd session active after the command executes, so you were forced to append the EXIT command. Much simpler to just use the /C option instead

start cmd.exe /c "git describe >>"file.txt""

But there is no need for START at all. You can simply run the command directly from the current session

git describe >>"file.txt"

You attempted to use SET /P to print out a single " without a newline. This failed because SET /P has some odd rules for how it handles leading quotes, white space, and =.

Either of the following could be used to redirect output of a single " to a file:

<nul >file.txt set /p ="""
<nul >file.txt set /p "=""""

I put the redirection in the front so you don't have to worry about the odd quote turning the redirection into literal strings. If you put the redirection at the end, then you must escape one of the quotes.

set /p =^""" <nul >file.txt
set /p =""^" <nul >file.txt
set /p ^"="""" <nul >file.txt
set /p "="^""" <nul >file.txt
set /p "="""^" <nul >file.txt
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • Thanks for a detailed explanation. I had a tough time to understand the { for /f "delims=" %%A in ('git describe') echo "%%A" >file.txt }. what is the purpose of "/f" after for keyword. How it affects the process? – Learner Dec 30 '16 at 10:07
  • @Learner- `FOR /F ... IN ('command') DO` iterates the results of command, line by line. The `"delims="` sets the token delimiter to nothing so the entire line is preserved. Use `help for` or `for /?` from the command prompt to see the documentation. – dbenham Dec 30 '16 at 10:12