Hello Monks,

I've been trying to diagnose trouble getting content to my site but could hardly wade through the tons of stuff, now mostly junk, that needed to be cleaned out. I tried to use filezilla to machete my way through it, but that goes very slowly when every page has an html component, a corresponding image component, and css as well. Therefore I decided that writing a script to delete entire classes of content was necessary.

I've got it far enough to where I'd like to ask for help. Here's the caller:

#!/usr/bin/perl -w use strict; use 5.010; use lib "template_stuff"; use html2; use Net::FTP; say "What keyword would you like to delete?"; my $word = <>; chomp $word; say "Delete everything associated with $word? (Y for yes)"; my $answer = <>; chomp $answer; die unless ( $answer eq 'Y' or 'y' ); # main data structure my %vars = ( word => $word, page => 'pages', image => 'images', css => 'css', ); my $rvars = \%vars; my $rftp = get_ftp_object(); my $rfiles = get_html_filenames( $rftp, $rvars ); my $rptr = kill_files( $rftp, $rvars, $rfiles ); my @killed = @$rptr; say "killed were @killed "; __END__

Q1: Why is my die statement completely ineffectual? I tried ||, grouping with parens, and "". Do I just flub the logic?

I think the intent is pretty clear with the first 2 subroutines, omitted for brevity. There's nothing exotic about getting an ftp object. The reference is passed to get_html_filenames, which gets matches, sent back as another reference to an array. kill_files is very much a work in progress:

sub kill_files { use Net::FTP; use 5.010; use Path::Class; my ( $ftp, $rvars, $rfiles ) = @_; my %vars = %$rvars; my @files = @$rfiles; my @killed_files; # kill html files for my $html (@files) { my $obj = file( '', $vars{page}, $html ); my $string = $obj->stringify; say "string is $string"; my $return = $ftp->delete($string); if ($return) { push( @killed_files, $html ); } } # chop off ending foreach (@files) { $_ =~ s/\.html//; } say "now files are @files"; # kill image directories for my $dir (@files) { my $obj = dir( '', $vars{image}, $dir ); my $string = $obj->stringify; say "string is $string"; my $return = $ftp->rmdir($string,1); if ($return) { push( @killed_files, $dir ); } } # kill css file my $css_obj = file( '', $vars{css}, $vars{word} . '1.css' ); my $string = $css_obj->stringify; say "string is $string"; my $return = $ftp->delete($string); if ($return) { push( @killed_files, $html ); } return \@killed_files; }

I was getting good behavior out of this for building up the strings of files and directories to be deleted. The trouble came when I actually tried to delete them, and I got the following typical diagnostics:

Net::FTP=GLOB(0x92dc200)>>> DELE /images/potato1/. Net::FTP=GLOB(0x92dc200)<<< 550 /images/potato1/.: Is a directory Net::FTP=GLOB(0x92dc200)>>> RMD /images/potato1/. Net::FTP=GLOB(0x92dc200)<<< 550 /images/potato1/.: Directory not empty

I was hoping that I could just cut off the directory branch with the rmdir method from Net::Ftp with recurse set to 1, but it would appear that I cannot.

Q2) Do I have to go into every one of these directories, delete every file, and only then will I be able to delete the directory itself? What's the best way of doing that without having to make a bunch of ftp requests?

As usual I'm also accepting any tips for style and reducing superfluous notation. Thanks for your comment.


In reply to script for deleting html, images and css from server by Aldebaran

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.