-1

I am executing command and parsing the command based on space, Formatting it and storing in a file.

This is how I have written the batch script

for /f "tokens=2,4,5" %G IN ('command') DO echo Id:%%G:~0,-1%  Timestamp:%H %I > C:\Users\TJ\Documents\out1.txt

I am getting the output like this

Id:3495068:~0,-1;  Timestamp:2023/01/25 14:57:18

But I am trying to trim the ";" semicolon but it's not trimmimg instead it's adding the trim logic to output.

I am expecting output like.

Id:3495068  Timestamp:2023/01/25 14:57:18

Is anything I am missing here.

  • Does this answer your question? [What is the best way to do a substring in a batch file?](https://stackoverflow.com/questions/636381/what-is-the-best-way-to-do-a-substring-in-a-batch-file) – hakre Jan 28 '23 at 14:52
  • I don't know the exact output of `command` - maybe just `"tokens=2,4,5 delims=; "` is enough to get rid of the semicolon. (If there is more than one, you may need to recount your tokens) – Stephan Jan 28 '23 at 14:55
  • Output of my command is this. transaction 3495068; promote; 2023/01/25 14:57:16 ; user: thejas – Thejas Jain M j Jan 28 '23 at 15:35
  • I am splitting based on ""(spaces). – Thejas Jain M j Jan 28 '23 at 15:37
  • so yes, splitting on `;` *and* space (as in my previous comment) will do what you want (no need to post-process the `;`). – Stephan Jan 28 '23 at 16:12

2 Answers2

0

You cannot substring a metavariable like %%G. You can only substring ordinary variables.

for /f "tokens=2,4,5" %G IN ('command') DO set "buildid=%G"&call echo Accurev Build Id:%buildid:~0,-1%  Timestamp:%H %I> C:\Users\TJainMJ\Documents\out1.txt&set "buildid="

should solve your problem, but it would be much better as a batch file where you don't have to retype the line every time you want to use it.

@ECHO OFF
SETLOCAL
for /f "tokens=2,4,5" %%G IN ('command') DO set "buildid=%%G"&>"u:\q75219686.txt" call echo Accurev Build Id:%%buildid:~0,-1%%  Timestamp:%%H %%I&set "buildid="
TYPE "u:\q75219686.txt"
GOTO :EOF

where "u:\q75219686.txt" is my destination file used for testing

Magoo
  • 77,302
  • 8
  • 62
  • 84
0

Consecutive delimiters are treated as one. You split by Spaces, if you just add the semicolon to the delimiters, every space, every semicolon and every combination of those will be treated as (one!) delimiter.

for /f "tokens=2,4,5 delims=; " %%G IN ('echo transaction 3495068; promote; 2023/01/25 14:57:16 ; user: thejas') DO echo Accurev Build ID:%%G  Timestamp:%%H 

Output:

Accurev Build ID:3495068  Timestamp:2023/01/25

Note: the space must be the last char in delims=, or you get a syntax error.

Stephan
  • 53,940
  • 10
  • 58
  • 91