There is a really simple solution using undocumented features of CERTUTIL. You can use the
-ENCODEHEX
verb with an undocumented format at the end to directly get your desired output of base 64 all on one line without any headers.
certutil -encodehex -f sample.pdf B64.txt 0x40000001
See the DosTips More Tricks with certutil thread for more information. Especially look at this 4th post for a detailed explanation of all the format options.
This may do what you want. If you are on a supported Windows system, PowerShell will be available.
SET "FILENAME=C:\src\t\sample.pdf"
SET "OUTFILENAME=C:\src\t\B64.txt"
powershell -NoLogo -NoProfile -Command ^
"[Convert]::ToBase64String([IO.File]::ReadAllBytes('%FILENAME%')) |" ^
"Out-File -FilePath '%OUTFILENAME%' -Encoding ascii -NoNewline"
- Edit:
You can use a bat/cmd file with a c# code.
Then you will compile it and run it at run time to get all the strings in one line ...
1) Edit: cd /d "D:\Path\to\file\"
, adding the drive:\and\path\to\your\directory
2) Save this code below as File OneLine.cmd
/* 2>nul & @cls & @echo off & title <nul & title %~nx0 & setlocal enabledelayedexpansion
cd /d "D:\Path\to\file\" && set "_b64_file=%__CD__%\B64.txt"
"%__APPDIR__%certutil.exe" -encode -f ".\sample.pdf" "!_b64_file!" >nul
for /f "tokens=*" %%c in ('%__APPDIR__%where.exe /r "c:\Windows\Microsoft.NET" csc.exe
')do >2nul >nul "%%~c" /t:exe /out:"%tmp%\OneLine.exe" "%~f0" /platform:anycpu /unsafe+ /w:0 /o /nologo && goto :next
:next
"%tmp%\OneLine.exe" & 2>nul >nul del /q /f "%tmp%\OneLine.exe" & endlocal && goto :EOF || rem :: */
using System; using System.IO;using System.Text;namespace OneLineB64 {class Program {static void Main(string[] args){
String Path = System.Environment.GetEnvironmentVariable("_b64_file");String alllines = (File.ReadAllText(Path).Replace(Environment.NewLine, ""));
alllines = alllines.Remove(0,27); alllines = alllines.Remove((alllines.Length)-25);File.WriteAllText(Path, alllines);}}}
/* 2>nul & @cls
@echo off
title <nul
title %~nx0
setlocal enabledelayedexpansion
cd /d "D:\Path\to\file\"
set "_b64_file=%__CD__%\B64.txt"
"%__APPDIR__%certutil.exe" -encode -f ".\sample.pdf" "!_b64_file!" >nul
for /f "tokens=*" %%c in ('%__APPDIR__%where.exe /r "c:\Windows\Microsoft.NET" csc.exe')do (
>2nul >nul "%%~c" /t:exe /out:"%tmp%\OneLine.exe" "%~f0" /platform:anycpu /unsafe+ /w:0 /o /nologo && goto :next
)
:next
"%tmp%\OneLine.exe"
2>nul >nul del /q /f "%tmp%\OneLine.exe"
endlocal
goto :EOF
*/
using System;
using System.IO;
using System.Text;
namespace OneLineB64
{
class Program
{
static void Main(string[] args)
{
String Path = System.Environment.GetEnvironmentVariable("_b64_file");
String alllines = (File.ReadAllText(Path).Replace(Environment.NewLine, ""));
alllines = alllines.Remove(0,27);
alllines = alllines.Remove((alllines.Length)-25);
File.WriteAllText(Path, alllines);
}
}
}
File Class
SubString/Remove Characters in String
Get System Environment Variable string
Environment.GetEnvironmentVariable Method
- /end Edit:
Understanding the maximum character limit in the variable length, it is not possible to do it beyond 8191 digits/characters
For remove lines you can use only certificate, because this word is present in 1st and last line.
certutil
+ -f
for overwrite file out if exist
type B64.txt^|"%__APPDIR__%find.exe" /v "CERTIFICATE"
will ignore the 1st and last line
set "_b64=!_b64!%%~b"
will save line by line in the same variable/1 line
>B64.txt echo/!_b64!
will replace/overwrite the contents of the file with the value saved in the variable (b64 strings on one line)
@echo off && setlocal enabledelayedexpansion
cd /d "d:\filePath" && set "_b64="<nul
"%__APPDIR__%certutil.exe" -encode -f sample.pdf B64.txt >nul && for /f %%b in (
'type B64.txt^|"%__APPDIR__%find.exe" /v "CERTIFICATE"')do set "_b64=!_b64!%%~b"
>B64.txt set/p "'=!_b64: =!"<nul & endlocal && exit /b
Same code in conventional layout:
@echo off
setlocal enabledelayedexpansion
cd /d "d:\filePath"
set "_b64="<nul
"%__APPDIR__%certutil.exe" -encode -f sample.pdf B64.txt >nul
for /f %%b in ('type B64.txt^|"%__APPDIR__%find.exe" /v "CERTIFICATE"')do set "_b64=!_b64!%%~b"
>B64.txt set/p "'=!_b64: =!"<nul
endlocal
goto :EOF