[comp.sys.ibm.pc.misc] SPLIT

cramer@optilink.UUCP (Clayton Cramer) (04/20/91)

In article <1991Apr15.024343.6027@cs.mcgill.ca>, storm@cs.mcgill.ca (Marc WANDSCHNEIDER) writes:
> 
> I am looking for any program that will split up large files into smaller ones
> on the hard drive.  I am aware of some that will split up files over floppies
> but I would like one that will split them up right on the hard drive.
> 
> If you could also give me an ftp site or Compu$erve joint where I can get it
> I would appreciate it.
> 
> storm@cs.mcgill.ca         McGill University           It's 11pm, do YOU

Couldn't find a valid path to you, and this may be generally useful:

======================================================================
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <errno.h>
#include "std.h"

main(Argc, Argv)

int		Argc;
char*	Argv[];

	{
	long		BytesThisPartFile;
	long		BytesPerPart;
	int			PartNbr;
	char		OutFileName[40];
	FILE*		OutFile;
	FILE*		FileToPart;
	char*		Bytes;
	bool		MoreBytes = TRUE;
	int			BytesRead;
	
	if(Argc < 3)
		{
		fprintf(stderr, "Three arguments required.\n");
		DisplayHelp();
		exit(2);
		}
	else
		{
		FileToPart = fopen(Argv[1], "rb");
		if(FileToPart)
			{
			if(1 == sscanf(Argv[3], "%ld", &BytesPerPart))
				{
				BytesPerPart *= 1000L;
				Bytes = malloc(1000);
				if((Bytes) && (FileToPart))
					{
					PartNbr = 0;
					sprintf(OutFileName, Argv[2], PartNbr);
					fprintf(stderr, "creating %s\n", OutFileName);
					BytesThisPartFile = 0L;
					OutFile = fopen(OutFileName, "wb");
					if(OutFile)
						{
						while(MoreBytes && !feof(OutFile) && !ferror(OutFile))
							{
							BytesRead = fread(Bytes, sizeof(char), 
											  sizeof(Bytes), FileToPart);
							fwrite(Bytes, sizeof(char), BytesRead, OutFile);
							BytesThisPartFile += sizeof(Bytes);
							if(feof(FileToPart))
								MoreBytes = FALSE;
							else if(BytesThisPartFile >= BytesPerPart)
								{
								PartNbr++;
								fclose(OutFile);
								sprintf(OutFileName, Argv[2], PartNbr);
								fprintf(stderr, "creating %s\n", OutFileName);
								BytesThisPartFile = 0L;
								OutFile = fopen(OutFileName, "wb");
								}
							}
						if(feof(OutFile) || ferror(OutFile))
							fprintf(stderr, "On %s: %s\n", 
									OutFileName, sys_errlist[errno]);
						}
					else
						{
						fprintf(stderr, "Unable to create file %s: %s\n", 
								OutFileName, sys_errlist[errno]);
						exit(2);
						}
					}
				fclose(OutFile);
				}
			else
				{
				fprintf(stderr, "Invalid size for partition: %s\n", Argv[3]);
				DisplayHelp();
				}
			}
		else
			{
			fprintf(stderr, "Unable to open file %s\n", Argv[1]);
			DisplayHelp();
			}
		}
	}

DisplayHelp()

	{
	fprintf(stderr, "PART filetopart partspec partsize\n");
	fprintf(stderr, "filetopart is the file to be partitioned\n");
	fprintf(stderr, "partspec is the format control string for file names\n");
	fprintf(stderr, "         which must contain %%d within it.\n");
	fprintf(stderr, "partsize is the number of 1K blocks in each\n");
	fprintf(stderr, "         output partitioned file\n");
	}
======================================================================
Link it with the setargv.obj module that comes with the Microsoft
C compiler, and everything will be wonderful.
-- 
Clayton E. Cramer {uunet,pyramid,pixar,tekbspa}!optilink!cramer
You must be kidding!  No company would hold opinions like mine!
Article IX, "Sec. 21.  That the right of citizens to bear arms, in defence of
themselves and the State, shall not be questioned." -- PA State Const. of 1790

raymond@math.berkeley.edu (Raymond Chen) (04/21/91)

In article <6256@optilink.UUCP>, cramer@optilink (Clayton Cramer) writes:
>In article <1991Apr15.024343.6027@cs.mcgill.ca>, storm@cs.mcgill.ca (Marc WANDSCHNEIDER) writes:
>> 
>>I am looking for any program that will split up large files into smaller ones
>
>Couldn't find a valid path to you, and this may be generally useful:
>[long program deleted]

Gack, a 101-line program to do something as simple as chopping a file?

Get perl from SIMTEL20:<MSDOS.PERL> and run the following script;
notice that the instructions are longer than the program!

If you have 4DOS, you can type  `set .pl=c:\bin\perl.exe' and call
this script `split.pl', then you can just type `split 64 bigfile file0000'.

# split: Chop a file into bite-sized pieces.
#
#  Usage:  split # file result
#
# Chops the <file> into pieces <#>K in size.
#
# <result> provides the file names to use.  If it ends in digits, then
# the digits are incremented.  For example if you say `file0001' then
# the output files will be `file0001' `file0002' `file0003'...
#
# If <result> ends in letters, then the letters are incremented.  For
# example, if you say `fileaa', then the output files will be `fileaa',
# `fileab', ... `fileaz', `fileba' ...
#
die "usage: split # file result\n" unless (($s $f, $r) = @ARGV) == 3;
open(f) || die "Couldn't open $f ($!)"; binmode(f);
while (read(f, $_, $s * 1024)) {
   die "Will not overwrite existing file $r\n" if -e $r;
   open(R, ">$r") || die "Couldn't open $r for writing ($!)"; binmode(R); 
   print R || die "Error writing $r ($!)"; 
   close(R) || die "Error closing $r ($!)";
   ++$r;
}