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

remove files starting with white from its folders and sub folders using perl?

by gpssana (Initiate)
on Apr 17, 2017 at 10:18 UTC ( [id://1188118]=perlquestion: print w/replies, xml ) Need Help??

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

sub DelTask{ my ($w1) = shift; my $m1 = $w1 . '/*'; my @d1 = glob $m1; foreach my $f (@d1) { my $d1 = basename($f); if ($d1 =~ /^white*/) { my $f1 = $f . '*'; print $f1; unlink glob $f1; unlink $f; } } } DelTask($o_dir);
Am unable to delete the files whatever starts with white from my folders and sub folders using perl.
How can i fix this issue.Here the $d1 is not fetching the folders and sub folders filename .

Replies are listed 'Best First'.
Re: remove files starting with white from its folders and sub folders using perl?
by shmem (Chancellor) on Apr 17, 2017 at 14:17 UTC

    Please omit the using perl part in your questions. Did you notice that this site is all about using perl?

    Here the $d1 is not fetching the folders and sub folders filename .

    So you are doing something wrong. Let's revise your code. First, your variable naming is poor. They don't tell what's meant, they could also be $blorfldyick or $foo or $captain_morgan or $jonathan_swift.

    sub DelTask{ my ($w1) = shift; my $m1 = $w1 . '/*'; my @d1 = glob $m1;

    The first argument to this function is a directory. So you should check whether it really is a directory, and bail out if it isn't:

    unless (-d $w1) { warn "argument to DelTask is not a directory.\n"; return; }

    You don't need the temporary variable $m1, since you can construct a string on the fly:

    my @d1 = glob "$w1/*";

    Then you loop over @d1 (again, poor variable naming) using the temporary variable $f which should be $file. Agree?

    foreach my $f (@d1) { my $d1 = basename($f);

    Where in your posted code is use File::Basename? or did you roll your own basename() function? Where is that in your code? See, omissions like this one make it hard to understand your question and providing an answer. You assume that we might tell what's meant, but to assume makes an ass from u and me.

    if ($d1 =~ /^white*/)

    Where did you read that the string white refers to whitespace? Read perlre. Seriously, read perlre. You did? skip the rest of this paragraph. You didn't yet? I'm going to swear and insult you: You fool of a monk, goddamn, for god's sake and our souls peace, GO READ perlre!

    Done? Ok, then you already know that you want this instead:

    if ($d1 =~ /^\s+/)

    In the next part you are trying to fix errors at the wrong place.

    { my $f1 = $f . '*'; print $f1; unlink glob $f1; unlink $f; } } }

    glob ought to have given you the filenames already, so there is no need to invoke it again. But, again, you are not checking whether the thing in $f is a file or a directory. Unlinking a directory containing files is a very bad idea.

    update: If you want to remove recursively, this is the place to invoke DelTask($f)

    Fix these errors, and then your call

    DelTask($o_dir);

    will yield the desired effect, or tell you what went wrong.

    Happy coding!

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
      > Please omit the using perl part in your questions. Did you notice that this site is all about using perl

      Come on, it might be a bit annoying but it helps identifying this user. ;)

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

      The above modifications are not considering the filenames which starts with task from the folders and sub folders
        The above modifications are not considering the filenames which starts with task from the folders and sub folders

        which starts with task - you mean "which start with whitespace" surely. If you code the same way as you post, your programs will be full of glaring bugs.

        Show your modified code.

        perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Re: remove files starting with white from its folders and sub folders using perl?
by 1nickt (Canon) on Apr 17, 2017 at 11:09 UTC

    if ($d1 =~ /^white*/)

    This matches any string consisting of 'whit' at the beginning followed by zero or more 'e' characters. Probably not what you wanted.

    perl -Mstrict -WE ' say "`$_` " . ( $_ =~ /^white*/ ? "match" : "no match" ) for ("while", "whine", "white", "whitf", "whit ", "whit!", "whit") +; '
    `while` no match `whine` no match `white` match `whitf` match `whit ` match `whit!` match `whit` match

    Hope this helps!


    The way forward always starts with a minimal test.
Re: remove files starting with white from its folders and sub folders using perl?
by Corion (Patriarch) on Apr 17, 2017 at 10:20 UTC
      > glob does not handle whitespace well

      It does, it treats it as a separator. Backslash it if you want it to interpret it literally:

      say for glob 'a b'; # Matches "a" and "b". say for glob 'a\ b'; # Matches "a b".

      ($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,
      How i can give opendir it will read only the respected directory location.Here i want to read all folders and sub folders

        How i can give opendir it will read only the respected directory location.Here i want to read all folders and sub folders

        Sometimes there isn't a way to do everything at once and you have to write code to perform the logic.

        Of course, as 1nickt pointed out, there are modules out there which have already coded the logic, so you can do it all at once.

        If you are constrained (for real or even if only in your imagination) from using a module, you'll need to write the code, and use of opendiris prescribed. (Hint: You'll have to do a loop, and recursion is usually employed as well.)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-03-29 05:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found