How to escape special characters on InputFile with Drag Drop

October 5, 2019 at 03:39:06
Specs: Windows 10
The goal of this code is to drag and drop a file onto the script and it encodes or decodes it in Base64 with Certutil Command.
So my question is : "What can i do to escape special characters when a file to be encoded or decoded have them ?"
Thank you !

@echo off
Set "file=%~1"
If "%file%"=="" GOTO:EOF
>nul find "-----BEGIN CERTIFICATE-----" "%file%" && (
 certutil -f -v -decode "%file%" "%file%"
) || (
 certutil -f -v -encode "%file%" "%file%"

See More: How to escape special characters on InputFile with Drag Drop

Reply ↓  Report •

October 5, 2019 at 19:14:26
hello hackoo: the nature of base-64 encoding/decoding precludes any problem with special characters contained in a file - that is its primary purpose: to make binary files into text and the reverse. Once encoded, the file should contain nothing except 0-9,a-z,A-Z, which covers 248 binaries, and the remaining 8 are covered by two additional characters which depends. (older versions used +-, certutil seems to use / and char(111). Certutil puts crlf every 64 bytes to make the file more manageable for text editors. These might need to be removed for other applications that don't expect them. Other than that, there should be no problems since certutil is not batch.

Reply ↓  Report •

October 6, 2019 at 09:16:29
Hello nbrane !
Sorry ! There is a misunderstood, i'm talking about the name of the file, not its contents, so the problem is when a file to be dragged and dropped contains a special character, like the ampersand & My&File.txt, this will fails by the batch file not from Certutil

So i got a solution from another forum and the problem is solved !

@echo off
Title Encode and Decode files into B64
setlocal enableextensions enabledelayedexpansion
set "count=0"
set scr="%~f0"
set "cmd=!cmdcmdline:*%~f0=!"
set "args=!cmd:~0,-1!"
set "args=!args:* =!"

for %%f in (!args!) do (
   set /a "count+=1"
   set infull[!count!]="%%~f"

for /l %%i in (1,1,!count!) do (
   Set "file=!infull[%%i]!"

If [!file!]==[] GOTO:EOF
echo !file! & pause

>nul 2>&1 find /I "-----BEGIN CERTIFICATE-----" !file! && (
certutil -f -v -decode !file! !file!
) || (
certutil -f -v -encode !file! !file!

Reply ↓  Report •
Related Solutions

Ask Question