Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have ~300 files that need to be truncated (I need to remove a specified number of bytes from the beginning of the file). I can do it one by one using a hex file editor, but given the number of files I have to process that would be a rather overwhelming task.

Is there an automated solution for this? (Operating system is Windows 7 64bit.)

share|improve this question
    
What operating system? –  Indrek Oct 8 '12 at 21:20
    
Windows 7 64bit –  groovy354 Oct 8 '12 at 21:21
1  
Your best bet is to use PowerShell. It should be pre-installed with the O.S. I'm not on a Windows machine now, but I'll come back with an answer tomorrow, if noone comes up with one. Here's a clue though: sans.org/windows-security/2010/02/11/… –  Radoo Oct 8 '12 at 21:31

2 Answers 2

dd has a skip option.

Per file You can use dd if=MyFile of=my_new_file skip=BytesToSkip

Optionally in a loop (e.g. in bash in the target directory)
for a in * ; do echo processing $a ; dd if=$a of=$a.shorter skip=300 ; done

Adjust skip with the correct number of bytes, KB or MB
If the files are large playing with the block size (bs=X) might speed things up.

share|improve this answer
    
Tell that to Windows... :) –  Radoo Oct 8 '12 at 21:29
    
There was no win7 tag yet when I started my answer. Anyway, how big are the files? Small enough for a quick move to a unix host? Large enough to warrant installing extra tools? –  Hennes Oct 8 '12 at 21:44
4  
dd is available for Windows too. In CMD the loop could be done like this: for %a in (*.*) do dd if=%a of=%a.shorter skip=300. –  Ansgar Wiechers Oct 8 '12 at 21:46

Here you go...

Powershell code:

$PATH = "d:\My Dir"
$BYTES_TO_TRIM = 10

$files = dir $PATH | where { !$_.PsIsContainer }

foreach ($file in $files) {
    Write-Output "File being truncated: $($file.FullName)"
    Write-Output "  Original Size: $($file.Length) bytes"
    Write-Output "  Truncating $BYTES_TO_TRIM bytes..."
    $byteEncodedContent = [System.IO.File]::ReadAllBytes($file.FullName)
    $truncatedByteEncodedContent = $byteEncodedContent[$BYTES_TO_TRIM..($byteEncodedContent.Length - 1)]
    Set-Content -value $truncatedByteEncodedContent -encoding byte -path "$($file.FullName)"

    Write-Output "  Size after truncation: $((Get-Item $file.FullName).Length) bytes"
    Write-Output "Truncation done!`n"
}
share|improve this answer
    
This script gives the following error: "Exception of type 'System.OutOfMemoryException' was thrown.." –  dtmland May 8 at 19:55
    
@dtmland I didn't thought of 100 GB files when I wrote it. If you want you can come with a byte-by-byte reading solution. –  Radoo May 10 at 18:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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