Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

After listing the folder in a directoy, i unable to zip the folders by using system command.

by perladdict (Chaplain)
on May 21, 2013 at 13:31 UTC ( #1034533=perlquestion: print w/ replies, xml ) Need Help??
perladdict has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
With the Below code i am trying to list directory content in windows OS, i am able to print the directory content.once after obtaining the subfolders in the directory, i wanted to create the zip for the subfolders like bk_20130520.zip
#!/usr/bin/perl print "Hellow World\n"; opendir(D,"C:\\xxx\\xxxx\dirname") or die "can't open the directory\n" +; my @folders = grep {(!/^\./) && (!/^\.\./)} readir(D); foreach (@folders) { $prefix = "bk"; Print "$_\n"; system("Zip $_ $prefix.$_"); }
Output
Hello World 20130516 20130517 20130520 Zip Error: Nothing to do! <20130516.zip> Zip wanrning name not matched: 20130516
The zip feature i have not used before in script,monks it would be a kind if you can guide me to get this zip created for the subfolders.

Comment on After listing the folder in a directoy, i unable to zip the folders by using system command.
Select or Download Code
Re: After listing the folder in a directoy, i unable to zip the folders by using system command.
by marto (Chancellor) on May 21, 2013 at 13:42 UTC

    You're using a system program to try this, not perl. The syntax for zip is wrong, see man zip. I suggest you don't blindly call system commands from your code, check what you're doing before you try to automate it.

    Your options are that you learn how to achieve this with the tools shipped with your OS, or use something like the Archive::Zip module.

    Update: Also, this can't be your code, Print "$_\n"; would throw an error.

Re: After listing the folder in a directoy, i unable to zip the folders by using system command.
by ww (Bishop) on May 21, 2013 at 14:11 UTC
    You probably need to CD (i.e., chdir() ) to each folder or to use complete absolute paths for each dir you're trying to zip/.

    But -- more perl-ishly -- go ahead with perl's native and module capabilities, rather than shelling out.


    If you didn't program your executable by toggling in binary, it wasn't really programming!

Re: After listing the folder in a directoy, i unable to zip the folders by using system command.
by hdb (Parson) on May 21, 2013 at 14:27 UTC

    Some comments:

    1. readdir (with double d) returns all contents of a directory, not only sub-directories.
    2. Your assignment to @folders suggests that you only want sub-directories?
    3. Apart from . and .. you should also exclude the directories starting with your $prefix. Otherwise, you backup them as well if you run the script twice. (Probably that is what you want?)
    4. Your zip command will not work with folder names containing blanks.

Re: After listing the folder in a directoy, i unable to zip the folders by using system command.
by Anonymous Monk on May 21, 2013 at 22:22 UTC
    #!/usr/bin/perl -- use strict; use warnings; #~ use autodie qw/ chdir system /; use autodie; use Win32::ShellQuote qw/ quote_system quote_system_cmd /; use File::Find::Rule qw/ find /; use File::Which qw/ which /; Main( @ARGV ); exit( 0 ); sub Main { 2==@_ or die "Usage: $0 dirtobackups prefix\n"; my( $dir , $prefix ) = @_; chdir $dir; ## or dies on its own my @folders = find( directory => maxdepth => 1, in => [ '.' ] ); shift @folders if $folders[0] eq '.'; ## Grrr for my $folder ( @folders ){ my $source = $folder; my $destination = "$prefix.$folder"; zip( $destination , $source ); } } our $infozip; sub zip { my( $destination, $source ) = @_; unless( defined $infozip ){ $infozip = which( 'zip.exe' ); if( $infozip ){ my $izv = quote_system_cmd( $infozip, '--version' ); if( not qx{$izv} =~ m{Info-ZIP} ){ $infozip = ''; die "can't find Info-ZIP zip.exe" } } } $infozip or die "no zip.exe"; $destination =~ s/(?:\.zip)?$/.zip/i; system quote_system( $infozip, '-r', '-v', '-9', $destination, $source ); } __END__

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1034533]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2014-08-23 00:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (168 votes), past polls