Delete a part in a file without ruining the encoding and extension

1

1

I have a fairly large database of files. The files have very different extensions (.txt, .doc, .ppt etc), but every one of them has a certain phrase at the start of the file which has to be removed.

So, let's say I have a file randomtext.doc. Looking at the file in a simple text editor, the file would normally begin with:

\D0\CFࡱ\E1\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00>\00\00\FE\FF                           \00\00\00\00\00\00\00\00\00\00\00\00\00\00\00K\00\00\00\00\00\00\00\00\00\00M\00\00\00\00\00\00\FE\FF\FF\FF\00\00\00\00J\00\00\00\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\

But my file begins with:

MYPHRASE \D0\CFࡱ\E1\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00>\00\00\FE\FF  \00\00\00\00\00\00\00\00\00\00\00\00\00\00\00K\00\00\00\00\00\00\00\00\00\00M\00\00\00\00\00\00\FE\FF\FF\FF\00\00\00\00J\00\00\00\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\

Deleting the phrase in a regular text editor ruins the file (for example, this file, which would normally be a .doc file, wouldn't be able to be viewed by Microsoft Word). I need to be able to delete the phrase, but keep the file extension etc.

Leonard Roosiõis

Posted 2013-05-25T13:22:22.823

Reputation:

Answers

1

This can be accomplished in multiple ways. For example, a search and replace utility that support binary files can be used to replace "MYPHRASE " with an empty string. There are also hex editors available that can be scripted to do this.

I would recommend using dd for Windows though (be sure to download the latest version i.e. dd-0.6beta3.zip).

The following command can be used to delete 9 bytes ("MYPHRASE ") from the beginning of a file:

dd bs=9 skip=1 if="Drive:\Path\to\infile.ext" of="Drive:\Path\to\outfile.ext"

If you want to do this with lots of files, place them all in a single directory and run the following batch file after editing the delbytes and rootfolder variables as per your requirements:

@echo off
cls
set delbytes=9
set rootfolder="C:\My Files"
for /r %rootfolder% %%a in (*.*) do (
    echo Truncating first %delbytes% bytes of "%%a"
    dd bs=%delbytes% skip=1 if="%%a" of="%%~dpna (Truncated)%%~xa"
    echo.
)

Karan

Posted 2013-05-25T13:22:22.823

Reputation: 51 857

Note: I wanted to add some error handling to the batch file. The changelog for 0.6beta3 states that support for errorlevel exit code has been added, but I could not get it to work (it always seemed to set errorlevel to 0, even when the output clearly stated that an error had occurred). Does anyone know how errorlevel works for dd for Windows, or is it a bug? – Karan – 2013-05-25T19:37:16.250