in reply to How to compare User Input to a file Name

Hello Again Monks,

As stated earlier I really appreciate the advice give so far. Now for the followup question. I am using this script to clean out directories.

For this example I have 4 files in the /User/jsmith/data directory named test1-4. I want to delete all four of them so far I have the following:

#!/usr/bin/perl -w use strict; use File::Glob qw(bsd_glob); use File::Path; my $fname = "empty"; my @found_files; my @found_files1; while (! @found_files) { print "Please enter the file name: "; chomp($fname = <STDIN>); @found_files = glob( "/Users/jsmith/data/$fname*" ); if (@found_files != 1) { print "File does not exist!"; }; }; print "The following files have been deleted: \n"; print "$_\n" for @found_files; while (@found_files != 0) { rmtree (@found_files); @found_files = glob( "/Users/jsmith/data/$fname*"); };
Now my questions is: Is there a more effective way to use the rmtree to delete multiple directories other than using a while loop, like I did in my above code?

Replies are listed 'Best First'.
Re^2: How to compare User Input to a file Name
by toolic (Bishop) on Sep 24, 2008 at 00:46 UTC
    Is there a more effective way to use the rmtree to delete multiple directories other than using a while loop
    I'm a little confused by your question. Are you trying to remove directories, or just some files within a directory? If you are just trying to remove individual files that match the specified input pattern, you probably could use rmtree from File::Path to remove your list of files. But I believe the most customary usage of rmtree is to remove directories. Here is a quote from the documentation:
    the rmtree function provides a convenient way to delete an entire directory subtree from the filesystem, much like the Unix command rm -r

    It is probably more appropriate in your case to use unlink. You could replace your while loop with:

    unlink @found_files;