Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

About this user

Ben Aurel

1 total

Fix for the Codesamples of the Haxe/Neko Book from Wrox

- downloads the zip
- unzips the file and all nested zips
- removes all the spaces in filenames and folders and replaces it with "_"

#!/usr/bin/perl
# 
# Fix for the Codesamples of the Haxe/Neko Book from Wrox
# ======================================================
# - download the zip
# - unzip the file and all nested zips
# - remove all the spaces in filenames and folders and re
# replace it with "_"
#
# Needs to be installed on your machine:
# - 'unzip' program (can be configured)
# - 'wget' program
#
# ben.aurel@gmail.com,25.04.08

use strict;
use warnings;

# -------------- Configuration ---------------------
my $dir = ".";

my $unzipcmd = "unzip";

my $URL_of_haxe_book = "http://media.wiley.com/product_ancillary/37/04701221/DOWNLOAD/9780470122136_code.zip";

# -------------- Do not change ---------------------
my @a=split(/\//,$URL_of_haxe_book);
my $zip_file_url = $a[$#a];
my $zip_file= 'haxe_neko_book.zip';

system("wget $URL_of_haxe_book") == 0 or die "Couldn't download the book";
rename "$zip_file_url","$zip_file" or die "Couldn't rename the downloaded zip file $! \n";
system("unzip $zip_file") == 0 or die "Couldn't unzip the book";





&recurse_extract($dir);

sub recurse_extract() {
	my $dir = shift;
	opendir my $dh , $dir  	 
		or die "error: $! \n"; 
	while ($_ = readdir $dh) {
		next if /^\.{1,2}$/;
		my $path = "$dir/$_";
                my $filename= "$_";
		print "in path: $path \n" if -f $path;
                my $filename2 = &remove_space_in_filename($filename);
                if ($filename2 ne $filename) {
                   rename "$dir/$filename","$dir/$filename2" or warn "Unable rename: $!\n";
                }      
                if ($filename2 =~ m/\.zip$/) {
                   &unzip_file("$dir", "$filename2");
                }
                $path = "$dir/$filename2";
                print "call recurse with $path\n";
                &recurse_extract($path) if -d $path;

	}
                                          
	print "in dir: $dir \n" or print "error - $! \n";
}

&recurse_filenames($dir);

sub recurse_filenames() {
	my $dir = shift;
	opendir my $dh , $dir  	 
		or die "error: $! \n"; 
	while ($_ = readdir $dh) {
		next if /^\.{1,2}$/;
		my $path = "$dir/$_";
                my $filename= "$_";
		print "in path: $path \n" if -f $path;
                my $filename2 = &remove_space_in_filename($filename);
                if ($filename2 ne $filename) {
                   rename "$dir/$filename","$dir/$filename2" or warn "Unable rename: $!\n";
                }      
                $path = "$dir/$filename2";
                &recurse_filenames($path) if -d $path;

	}
                                          
	print "in dir: $dir \n" or print "error - $! \n";
}


sub remove_space_in_filename() {
   my $file_w_space = shift;

   (my $file_nospace = $file_w_space) =~ s/\s/_/g;
   return $file_nospace;
}

sub unzip_file() {
   my $dir = shift;
   my $filename = shift;
   my $path2zip = "$dir/$filename";
   system("$unzipcmd $path2zip -d $dir") == 0 or die "Couldn't unzip $path2zip";

}



1 total