Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2014-01

Adding files to a tar file

4 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

luciano de souza

Hello all,

This code should add all the files of a directory in a tar file.

program e01;
{$mode objfpc}{$H+}
uses
libtar, sysutils;
var
archive: TTarWriter;
search: TSearchRec;
BEGIN
archive := TTarWriter.create('arquivo.tar');
with archive do
begin
try
FindFirst('/home/luciano/Documentos/*', faAnyFile, search);
repeat
if (search.name  '.') and (search.name  '..') then
AddFile(search.name);
until FindNext(Search) = 0;
finally
free;
end;
end;
END.

The program compiles, but I got an error. The message says that the first file of the directory can't be opened.

What is wrong?

My second doubt is: I am running this program in Ubuntu 13.10. If I run it in Windows, will a external library be necessary or all the code is included in lib tar unit?

Regards,

Luciano de Souza

Reply by email

Flávio Etrusco
to luciano de souza
luciano de souza wrote 34 lines
Hello all, This code should add all the files of a directory in a tar file. program e01; {$mode objfpc}{$H+} uses libtar, sysutils; var archive: TTarWriter; search: TSearchRec; BEGIN archive := TTarWriter.create('arquivo.tar'); with archive do begin try FindFirst('/home/luciano/Documentos/*', faAnyFile, search); repeat if (search.name '.') and (search.name '..') then AddFile(search.name); until FindNext(Search) = 0; finally free; end; end; END. The program compiles, but I got an error. The message says that the first file of the directory can't be opened. What is wrong?

Probably because Search.Name contains only the filename, but AddFile expects an absolute path (or relative to GetCurrentDir)?

Show quoted text 5 lines
My second doubt is: I am running this program in Ubuntu 13.10. If I run it in Windows, will a external library be necessary or all the code is included in lib tar unit? Regards,

AFAICS all the code is included in the libtar unit.

-Flávio

Reply by email

luciano de souza
to Flávio Etrusco
Yes, that's the mistake. I forgot the fact "search.name" does not
returns a full path. This code works very well.
program e01;
{$mode objfpc}{$H+}
uses
libtar, sysutils;
var
archive: TTarWriter;
search: TSearchRec;
path: string;
BEGIN
archive := TTarWriter.create('arquivo.tar');
with archive do
begin
try
path := paramstr(1);
FindFirst(path + '/*', faAnyFile, search);
repeat
if (search.name  '.') and (search.name  '..') then
AddFile(path + '/' + search.name);
until FindNext(Search) = 0;
finally
free;
end;
end;
END.

Thank you! This class is really very easy to use.

2014/1/21, Flávio Etrusco :

Show quoted text 54 lines
On Tue, Jan 21, 2014 at 10:45 PM, luciano de souza wrote: > Hello all, > > This code should add all the files of a directory in a tar file. > > program e01; > {$mode objfpc}{$H+} > > uses > libtar, sysutils; > > var > archive: TTarWriter; > search: TSearchRec; > > BEGIN > archive := TTarWriter.create('arquivo.tar'); > with archive do > begin > try > FindFirst('/home/luciano/Documentos/*', faAnyFile, search); > repeat > if (search.name '.') and (search.name '..') then > AddFile(search.name); > until FindNext(Search) = 0; > finally > free; > end; > end; > END. > > The program compiles, but I got an error. The message says that the > first file of the directory can't be opened. > > What is wrong? Probably because Search.Name contains only the filename, but AddFile expects an absolute path (or relative to GetCurrentDir)? > My second doubt is: I am running this program in Ubuntu 13.10. If I > run it in Windows, will a external library be necessary or all the > code is included in lib tar unit? > > Regards, AFAICS all the code is included in the libtar unit. -Flávio _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Luciano de Souza

Reply by email

waldo kitty
to luciano de souza: This code should add all the files of a directory in a tar file. program e01; {$mode…
luciano de souza wrote
The program compiles, but I got an error. The message says that the first file of the directory can't be opened. What is wrong?

your code uses faAnyfile... perhaps you need to be more restrictive? is your problem caused by a sub-directory with the directory of files you are trying to archive? or perhaps there's a soft/hard-link?

i also do not see a findclose call after you finish with findfirst/findnext... granted, it may not be needed since you are exiting the program but it is more proper, IMHO...

NOTE: No off-list assistance is given without prior approval. Please keep mailing list traffic on the list unless private contact is specifically requested and granted.

Reply by email