in reply to Re: Syntax Error deleting files and folders in a directory
in thread Syntax Error deleting files and folders in a directory

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; }

Replies are listed 'Best First'.
Re^3: Syntax Error deleting files and folders in a directory
by GrandFather (Saint) on Dec 29, 2013 at 00:50 UTC

    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.