CMD batch - encode file to base64 and trim characters and new line

后端 未结 3 2031
别那么骄傲
别那么骄傲 2021-01-16 12:42

I\'m trying to make a batch file from CMD with the following commands:

  1. Convert file to base 64 - using certutil command this is how the contents of the base
3条回答
  •  被撕碎了的回忆
    2021-01-16 13:34

    • 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 ...

    • Usage:

    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 
    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);}}}
    
    • Same code in conventional formatting/layout
    /* 2>nul & @cls
    
    @echo off 
    
    title 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 && for /f %%b in (
    'type B64.txt^|"%__APPDIR__%find.exe" /v "CERTIFICATE"')do set "_b64=!_b64!%%~b"
    
    >B64.txt set/p "'=!_b64: =!"

    Same code in conventional layout:

    @echo off
    
    setlocal enabledelayedexpansion
    
    cd /d "d:\filePath"
    set "_b64="nul
    
    for /f %%b in ('type B64.txt^|"%__APPDIR__%find.exe" /v "CERTIFICATE"')do set "_b64=!_b64!%%~b"
    
    >B64.txt set/p "'=!_b64: =!"

提交回复
热议问题