Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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 ( [id://1188136]=note: print w/replies, xml ) Need Help??


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

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'

Replies are listed 'Best First'.
Re^2: remove files starting with white from its folders and sub folders using perl?
by LanX (Saint) on Apr 17, 2017 at 15:13 UTC
    > 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!

Re^2: remove files starting with white from its folders and sub folders using perl?
by gpssana (Initiate) on Apr 18, 2017 at 06:07 UTC
    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'

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-03-28 14:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found