another recursive question.

Is it possible to take all the files of the same name but with different extensions (i.e. test.txt, test.xls, test.doc) and put them in 1 .zip file, and do this for every directory in a tree?
kaleidAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MindoCommented:
Yes, it's possible, why not? You just list the files you want to add to the zip file and and tell the program to put them in a *.zip file. It doesn't matter what it's name. To make it recursive, just go through the directories and repeat this action on each of them.
0
jhurstCommented:
assuming that you are operating under unix or xenix or something decent like that:

@listOfFiles=`find directory -name \\nameToLookFor* -print`;
will generate an list with all files that start with nameToLookFor and are in any directory rooted at directory.
0
ozoCommented:
use File::Find;
find(\&wanted, 'directory');
sub wanted {
  /^nameToLookFor/ && push @listOfFiles,$File::Find::name;
}
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

kaleidAuthor Commented:
Im using winnt, I can locate the files, but im not sure exactly how to zip them up automatically.
0
kaleidAuthor Commented:
Adjusted points to 20
0
kaleidAuthor Commented:
Still cant get the script to compress the files...is it possible to automate this process so it goes through every folder and zips up all files with the same name?
I tried push but doesnt seem to work.
Please help...
Thanks.

 
0
kaleidAuthor Commented:
Adjusted points to 50
0
adam923Commented:
ozo: what's the advantage to using File:Find over @files = <"test.*">;
??
kaleid: post your code that's not working... is it not reaching all desired files or is the zip call failing?
0
Kim RyanIT ConsultantCommented:
There is a CPAN library for compressing files, Compress::Zlib. I am not sure if it will create a multi file archive though.

Here is an example of how it compress binary files in a directory each to a seperate zip file.

use Compress::Zlib ;

foreach $file (@listOfFiles) {
  $gz = gzopen($file, "rb")
  or die "Cannot open $file: $gzerrno\n" ;

  print $buffer
     while $gz->gzread($buffer) > 0 ;
  die "Error reading from $file: $gzerrno\n"
  if $gzerrno != Z_STREAM_END ;  
     $gz->gzclose() ;
}


0
ozoCommented:
The advantage to using File:Find over @files = <"test.*">; is that File:Find can recursively visit every sub-directory in a tree

Do you want each of the filed compressed separately?
Or all the files compressed in one zip?
Or one zip per each directory?
0
kaleidAuthor Commented:
ozo,
I am trying to compress the files into one zip.  That is, all the files with the same name which will in most cases give me many zip files per directory.  I used the code that you had posted but i dont think it created the zip files.  Do I have to manipulate what you had written (other than change the directory name) to make it work?

#!usr/bin/perl

use File::Find;
                   find(\&wanted, 'directory');
                   sub wanted {
                     /^nameToLookFor/ && push @listOfFiles,$File::Find::name;
                   }

0
kaleidAuthor Commented:
Adjusted points to 70
0
Kim RyanIT ConsultantCommented:
Ozo's code will built up your list of files. You still need to run extra code across this array to compress your files. The Compresss::Zlib should get you started. Alternatively, get something like the 'tar' utiltiy for Windows. This will make one big file out of a directory tree
0
kaleidAuthor Commented:
teraplane: will zlib work with winnt?
0
kaleidAuthor Commented:
Adjusted points to 100
0
Kim RyanIT ConsultantCommented:
Yes, I have tried it out and it generates zip files without any problems
0
kaleidAuthor Commented:
teraplane:  did it work recursively?  I am unfamiliar with zlib.  Do I unzip it to the /bin directory after I have downloaded it?
0
Kim RyanIT ConsultantCommented:
ozo's code does the recursive search and builds up the list of files. My code then compresses all these files. I suggest you download the zlib package from CPAN, check the README instructions, install it and then try doing some simple examples.

The best way for you to learn is by trial and error, rather than me giving a complete solution to one specific problem. This sample code should point you in the right direction
0
kaleidAuthor Commented:
Adjusted points to 125
0
kaleidAuthor Commented:
Adjusted points to 150
0
kaleidAuthor Commented:
Adjusted points to 200
0
kaleidAuthor Commented:
is there a way to compress these with the dos version of winzip?
please help.
0
omereCommented:
Use the following, it should do it all (you need pkzip in your path,
if you don't have it - remove the system() line and after the program
runs, run c:\wherever\pkzip.exe -a -ex myzip.zip @listfile.pkz)

use File::Find;

find(\&wanted, 'c:\');  # or whatever, substitute C:\ for the directory

open(OUT, ">listfile.pkz");
print OUT join("\n", @listOfFiles);
close(OUT);

system("pkzip -a -ex zipname @listfile.pkz");

sub wanted {
  /^filename/ && push @listOfFiles,$File::Find::name; # substitute filename
}

Seems final enough to me :)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kaleidAuthor Commented:
Thank you, Ive given it a try and
got this message when trying to run:

"In string, @listfile now must be written as \@listfile at D:\temp\test\perl\d2test2.pl line 11, near 'pkzip -a -ex zipname @listfile'"

running this script...

#!usr/bin/perl

use File::Find;

find(\&wanted, 'D:\temp\test\perl');  # or whatever, substitute C:\ for the directory

open(OUT, ">listfile.pkz");
print OUT join("\n", @listOfFiles);
close(OUT);

system("pkzip -a -ex zipname @listfile.pkz");

sub wanted {
  /^filename/ && push @listOfFiles,$File::Find::name; # substitute filename
}
0
omereCommented:
Add a \ before @, or use:
system("pkzip -a -ex zipname " . '@listfile.pkz'
0
omereCommented:
Add a \ before @, or use:
system("pkzip -a -ex zipname " . '@listfile.pkz');
0
kaleidAuthor Commented:
Ok,
(thank you by the way, this is the most progress ive made to date...)
it worked but it zipped everything into one zip file.  What I need it to do is zip files with only similar filenames for example:

in directory d:\temp\test\perl\

test1.txt
test1.xls
test1.ppt

test2.txt
test2.xls
test2.ppt

after running the script i would like to achieve two (or however many sets in a directory) zip files.  one "test1.zip" and one "test2.zip"


thanks.
0
omereCommented:
Ah, that's something else then.

This would require some more work, as you would need to recurse through all of the subdirectories, and in each sub directory create a hash with the list of all filenames, add those with similar names to it and then zip it - then moving on.

If you don't know how to do it, lemme know and I'll post some code up.
0
kaleidAuthor Commented:
EXACTLY!!!...
problem is...i dont know how to do it, and ive needed to do it for weeks.
any help you can offer is very much appreciated.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.