PC Review


Reply
Thread Tools Rate Thread

Copying part of a binary file?

 
 
simonc
Guest
Posts: n/a
 
      4th Sep 2009
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.
 
Reply With Quote
 
 
 
 
Pegasus [MVP]
Guest
Posts: n/a
 
      4th Sep 2009

"simonc" <(E-Mail Removed)> wrote in message
news:7794E119-142D-48B4-94C4-(E-Mail Removed)...
> 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


 
Reply With Quote
 
simonc
Guest
Posts: n/a
 
      4th Sep 2009
BRILLIANT.

Many thanks

"Pegasus [MVP]" wrote:

>
> "simonc" <(E-Mail Removed)> wrote in message
> news:7794E119-142D-48B4-94C4-(E-Mail Removed)...
> > 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
>
>
>

 
Reply With Quote
 
Pegasus [MVP]
Guest
Posts: n/a
 
      4th Sep 2009

"simonc" <(E-Mail Removed)> wrote in message
news:7360FD46-BE85-455A-94A3-(E-Mail Removed)...
> BRILLIANT.
>
> Many thanks
>


Thanks for the feedback.


 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      4th Sep 2009
simonc wrote:
> 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
 
Reply With Quote
 
Twayne
Guest
Posts: n/a
 
      4th Sep 2009
"simonc" <(E-Mail Removed)> wrote in message
news:7794E119-142D-48B4-94C4-(E-Mail Removed)
> 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`



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Uploading a binary file as part of a POST WebRequest Anthony Papillion Microsoft VB .NET 3 1st Feb 2010 10:47 PM
How to change binary data buffer read from a binary file to string format Anderson Microsoft VC .NET 1 21st Jul 2006 11:35 AM
Copying Part of a row down part of a column Not Excelling Microsoft Excel Misc 3 6th Jan 2006 11:58 PM
Copying Binary Data To the Clipboard Neo Microsoft Dot NET 0 15th Dec 2005 02:27 AM
Convert binary file->utf8->binary file Joey Lee Microsoft C# .NET 2 25th Apr 2005 07:16 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:27 AM.