in reply to Syntax Error deleting files and folders in a directory

Welcome to the Monastery!

        If your eyes hurt after you drink coffee, you have to take the spoon out of the cup.
              -Norm Crosby

  • Comment on Re: Syntax Error deleting files and folders in a directory

Replies are listed 'Best First'.
Re^2: Syntax Error deleting files and folders in a directory
by tobyink (Canon) on Dec 28, 2013 at 19:34 UTC

    You missed one...

    • while (my$file = @files) {...} is an infinite loop, and the variable $file will just be a number, not a filename. The intention was probably something like while (my $file = shift @files) {...}, though the following would be safer: while (@files) {my $file = shift @files; ...} (just in case there is a file named "0").
    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
      Aah - yes - I had not read that file down ... thanks.

      Also - these (completely incorrect) loops:

      FILE: while (my$file = @files) { for my $kp (@KEEP) { next FILE if $file eq $kp; } rmtree($deletes); } return; }
      can be written in a more perlishly as :
      my %keepers = map {$_=>1} @KEEP ; # OR declare %KEEP instead of @KEEP rmtree ([grep {not $keepers{$_} }@files]);
      (untested).

      You probably need to chomp(@files) too, before this operation.

      File::Path documentation prefers remove_tree over the legacy rmtree.

              If your eyes hurt after you drink coffee, you have to take the spoon out of the cup.
                    -Norm Crosby

        Well, now I have much to think about. I will repost in a couple of days once I scour my Programming Perl manual.

      Thank you Tobyinc

Re^2: Syntax Error deleting files and folders in a directory
by smturner1 (Sexton) on Dec 29, 2013 at 00:19 UTC

    Thank you for your input. I corrected all of the bullet points you suggested with one exception; my @files is declared twice because I want to copy files and directories from the $destinationDir to the $deletes var. Can I do this with only one of the methods @files?

    use strict; use warnings; use autodie; use File::Path qw(make_path rmtree); #Set vars my $destinationDir = 'C:\Users\Shaun\Documents\$website'; my $file; #Set File/Folders to exclude from delete my @KEEP = ( 'Prg421 pdf', ); #Set routine to do selective delete sub selective_delete { opendir my ($deletes), $destinationDir; #Set params my @files = glob( "$deletes/*" ); my @files = ( readdir $deletes ); #loop through directory copying all files and folders to $del for dele +tion FILE: while (my$file = @files) { for my $kp (@KEEP) { next FILE if $file eq $kp; } rmtree($deletes); } return; }

      You know how troublesome it is when there are two (or more) people in the room with you who have the same name and you want to address only one of them? Well Perl has the same trouble with variables. You have introduced two array variables with the same name in the same room (for room read: lexical scope (set of {})) - kinda like having identical twins dressed and groomed the same with the same name in the room. Perl warns you about that, then just uses one of them completely ignoring the other.

      Actually your whole script is still full of bugs. I'll let you go off and read the rest of the replies you got so you can fix some of them yourself. Come back if you don't get it sorted out.

      BTW, a good technique that may save you a lot of grief is to comment out you rmtree code and use a print instead as a trial run to see what is going to be deleted when you reinstate the rmtree and run again.

      True laziness is hard work

        There is no doubt that I need to go back to the drawing board. I am in the process of researching each aspect of the junky code I put up for scrutiny. Learning is and can be a very frustrating process, but my frustration is a good indicator of learning! :) Thanks for your help. I will republish this in a couple of days once I know I am closer to where I need to be. Thank you for your patience.