Apr 5, 2020 06:19 PM
(This post was last modified: Apr 5, 2020 06:22 PM by SIM-4581439.)
Without going into much detail I was required to encode a decent amount of files in Base64. The script below encodes/decodes either one file or many (from a directory). The script determines if a file has been encoded by looking at the header. If present, the script decodes the file, and if not, encodes it. This code isn't fool-proof however. It uses string manipulation to trim off the last three or last four characters from the object name. If what was trimmed starts with a period then the script assumes it's a file. If not, it assumes the object is a directory. This only functions for two or three character file extensions. Feel free to post any suggestions or possible upgrades to improve the functionality. Cheers!
Code
@echo off >NUL
Set "file=%~1"
REM [CHECK FOR FILE EXTENSION]
If "%file:~-4,1%"=="." goto :FILE
If "%file:~-3,1%"=="." goto :FILE
REM [IF NOT FILE, DECRYPT/ENCRYPT DIRECTORY]
goto :DIR
exit
:FILE
If "%file%"=="" GOTO:EOF
REM [CHECKS FOR BASE64 HEADER]
>nul find "-----BEGIN CERTIFICATE-----" "%file%" && (
certutil -f -v -decode "%file%" "%file%"
) || (
certutil -f -v -encode "%file%" "%file%"
)
exit
:DIR
REM [CATALOGS FILE(S) FROM DIRECTORY]
DIR /B /S "%file%" >"%temp%\list.ini"
Set "content=%temp%\list.ini"
REM [CHECKS FOR BASE64 HEADER IN FILE(S)]
Setlocal EnableDelayedExpansion
FOR /F "delims=" %%K IN (%content%) DO (
set "obj=%%K"
>nul find "-----BEGIN CERTIFICATE-----" "!obj!" && (
certutil -f -v -decode "!obj!" "!obj!"
) || (
certutil -f -v -encode "!obj!" "!obj!"
)
)
Del /F /Q "%content%"
exit
