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

Hi All, I am trying to delete a folder based on age. I have used the rmtree command to delete, but it is throwing ans error as in the inner folders there exists a file with long name. Can you please let me know how to handle such names. Thanks in Advance Regards, Anusha

Replies are listed 'Best First'.
Re: Delete a folder with long path
by Discipulus (Canon) on Jan 05, 2017 at 09:25 UTC
    Hello Anusha4268 and welcome to the monastery!

    I guess you are using rmtree from the core module File::Path ..right?

    Can you post a short example that demonstrate the behaviour you are facing? Remove all age checks and post the minimum code that reproduce your error along with the minimal file name lenght that provoke it. Remember to wrap the code in <code> .. </code> tags for readability.

    Also tell us which OS are you running and what the error states: rmtree is a legacy interface to remove_tree one which provide also the possibility to specify options like verbose and safe .. Are you using them?

    You receive the same error with the builtin system call ( rm -r on Linux or the Windows commands rd /S /Q )?

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Delete a folder with long path
by choroba (Cardinal) on Jan 05, 2017 at 10:43 UTC
    I'm not able to replicate the problem, even if I use the maximal filename length available on my system.
    #! /usr/bin/perl use warnings; use strict; use Cwd; use File::Path qw{ rmtree make_path }; use constant MAX_FILENAME_LENGTH => 255; my $start_dir = getcwd(); my $depth = 200; my $step = 'a' x MAX_FILENAME_LENGTH; for (1 .. $depth) { make_path($step); chdir $step; } open my $fh, '>', "$step" or die $!; chdir '..' for 1 .. $depth; rmtree($step) or die $!; END { chdir $start_dir; if (-d $step) { rmtree($step) or warn $!; } }
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Delete a folder with long path
by BillKSmith (Monsignor) on Jan 06, 2017 at 03:45 UTC
    It is likely that your problem is a special character (usually a space) rather than the number of characters in a directory name. In windows, the simplest solution often is to use the alternate (remember DOS 8.3 file name format) version of the name.
    Bill