how to rename 200 files in many sub-directories?

G

Geoff Cox

Hello,

I have over 200 zip files in about 100 sub-directories of say c:\docs.

Each zip file contains one MS Word doc file. The name of the doc file
is different from that of its parent zip file.

I would like to run wzunzip.exe (command line version of WinZip) to
expand each zip file, rename the doc file to name of its zip file and
then delete the zip file....

I need to loop through the sub-directories, expanding each zip file,
renaming the doc file produced and deleting the zip file, in each
sub-directory. I guess I need to put the name of the zip file into a
variable so that after running wzunzip.exe I can then rename the doc
file produced etc etc...

Any ideas please!

Thanks

Geoff
 
G

Gregory Toomey

Geoff Cox said:
Hello,

I have over 200 zip files in about 100 sub-directories of say c:\docs. ....
Any ideas please!
Thanks
Geoff

You can use Archive::Zip in Perl to manipulate the zip files.

gtoomey
 
J

Jonathan Stowe

Geoff Cox said:
Thanks for info - can I use this with Perl under Windows 98 (SE) ?

I believe that it is actually included as standard with the activestate
Perl distribution - if not (I don't have a windows machine handy to check)
it *is* available for installation with PPM.

/J\
 
J

John W. Krahn

Geoff said:
I have over 200 zip files in about 100 sub-directories of say c:\docs.

Each zip file contains one MS Word doc file. The name of the doc file
is different from that of its parent zip file.

I would like to run wzunzip.exe (command line version of WinZip) to
expand each zip file, rename the doc file to name of its zip file and
then delete the zip file....

I need to loop through the sub-directories, expanding each zip file,
renaming the doc file produced and deleting the zip file, in each
sub-directory. I guess I need to put the name of the zip file into a
variable so that after running wzunzip.exe I can then rename the doc
file produced etc etc...

Any ideas please!

Something like this (untested):

use warnings;
use strict;

use File::Find:

my $dir = 'c:/docs';
# The -p option from Info-ZIP's unzip sends file data to stdout.
# It may not be the same in WinZIP's wzunzip
my $unzip = 'wzunzip.exe -p';

find( sub {
( my $name = $_ ) =~ s/\.zip$/.doc/i or return;
open my $doc, '>', $name or die "Cannot open $name: $!";
binmode $doc;
print $doc qx( "$unzip $_" ) or die "Cannot print to $name: $!";
unlink $_ or warn "Cannot delete $_: $!";
}, $dir );

__END__



John
 
G

Geoff Cox

On Sun, 17 Aug 2003 12:07:43 GMT, "John W. Krahn" <[email protected]>
wrote:

John,

Can you help with this? I am using ActivePerl with Windows 98 (SE)
.... have been looking at the use of Archive::Zip .. not clear how
Archive:: differs from File:: ?

When I use File::Find I get an syntax error message. Any ideas?

Cheers

Geoff
 
G

Geoff Cox

You can use Archive::Zip in Perl to manipulate the zip files.

Thanks for the info - have started to look at the example files which
come with Archive::Zip but any suggestions re where I can find info
on how to use it?

Incidentally it was installed with ActivePerl 5.8 as others have
said..

Cheers

Geoff
 
T

Tad McClellan

Geoff Cox said:
When I use File::Find I get an syntax error message. Any ideas?


Fix the invalid syntax.

We (still) cannot help you fix your code if you do not show us your code...
 
T

Tad McClellan

Archive::Zip but any suggestions re where I can find info
on how to use it?


Have you tried reading the documentation that comes with the module yet?


perldoc Archive::Zip
 
G

Geoff Cox

Fix the invalid syntax.

It turned out to be a : instead of a ; at the end of the line..
We (still) cannot help you fix your code if you do not show us your code...

Problem is - I am trying to get an idea of where to start! Have used
perl a few years ago and it is slow to come back..

I now have 2 routes - use Archive::Zip or File::Find ....

Cheers

Geoff
 
B

Bart Lateur

Geoff said:
I would like to run wzunzip.exe (command line version of WinZip) to
expand each zip file, rename the doc file to name of its zip file and
then delete the zip file....

With Perl? Nah.... you'd better use Archive::Zip. From the docs:

extractMember( $memberOrName [, $extractedName ] )

The second optional argument is the name to save the extracted file
under.

You can get a list of existing members (the files in the archive) with
the "members" method.

So what you need to do is make a copy of the filename of the zipfile,
and replace the extension with ".doc".

($docname = $zipname) =~ s/\.zip$/.doc/i;
 
B

Bart Lateur

Geoff said:
Thanks for info - can I use this with Perl under Windows 98 (SE) ?

Yuppers.

If you don't have it, you can get it from
<http://ppm.activestate.com/PPMPackages/zips/> (choose your proper
subdirectory). Fetch and uncompress Archive::Zip and Compress::Zlib (at
least you probably will have the latter, as PPM uses it to decompress
..gz files), and use PPM to install it/them.
 
