Copying part of a binary file?

  • Thread starter Thread starter simonc
  • Start date Start date
S

simonc

Is there a way in Windows or a Command Window of copying a specified number
of bytes from a binary file to another file? I've got a very large binary
file and want to break it into smaller chunks to manipulate more easily.

Grateful for advice.
 
simonc said:
Is there a way in Windows or a Command Window of copying a specified
number
of bytes from a binary file to another file? I've got a very large binary
file and want to break it into smaller chunks to manipulate more easily.

Grateful for advice.

There are no native tools in Windows to do this. You need a file splitter
like the one below. Here is what you need to do:
1. Copy and paste the code into c:\Split.vbs. Make sure to have a .vbs file
extension!
2. Click Start / Run
3. Type the three letters cmd and click OK.
4. Type the following command:
cscript c:\split.vbs "c:\Documents and Settings\SimonC\BigFile.bin" xxx
c:\
Instead of "xxx", type the desired chunk size in kBytes.
5. Press the Enter key.

This will give you a number of file chunks named c:\BigFile.001,
c:\BigFile.002 etc.

'--------------------------------
'Split a file into smaller chunks
'8.4.2008 FNL
'--------------------------------
LF = Chr(10)
Dim oFSO, FullName, Path, Name, Size, TargetDir
Set oFSO = CreateObject("Scripting.FileSystemObject")

WScript.Echo
Parms
Split

'-----------------------------------
'Process the command line parameters
'-----------------------------------
Sub Parms
Dim oArgs
Set oArgs = WScript.Arguments
If oArgs.Count <> 3 Then Error _
("Incorrect number of parameters." & LF & LF _
& "Usage: Split FullName Size TargetDrive" & LF _
& "State desired output file size in kBytes!")

FullName = oArgs(0)
Path = oFSO.GetParentFolderName(oArgs(0))
Name = oFSO.GetBaseName(oArgs(0)) & "."
Size = 1000 * oArgs(1)
TargetDir = Left(oArgs(2),1) & ":\"
End Sub

'--------------
'Split the file
'--------------
Sub Split
Dim iFile, oFile, iStream, Data
Dim Ext, e, offset, length, NewName

if not oFSO.FileExists(FullName) then Error _
("Cannot locate the file """ & FullName & """")
Set iFile = oFSO.GetFile(FullName)

On Error Resume Next
Set iStream = iFile.OpenAsTextStream(1)
if err.number > 0 then Error("Cannot open the file """ & FullName _
& """" & LF & Err.Description)
On Error Goto 0
Data = iStream.Read(iFile.Size)
iStream.close

Ext = 0
offset = 1
Do
Ext = Right("00" & Ext + 1, 3)
if ext > "999" then Error ("Too many files - maximum is 999!")
NewName = TargetDir & Name & Ext
WScript.Echo "Writing """ & NewName & """"
On Error Resume Next
Set oFile = oFSO.CreateTextFile(NewName, 2)
if err.number > 0 then Error("Cannot open the file """ _
& NewName & """" & LF & Err.Description)
On Error Goto 0

length = Size
If length > Len(data)+1 - offset Then length = Len(data) + 1 - offset

oFile.Write Mid(Data, offset, length)
offset = offset + length
oFile.Close
Loop Until offset >= Len(data)
End Sub
'----------
'Error exit
'----------
Sub Error (Message)
WScript.Echo Message
WScript.Quit
End Sub
 
BRILLIANT.

Many thanks

Pegasus said:
There are no native tools in Windows to do this. You need a file splitter
like the one below. Here is what you need to do:
1. Copy and paste the code into c:\Split.vbs. Make sure to have a .vbs file
extension!
2. Click Start / Run
3. Type the three letters cmd and click OK.
4. Type the following command:
cscript c:\split.vbs "c:\Documents and Settings\SimonC\BigFile.bin" xxx
c:\
Instead of "xxx", type the desired chunk size in kBytes.
5. Press the Enter key.

This will give you a number of file chunks named c:\BigFile.001,
c:\BigFile.002 etc.

'--------------------------------
'Split a file into smaller chunks
'8.4.2008 FNL
'--------------------------------
LF = Chr(10)
Dim oFSO, FullName, Path, Name, Size, TargetDir
Set oFSO = CreateObject("Scripting.FileSystemObject")

WScript.Echo
Parms
Split

'-----------------------------------
'Process the command line parameters
'-----------------------------------
Sub Parms
Dim oArgs
Set oArgs = WScript.Arguments
If oArgs.Count <> 3 Then Error _
("Incorrect number of parameters." & LF & LF _
& "Usage: Split FullName Size TargetDrive" & LF _
& "State desired output file size in kBytes!")

FullName = oArgs(0)
Path = oFSO.GetParentFolderName(oArgs(0))
Name = oFSO.GetBaseName(oArgs(0)) & "."
Size = 1000 * oArgs(1)
TargetDir = Left(oArgs(2),1) & ":\"
End Sub

'--------------
'Split the file
'--------------
Sub Split
Dim iFile, oFile, iStream, Data
Dim Ext, e, offset, length, NewName

if not oFSO.FileExists(FullName) then Error _
("Cannot locate the file """ & FullName & """")
Set iFile = oFSO.GetFile(FullName)

On Error Resume Next
Set iStream = iFile.OpenAsTextStream(1)
if err.number > 0 then Error("Cannot open the file """ & FullName _
& """" & LF & Err.Description)
On Error Goto 0
Data = iStream.Read(iFile.Size)
iStream.close

Ext = 0
offset = 1
Do
Ext = Right("00" & Ext + 1, 3)
if ext > "999" then Error ("Too many files - maximum is 999!")
NewName = TargetDir & Name & Ext
WScript.Echo "Writing """ & NewName & """"
On Error Resume Next
Set oFile = oFSO.CreateTextFile(NewName, 2)
if err.number > 0 then Error("Cannot open the file """ _
& NewName & """" & LF & Err.Description)
On Error Goto 0

length = Size
If length > Len(data)+1 - offset Then length = Len(data) + 1 - offset

oFile.Write Mid(Data, offset, length)
offset = offset + length
oFile.Close
Loop Until offset >= Len(data)
End Sub
'----------
'Error exit
'----------
Sub Error (Message)
WScript.Echo Message
WScript.Quit
End Sub
 
simonc said:
Is there a way in Windows or a Command Window of copying a specified number
of bytes from a binary file to another file? I've got a very large binary
file and want to break it into smaller chunks to manipulate more easily.

Grateful for advice.

There is another tool here.

http://www.chrysocome.net/dd

Example:

dd if=C:\big.bin of=C:\small.bin bs=1024 skip=300 count=400

That would skip over the first 300KB of the file "big.bin",
and then copy the next 400KB of data to "small.bin".

When you use larger values for the block size "bs", the command
transfers data a bit faster.

That example would be good in a situation, where you only want
to examine a single section of data from the bigger file.

Paul
 
simonc said:
Is there a way in Windows or a Command Window of copying a specified
number of bytes from a binary file to another file? I've got a very
large binary file and want to break it into smaller chunks to
manipulate more easily.

Grateful for advice.

Yes. Get yourself one of the many free hex editors around the 'net.
It's a little tedious, but you can then see exactly what/where you are
splitting the file. Copy it out, save it to a new file, continue for
the next part.
And of course do NOT work on the original; use a copy!

HTH,

Twayne`
 
Back
Top