Send certutil output directly to a variable instead of file, and then finally copy to clipboard?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
koko
Posts: 38
Joined: 13 Oct 2016 00:40

Send certutil output directly to a variable instead of file, and then finally copy to clipboard?

#1 Post by koko » 13 Aug 2019 04:19

I've been considering creating a dataURI batch script to make inserting data of files (probably exclusively images) more easily into text editors for web purposes. I discovered that certutil offers the function to base64 encode a file, which is the first step (btw it seems the findstr command in that link only writes to a secondary file to strip the 'Begin Certificate' first line of certutil's output).

Is there a way to instead output certutil's base64 result to a variable so it can be further manipulated by the batch script (trimming the 'Begin Certificate' and adding the appropriate dataURI prefix)? And additionally is there a known way to copy the final variable to the clipboard?

Perhaps I haven't found the right combination of words to search for but with my queries on DuckDuckGo and on this site I couldn't find results that were relevant. Is what I'm looking to achieve possible?


aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Send certutil output directly to a variable instead of file, and then finally copy to clipboard?

#3 Post by aGerman » 13 Aug 2019 10:45

Nope. Certutil will always write the encoded content to a file. Furthermore URIs don't have newlines and the string length in Batch is restricted. Eventually you need at least two temporary files.

Code: Select all

@echo off &setlocal

:: file to be encoded
set "file=reddot.png"
:: begin of the URI
set "uribegin=data:image/png;base64,"

:: this is only to create reddot.png as an example; once you have it, you can remove these 3 lines
>"reddot.tmp~" echo iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==
>nul certutil.exe -f -decode "reddot.tmp~" "%file%"
del "reddot.tmp~"

:: write the begin of the URI to a temporary file
<nul >"uri.tmp~" set /p "=%uribegin%"
:: decode the png to base64
>nul certutil.exe -f -encode "%file%" "b64.tmp~"
:: read the base64 file and append the code to the temporary file without any newline
for /f %%i in ('findstr.exe /vbc:"---" "b64.tmp~"') do <nul >>"uri.tmp~" set /p "=%%i"
:: save the content of the temporary file in the clipboard
type "uri.tmp~"|clip.exe
::clean up
del "b64.tmp~" "uri.tmp~"
Steffen

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Send certutil output directly to a variable instead of file, and then finally copy to clipboard?

#4 Post by dbenham » 14 Aug 2019 05:58

As aGerman said, you cannot avoid creating a file. But if your goal is to put the result on the clipboard, then there is no need for an environment variable. You can simply TYPE the output file and pipe it into the CLIP utility.

Also, the -encodehex verb has options that allow you to create base64 using different formats. See viewtopic.php?f=3&t=8521 and viewtopic.php?t=8521#p57918

For example, the following will encode the file specified as the first argument (%1) using base64 url format, on a single line without any line terminator, header, or footer. The result is placed on the clipboard, and the temp file deleted.

Code: Select all

@echo off
certutil -encodehex -f %1 %1.64 0x4000000D
type %1.64 | clip
del %1.64

Dave Benham

koko
Posts: 38
Joined: 13 Oct 2016 00:40

Re: Send certutil output directly to a variable instead of file, and then finally copy to clipboard?

#5 Post by koko » 15 Aug 2019 22:13

aGerman wrote:
13 Aug 2019 10:45
Nope. Certutil will always write the encoded content to a file. Furthermore URIs don't have newlines and the string length in Batch is restricted. Eventually you need at least two temporary files.
dbenham wrote:
14 Aug 2019 05:58
Also, the -encodehex verb has options that allow you to create base64 using different formats. See viewtopic.php?f=3&t=8521 and viewtopic.php?t=8521#p57918

For example, the following will encode the file specified as the first argument (%1) using base64 url format, on a single line without any line terminator, header, or footer. The result is placed on the clipboard, and the temp file deleted.
Sorry for the delayed reply. Thanks to you both! I think I can live with the temp file creation. Also great that certutil can output without the header, though it didn't appear to work with '0x4000000D' so I changed it to '1' instead. Allowed me to simply concat the two files together using copy thanks to that feature.

Btw I came across a comment on this SE answer, while looking at ways to strip new lines from text files, which seems to suggest that parsing text files with for loops only supports up to a max of 8191 characters due to a limitation of environment variables. Does what they're saying also apply to for loops using findstr or am I misunderstanding the context of the comment?

Eureka!
Posts: 137
Joined: 25 Jul 2019 18:25

Re: Send certutil output directly to a variable instead of file, and then finally copy to clipboard?

#6 Post by Eureka! » 04 Sep 2019 15:37

aGerman wrote:
13 Aug 2019 10:45

Code: Select all

type "uri.tmp~"|clip.exe
dbenham wrote:
14 Aug 2019 05:58

Code: Select all

type %1.64 | clip
I was wondering if there was any technical reason for using the type file | clip syntax in this case instead of the clip < file one?
Or is this in the category "same difference" / personal preference?

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Send certutil output directly to a variable instead of file, and then finally copy to clipboard?

#7 Post by Aacini » 04 Sep 2019 15:55

I prefer the redirection < syntax because it is more efficient. The | pipe requires the execution of two additional copies of cmd.exe

Antonio

Post Reply