Output to text file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
A_Bobby
Posts: 38
Joined: 21 Oct 2010 12:48

Output to text file

#1 Post by A_Bobby » 03 Jan 2011 15:59

Hi,

I am trying to output results of a for loop to a text file. Here's the command

C:\>for /f "delims=:" %i in ('certutil -v -store My ^| findstr /i /n "_ID(20)"') do echo %i >>tmp.txt

Easy enough... i get my line numbers matching the string to tmp.txt. The output in tmp.txt looks like
135
330
479
660
818
970

What I want instead is to add 1 number and display the output as

136
331
480
661
819
971

So tried this

for /f "delims=:" %i in ('certutil -v -store My ^| findstr /i /n "_ID(20)"') do set /a n=%i+1 >>tmp.txt

but my output is 136331480661819971 in the tmp.txt file. Either of the following two are acceptable
space delimited 136 331 480 661 819 971 or

136
331
480
661
819
971

Plz help. Thanks a bunch in advance.

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Output to text file

#2 Post by jeb » 03 Jan 2011 17:31

Hi A_Bobby,

this should work

Code: Select all

for /f "delims=:" %i in ('certutil -v -store My ^| findstr /i /n "_ID(20)"') do ( set /a n=%i+1 & echo( ) >>tmp.txt


The main problem was, that the output of the SET command is always without line end (CR/LF).

hope it helps
jeb

A_Bobby
Posts: 38
Joined: 21 Oct 2010 12:48

Re: Output to text file

#3 Post by A_Bobby » 04 Jan 2011 10:54

Thanks for that Jeb. It worked. I am also trying to output the resulting line content to a text file. Is that possible with a single for loop?

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Output to text file

#4 Post by jeb » 04 Jan 2011 12:43

Hmmm ?

You redirect it to a file (tmp.txt), so I don't understand what your question is?

A_Bobby
Posts: 38
Joined: 21 Oct 2010 12:48

Re: Output to text file

#5 Post by A_Bobby » 04 Jan 2011 13:58

My bad!

Here's my issue. I am trying to capture multiple lines from an output of a command. The command is certutil -v -store My. Basically its dumping the entire certificate store information and what I am trying to capture are SKID (subject key identifiers) values. The skid values are represented by string "_ID(20)" but the skid value itself is on the next line, hence set /a n=%i+1. Below is part of the the output that I am interested in.

CERT_KEY_IDENTIFIER_PROP_ID(20): -Line number 135
a0 18 d2 cd c5 c3 ce ac be ae 78 dd 02 58 ef 65 c9 f7 5a 72 -Line number 136

What I am interested in is finding and isolating the string _ID(20):, adding one to that variable and use that variable to dump that line to another output file using best way possible. So I was first trying to create a tmp.txt file with line numbers and then using that to dump corresponding lines to another text file.

Hope this clarifies it. Thanks again for looking at it.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Output to text file

#6 Post by ghostmachine4 » 04 Jan 2011 23:38

get a good file parsing tool, like gawk for windows.

Code: Select all

C:\test> cert -v .....  | gawk "/CERT.*_ID\(20\)/{getline;print}" 

A_Bobby
Posts: 38
Joined: 21 Oct 2010 12:48

Re: Output to text file

#7 Post by A_Bobby » 10 Jan 2011 14:25

Using gawk is not an option. I made some progress and I am able to get the lines using the following script

First I dump contents of cert stores in dump file with line numbers using this command

certutil -v -store My | findstr /v /n 1362322 >>dump.txt

Then I create a temp file with the line numbers that I am interested in using the following command that you guys fixed for me

for /f "delims=:" %i in ('certutil -v -store My ^| findstr /i /n "_ID(20)"') do ( set /a n=%i+1 & echo( ) >>tmp.txt

Then I use this command to output the lines that I need

for /f %i in (tmp.txt) do findstr %i: dump.txt

It gives me the output like following
C:\>findstr 136: dump.txt
136: 5a 4b fb 38 d3 e9 b3 17 06 04 be 73 5a b9 f0 69 76 61 00 19

C:\>findstr 331: dump.txt
331: d7 9b 7c d8 22 a0 15 f7 dd ad 5f ce 29 9b 58 c3 bc 46 00 b5

C:\>findstr 480: dump.txt
480: 76 8e fd e4 e3 e7 a3 fe eb 6a 0a 95 a3 6d 2a 74 d0 79 31 06

What is unwanted in this output is the line numbers at the beginning and spaces inbetween characters. I would like to get the output like

C:\>findstr 136: dump.txt
5a4bfb38d3e9b3170604be735ab9f06976610019

C:\>findstr 331: dump.txt
d79b7cd822a015f7ddad5fce299b58c3bc4600b5

C:\>findstr 480: dump.txt
768efde4e3e7a3feeb6a0a95a36d2a74d0793106

Is it possible to do that?

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Output to text file

#8 Post by jeb » 10 Jan 2011 14:53

You could put it into a batch file

myFind.bat

Code: Select all

@echo off
for /F "usebackq delims=" %%a in (`findstr %1 %2`) DO (
  set "line=%%a"
)
REM ** remove the line number, all characters to the first ":"
set "line=%line:*:=%"
REM ** remove all spaces
set "line=%line: =%"
echo %line%


myFind 331 dump.txt

A_Bobby
Posts: 38
Joined: 21 Oct 2010 12:48

Re: Output to text file

#9 Post by A_Bobby » 11 Jan 2011 12:26

Thanks Jeb. This works but I have run into another problem. I am trying to put everything into one batch file but the following command

for /f "delims=:" %i in ('certutil -v -store My ^| findstr /i /n "_ID(20)"') do ( set /a n=%i+1 & echo( ) >>tmp.txt

that you fixed for me is dumping blank echo's into the tmp.txt file. It works fine when I run it from command line but doesn't work within batch file.

ChickenSoup
Posts: 79
Joined: 13 Dec 2010 10:32

Re: Output to text file

#10 Post by ChickenSoup » 11 Jan 2011 13:40

In batch file, you need %%i in for loops. So:

Code: Select all

for /f "delims=:" %%i in ('certutil -v -store My ^| findstr /i /n "_ID(20)"') do ( set /a n=%%i+1 & echo( ) >>tmp.txt

A_Bobby
Posts: 38
Joined: 21 Oct 2010 12:48

Re: Output to text file

#11 Post by A_Bobby » 11 Jan 2011 14:08

I know that but I am getting the blank output even with %%. If I don't use double %, the error is different... %i was unexpected at this time.

A_Bobby
Posts: 38
Joined: 21 Oct 2010 12:48

Re: Output to text file

#12 Post by A_Bobby » 13 Jan 2011 09:42

Hello Experts,

Any update on if this is possible? Please reply regardless.

Thanks

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Output to text file

#13 Post by jeb » 14 Jan 2011 03:29

Ok it is tricky.

On the command line the set /a echo's also the result, but in the batch file it is quite.
So you have to output explicit.

Code: Select all

setlocal EnableDelayedExpansion
for /f "delims=:" %%i in ('certutil -v -store My ^| findstr /i /n "_ID(20)"') do (
    set /a n=%%i+1
    echo !n!
) >>tmp.txt


hope it helps
jeb

A_Bobby
Posts: 38
Joined: 21 Oct 2010 12:48

Re: Output to text file

#14 Post by A_Bobby » 14 Jan 2011 14:27

Great! That worked. Thanks a lot Jeb. This little script will save me a at least a 100 man hours!

Post Reply