You can use the built-in executable certutil
to convert the file from ASCII to hexadecimal, process the resulting file with a for /f
loop, strip any instances of 0d
(which is the carriage return character in hexadecimal) from each line, and rebuild the file from the remaining hex code. I swear it's easier than I'm making it sound.
Please note that there is a maximum input file size limit of 71 MB due to a limitation with certutil
, and that files larger than 2 MB can take a while to process, but at least everything is native to Windows so you don't have to install anything or learn a completely new language.
@echo off
setlocal enabledelayedexpansion
:: Ensure a file was passed in
if "%~1"=="" (
echo Please provide a file to process.
pause
exit /b
)
:: Ensure file is under the certutil input limit
if %~z1 GTR 74472684 (
echo This file exceeds the maximum file size of 74472684 bytes ^(71 MB^)
echo Please use a smaller file.
pause
exit /b
)
:make_rand
:: Generate a random number to reduce the risk of filename collision
set rand=%RANDOM%%RANDOM%
set "temp_file=%~dpf1_%rand%.tmp"
set "hex_file=%~dpf1_%rand%.hex"
set "new_file=%~dpf1_new.%~x1"
if exist %temp_file% goto :make_rand
if exist %hex_file% goto :make_rand
if exist %new_file% choice /c:YN /M "%new_file% already exists. Overwrite? "
if %errorlevel% equ 1 del %new_file%
if %errorlevel% equ 2 exit /b
certutil -encodehex "%~1" "%temp_file%"
:: The script will break if you have spaces in your file path.
:: This is a feature, not a bug. Names your paths correctly.
for /f "tokens=1,*" %%A in (%temp_file%) do (
set "line=%%B"
set "hex_substring=!line:~0,48!"
set "no_carriage=!hex_substring:0d=!"
echo !no_carriage! >>%hex_file%
)
certutil -decodehex "%hex_file%" "%new_file%"
:: Temp file cleanup
del /q %hex_file%
del /q %temp_file%