Re: disks and files

new topic     » goto parent     » topic index » view thread      » older message » newer message

On 30 Oct 97 , Philip A Lettkeman wrote:

> does anybody know how I would be able to take a file that is larger
> than a HD floppy (1.44mb) and split that file into parts that will
> fit onto more than one floppy?

You can determine the length of a file using the dir() function.
Knowing the maximun bytes each split should have you can do something
like this:

-----< CODE START >-----
-- SPLIT.EX
-- Daniel Berstein 30/10/97
-- Usage: ex split <filename>
-- Creates a batch file (join.bat) for recreating original file

include file.e

integer     source, dest, splits
object      size
atom        max, offset
sequence    filename

max = 1415577       -- About 1.35 MB

-- Read filename
filename = command_line()
if length(filename) < 3 then
    puts(1, "Please supply a file to split.\n")
    abort(0)
end if
filename = filename[3]

-- Get file size
size = dir(filename)
if atom(size) then
    puts(1,"Can't find file " & filename & ".\n")
    abort(0)
end if
size = size[1][D_SIZE]

-- Check splits needed
if size/max > 999 then
     puts(1, "Too many split files needed!\n")
    abort(0)
end if

-- Open source file
source = open(filename, "rb")
if source = -1 then
    puts(1, "Can't open file " & filename & "\n.")
    abort(0)
end if

splits = 1
offset = 1

-- Main loop
while 1 do
    -- Create split file
    puts(1, "Creating split file #" & sprintf("%d", splits) & "\n")
    dest = open(filename[1..find('.', filename)] & sprintf("%d",splits),"wb")
    if dest = -1 then
        puts(1, "Can't create split file " & filename[1..find('.', filename)] &
           sprintf("%d",splits))
        close(source)
        abort(0)
    end if
    -- Write split file
    while (offset <= max*splits) and (offset <= size) do
        puts(dest, getc(source))
        offset = offset + 1
    end while
    -- Close split file
    close(dest)
    -- Exit if all done
    if offset > size then
        close(source)
        exit
    end if
    -- Update splits file counter
    splits = splits + 1
end while

-- Create batch file
dest = open("join.bat","w")
puts(dest,
"@ echo off\n" &
"cls\n" &
"echo Recreating " & filename & "\n" &
"@ echo off\n" &
"copy /b ")
for i = 1 to splits do
    puts(dest, filename[1..find('.', filename)] &
      sprintf("%d",i) & " ")
    if i <= splits - 1 then
        puts(dest, "+ ")
    end if
end for
puts(dest, filename & " > nul\n")
close(dest)

puts(1,sprintf("%d",splits) & " split files created and join.bat\n")
-----< CODE END >-----

Hope this helps you. i tested it on a 2.2 MB zip file and worked
correctly (creating 2 splits).

Regards,
  Daniel Berstein
  danielberstein at usa.net
  http://www27.pair.com/daber/architek

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu