2

I have rescued some files (Python, Shell scripts etc., so just text files) from a Linux SD card using FTK Imager Lite. The files are ok, but they now contain a lot of 0x00 bytes at the end.

How could I bulk-remove these trailing 0x00 bytes with my Windows machine?

I am familiar with the for command, so a suggestion on how to do it for one file would be sufficient. I can then apply it to all files.

2

You can use the MORE command to convert each \0x00 into \0x0D\0x0A

more yourFile.ext >yourFile.ext.new
move /y yourFile.ext.new yourFile.ext >nul

Caveats:

  • Each file must have fewer than ~64k lines else the command will hang (inserts a prompt and waits for a keypress after 64k lines)
  • Tabs will be converted into a string of spaces

A more robust alternative is to use JREPL.BAT - a regular expression text processing command line utility for Windows. It is pure script (JScript/batch) that runs natively on any Windows machine from XP onward - no 3rd party exe file required.

As long as no file is too large (approaching 1GB?), then you can use the /M option to read the entire file into memory and perform the search/replace operation on one big string:

jrepl \x00 "" /m /f yourFile.ext /o -

For large files you can process the file line by line, but the null bytes require specification of a character set for the input which forces the use of ADO. The standard JScript file input routines choke on lines containing null bytes. ADO solves that problem. It does not matter what character set is chosen as long as it is a single byte character set.

jrepl \x00 "" /f "yourFile.ext|windows-1252" /o -

Full documentation is built into JREPL.BAT and available via jrepl /? or jrepl /?? for paged help. A summary of all options is available via jrepl /?options, and a description of all help options is gotten via jrepl /?help.

JREPL.BAT is a lot of code for just this one task. It wouldn't be hard to whip up a very small dedicated JScript or VBS script for this task. But once you have JREPL in your arsenal, you will likely find many uses - it is a powerful tool.

1

The core of a PowerShell solution would look like this:

$file = "C:\Users\keith\SomeFile.txt"

$Bytes = [IO.File]::ReadAllBytes($file)

$i = $Bytes.Length - 1

While ($Bytes[$i] -eq 0) { $i-- }

$Bytes = $Bytes[0..$i]

[IO.File]::writeAllBytes($file, $bytes)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.