'Binary file edit in Win CMD

How to split or edit binary file in Win CMD?
I'm on Win 10 x64, and there's no DEBUG utility

eg. to replace all 0a with 0d0a or change a byte value at some byte offset?



Solution 1:[1]

Yes, debug is an "old" DOS command (maybe it was present till WindowsXP or so). I do not know any default way to manipulate binary files under Windows 10.
You need to find a third-party-tool which can provide the desired functionality. Right now I have no hint for you, where to find one or which one would work best. Please try a web search on your own.

However, you could check out some visual tools, if there is no urgent need to use a batch file. Like HxD for example. A free, versatile Hex-Editor for Windows. It should come with a find/replace functionality that might to the job for you.

Solution 2:[2]

fc /b performs a similar function to certutil -encodehex -f <in> <out> 12.

The resulting hex file can then be manipulated (edited/processed) by common batch commands before written back to disk in binary, just like HxD.

Each character in the resultant (or intermediate, rather) file is a nibble, and may be processed as pairs to reflect the full byte of the current binary file encoding.

To replace characters, conduct a string search/replace.
For offsets, search for & replace with for loop in the resulting hex file.
To replace bytes at known offsets, files can be split before offset, replacement character inserted then rest of file added, instead of hexing the whole file and searching for the byte to be replaced.

To split binary files, parse upto required character/offset, write part file, then parse next length of characters. Continue until whole file has been parsed & split:

set file="x.edb"                &REM No dir path, local only, compressed for CRLF
set max=70000000            &REM certutil has max file limit around 74MB

REM Findstr line limit 8k
REM Workaround: wrap in 7z archive to generate CRLF pairs

for %i in (%file%) do (
set /a num=%~zi/%max% >nul      &REM No. of chunks
set /a last=%~zi%%max% >nul     &REM size of last chunk
if %last%==0 set /a num=num-1       &REM ove zero byte chunk
set size=%~zi
)

ren %file% %file%.0

for /l %i in (1 1 %num%) do (
set /a s1=%i*%max% >nul
set /a s2="(%i+1)*%max%" >nul
set /a prev=%i-1 >nul

echo Writing %file%.%i
type %file%.!prev! | (
  (for /l %j in (1 1 %max%) do pause)>nul& findstr "^"> %file%.%i)

FSUTIL file seteof %file%.!prev! %max% >nul
)
if not %last%==0 FSUTIL file seteof %file%.%num% %last% >nul
echo Done.

Tested on Win 10

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Antares
Solution 2