G

Geoff Cox

With Perl? Nah.... you'd better use Archive::Zip. From the docs:

Bart,

Getting close now! I have not yet understood to to use Archive::Zip
code though - could you give me a few lines of code to show how you
get the names of the zip files, extract the doc file within the zip
file and then rename it?! I know this is asking a lot but am stuck at
the moment and cannot find any basic tutorial stuff for this!

Cheers

Geoff


extractMember( $memberOrName [, $extractedName ] )

The second optional argument is the name to save the extracted file
under.

You can get a list of existing members (the files in the archive) with
the "members" method.

So what you need to do is make a copy of the filename of the zipfile,
and replace the extension with ".doc".

($docname = $zipname) =~ s/\.zip$/.doc/i;
 
G

Gunnar Hjalmarsson

Geoff said:
Getting close now! I have not yet understood to to use Archive::Zip
code though - could you give me a few lines of code to show how
you get the names of the zip files, extract the doc file within the
zip file and then rename it?! I know this is asking a lot but am
stuck at the moment and cannot find any basic tutorial stuff for
this!

Have you considered to check the module docs?
http://search.cpan.org/author/NEDKONZ/Archive-Zip-1.06/lib/Archive/Zip/FAQ.pod

The distribution includes quite a few examples:
http://search.cpan.org/src/NEDKONZ/Archive-Zip-1.06/examples/
 
G

Geoff Cox

On Mon, 18 Aug 2003 10:32:50 +0200, Gunnar Hjalmarsson

Gunnar,

thanks for the info - have been there but having difficulty getting
started!

I can traverse the directories and expand the zip files using
File::Find but not so far with Archive::Zip ...

How do I use Archive::Zip to go through all the sub-directories of say
c:\docs without having to give all the sub-directory paths?

Also how do I rename the 1 MS Word doc which is in each zip file to
have the name of the parent zip file?

Thanks

Geoff
 
J

John W. Krahn

Geoff said:
Getting close now! I have not yet understood to to use Archive::Zip
code though - could you give me a few lines of code to show how you
get the names of the zip files, extract the doc file within the zip
file and then rename it?! I know this is asking a lot but am stuck at
the moment and cannot find any basic tutorial stuff for this!


Something like this:

use warnings;
use strict;

use File::Find;
use Archive::Zip;

my $dir = 'c:/docs';

find( sub {
( my $name = $_ ) =~ s/\.zip$/.doc/i or return;
my $zip = Archive::Zip->new( $_ );
$zip->extractMember( ($zip->memberNames)[ 0 ], $name );
unlink $_ or warn "Cannot delete $_: $!";
}, $dir );

__END__



John
 
J

John W. Krahn

Geoff said:
John,

I wonder if you could just explain what is happening in the sub ..?

Sure, it's not that complicated. :)


$_ contains the name of the current file which is assigned to $name. A
substitution is performed on $name and if it ends with the four
characters '.zip' (or '.ZIP' or '.Zip', etc.) they are replaced with the
four characters '.doc'. If the substitution was not successful (the
file name didn't end in '.zip') return from the sub. If successful $_
will contain 'somefile.zip' and $name will contain 'somefile.doc'.


Create an Archive::Zip object using the file name in $_.

$zip->extractMember( ($zip->memberNames)[ 0 ], $name );

Use $zip->memberNames to get the first file name from the zip archive
and use $zip->extractMember to store the contents of that file using the
file name in $name.


Delete (unlink) the file $_ or complain if you can't.


Use the directory name in $dir as the starting point in the search.



John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top