Tuesday, January 8, 2019

Automatically Convert Base64 to Binary on Double-Click

Okay, so this one doesn't include any direct VBA code, but hear me out.  I have programmed (in VBA) SOAP web service clients.  It is common practice to encode information with JSON or XML when sending and receiving such requests.  Sometimes, you even need to download data. Since these web services are usually in text format, they often send and receive data via base64 encoded data.

I custom coded a base64 decoder to pull information from these SOAP requests, convert them to binary, and output to a file. This works great, and is relatively quick!  However, I AM able to speed up the process by using windows' built-in base64 decoder "certutil".

I then realized, I can make a quick batch file which will do all the hard work for me and automatically decode a file.  I just needed to make sure that the file was actually base64.  I found myself renaming all of my base64 encoded files to myFile.txt.b64.

As you may know, base64 does not contain file type information. In order to overcome this, I included the original extension (.txt in this case) followed by a new extension (.b64). Let's go ahead and use this for our project!

So... what to do next? Make a batch file!  I opened up notepad (well... notepad++ actually) and made a cute little batch file. The code can be seen below:

 @ECHO OFF  
 certutil -decode %1 %~n1  
 %~n1  
 del %~n1  

I have 4 simple lines, let's break them down:

  1. (optional) Don't show us a bunch of stuff in the command prompt. 
  2. Decode the file passed as an argument into the batch file. Note: %1 means argument number 1, and %~n1 is a neat little trick which returns the argument without the extension. This is perfect for us! Since we are essentially using two extensions, we strip off the b64 extension in favor of the true extension
  3. Open up the decoded file
  4. (Optional) Delete the decoded file when finished. If you want to keep your file in a decoded format, be sure to remove this line! Optionally, you can modify %~n1 to be %1. Then we remove the base64 encoded file and only keep the decoded file (might be better in the long run since base64 files are larger than their binary counterparts)
Warning! 
Okay, so here's a problem. If we already have a file named myFile.txt in the same location as myFile.txt.b64 then we will end up deleting the possibly very different file called myFile.txt. To remedy this, I have modified the code slightly down to two lines.

 @ECHO OFF  
 certutil -decode %1 %~n1 && %~n1 && del %~n1  

This is better! The && makes sure that the delete function doesn't run unless opening the file works correctly and THAT won't run unless the file is properly decoded (which will fail if the file already exists). Edit: Turns out this doesn't actually work. There is no return true indicator to the command prompt that signals to the batch file that it's okay to delete file file. If you use this method del %~n1 will never run. Perhaps try the following code instead:


 @ECHO OFF  
 certutil -decode %1 %~n1  
 if NOT ["%errorlevel%"]==["0"] (  
   exit  
 )  
 %~n1  
 del %~n1  

This should exit our script if we have an error when decoding, thus preserving any files that may already exists.

Phew! Look at that! Now all we need to do is associate the file extension .b64 with our new batch file. So go ahead and save your batch file. I saved it in My Documents and called it "convert64ToBinary.bat" (note: the .bat extension is absolutely necessary).

In Windows 7 and up, this is quite simple. Just double click on your .b64 file and click "Open With."  At this point, we go to My Documents and click on the "convert64ToBinary.bat" file. Now all files with the .b64 file extension will open automatically with our batch file!

This is great news. You no longer need to convert base64 encoded files. Simply save the base64 encoded data in a file with two extensions, first the original extension and then .b64. Then you can simple double-click to open the file.

Here's a good alternative full code for the batch file. It keeps your decoded file and opens it and closes the pesky cmd window. As an added bonus, it will pause and show you any error that has occurred if the file wasn't successfully decoded.

 certutil -decode %1 %~n1  
 if NOT ["%errorlevel%"]==["0"] (  
      pause  
   exit  
 )  
 start %~n1  

No comments:

Post a Comment

VBA Add an animated Notification Box to your Excel Program

For those of us who create programs and add-ins in Excel, we are very, very familiar with the message box.  The message box gives us the opp...