Hi here is a small tool I made to bind many files into one. I made this because I had many text files with code examples splattered all over my desktop so I made this tool to combined them into one neat file. anyway This code is written for the open-source Delphi like IDE Lazarus but you should have little trouble using this in Delphi. anyway hope it maybe of some use. comments and suggestions welcome.
Console application code:
Code:
//A Simple file binder by Ben Jones.
//For use with Lazarus.
program binder;
{$mode objfpc}{$H+}
uses {$IFDEF UNIX} {$IFDEF UseCThreads}
cthreads, {$ENDIF} {$ENDIF}
Classes { you can add units after this };
var
i, Counter: integer;
files: array of string;
lzFile, lzOutFile: string;
procedure BindFiles(outfile: string; files: array of string);
var
fout, fin: TFileStream;
x: integer;
begin
try
//Create output filename.
fout := TFileStream.Create(outfile, fmCreate);
//Get the input file names data.
for x := 0 to High(files) do
begin
try
//Read in input data.
fin := TFileStream.Create(files[x], fmOpenRead);
//Copy input file data to output filename.
fout.CopyFrom(fin, fin.Size);
finally
//Tidy up
fin.Free;
end;
end;
finally
//Tidy up
fout.Free;
end;
end;
begin
Counter := 0;
//Check for parms
if (Paramcount < 2) then
begin
writeln('Syntext Outfilename -files[n]');
ExitCode := 0;
exit;
end;
//Get output filename.
lzOutFile := ParamStr(1);
//Get number of files to bind.
for i := 3 to Paramcount do
begin
//Get input filename.
lzfile := ParamStr(i);
//Make sure string is not a plus sign.
if (lzfile <> '+') then
begin
//Inc file counter.
Inc(Counter);
end;
end;
//Make sure we have files to bind.
if (Counter <= 1) then
begin
ExitCode := 1;
writeln('You need to include more files.');
exit;
end;
//Set array size for files and reset counter.
SetLength(files, Counter);
Counter := 0;
for i := 3 to Paramcount do
begin
//Get input filename.
lzfile := ParamStr(i);
//Make sure string is not a plus sign.
if (lzfile <> '+') then
begin
//Store input filename to bind.
files[Counter] := lzfile;
//Inc file counter.
Inc(Counter);
end;
end;
//Bind the files.
BindFiles(lzOutFile, files);
ExitCode := 2;
end.
Example using the program save the script below as a batch file.
Code:
@ECHO OFF
CLS
binder.exe new.txt -files file1.txt + file2.txt + file3.txt
PUASE