Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Newbie Question -- Changing File Path?

by KStowe (Novice)
on Jun 25, 2001 at 19:44 UTC ( [id://91314]=perlquestion: print w/replies, xml ) Need Help??

KStowe has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, thanks for the help last time I posted...sorry about that double post thing, I got a bit confused, but all is well after reading the FAQ.

Anyway, I would like to delete files from a filelist specified by a user (easily accomplished with the unlink function) but I would also like to let the user specify which directory the files need to be removed from? How can the unlink function be modified to allow for a path variable? After the filelist is in an array I tried:
unlink($path <@arr>);
How can I get this right? Any suggestions and ideas would be appreciated. Thanks a million. --KStowe

Replies are listed 'Best First'.
Re: Newbie Question -- Changing File Path?
by azatoth (Curate) on Jun 25, 2001 at 19:45 UTC
Re: Newbie Question -- Changing File Path?
by bwana147 (Pilgrim) on Jun 25, 2001 at 19:58 UTC

    If I get it right, you've got the path in $path and a list of mere filenames in @arr, each of which must be prepended with $path to get a list of full paths. I'd use map:

    unlink map { "$path/$_" } @arr;

    You should definitely test the return value of unlink: If anything goes wrong, it won't complain loudly but merely return a false value

    --bwana147

Re: Newbie Question -- Changing File Path?
by mikfire (Deacon) on Jun 25, 2001 at 19:59 UTC
    What about something simple? Simply tell the user that if the first command line arguement is a directory, all the listed files will be assumed to be in that directory.

    You could do something like this:

    my @arr; if ( -d $ARGV[0] ) { my $dir = shift @ARGV; @arr = map { "$dir/$_" } @ARGV; } else { @arr = @ARGV; }
    I would also warn you against using unlink the way you are. Very, very, very bad things can happen if you unlink a directory that has files in it. I would prefer to loop through the array and do them one-by-one:
    for ( @arr ) { if ( -d $_ ) { print "nice try - I cannot unlink directories\n"; next; } unlink $_; }
    mikfire
      What kind of bad things happen if I delete a directory with files in it? I'd normally just test it, but now I'm afraid. And if it's a potentially serious issue, why does Perl let you do it? Even the shell command doesn't let you delete a non-empty directory...
        Hmmm, I decided to read the docs just to make sure I knew what I was talking about. And unlink will not touch a directory unless you are root and running with the -U flag. So, it would appear that you can ignore my warning and I really didn't know what I was talking about :)

        mikfire

Re: Newbie Question -- Changing File Path?
by Anonymous Monk on Jun 26, 2001 at 00:36 UTC
    I do this in a program I wrote to get rid of unwanted files in the windows environment. When the user enters the path name append the file name on to the end of it, so instead of having file1.txt
    $array[0] = C:/MainDir/SubDir/ExtraDir/file1.txt
    When the array is all filled up with your unwanted files you just say:
    $i = $#array; for ($i;$i >= 0;$i--){ unlink $array[$i]; }
    or this works just as well
    while (@array) { $x = (pop, @array); unlink $x; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://91314]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2025-07-16 07:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.