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?
Send certutil output directly to a variable instead of file, and then finally copy to clipboard?
Moderator: DosItHelp
-
- Posts: 128
- Joined: 23 May 2016 15:39
- Location: Spain
Re: Send certutil output directly to a variable instead of file, and then finally copy to clipboard?
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.
Steffen
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~"
Re: Send certutil output directly to a variable instead of file, and then finally copy to clipboard?
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.
Dave Benham
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
Re: Send certutil output directly to a variable instead of file, and then finally copy to clipboard?
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.dbenham wrote: ↑14 Aug 2019 05:58Also, 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.
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?
Re: Send certutil output directly to a variable instead of file, and then finally copy to clipboard?
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?
Re: Send certutil output directly to a variable instead of file, and then finally copy to clipboard?
I prefer the redirection < syntax because it is more efficient. The | pipe requires the execution of two additional copies of cmd.exe
Antonio
Antonio