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

removing directories with wildcard glob in OS-portable way

by Anonymous Monk
on Dec 29, 2012 at 23:21 UTC ( #1010858=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to replace a single line of code `rm -rf $tmp/scale-*` with something that is portable across OSes. My first idea was File::Path::remove_tree($tmp,{keep_root=>1}), but I don't want to necessarily remove everything in the $tmp directory. Those scale-* directories have filled subdirectories too, hence the -f in the rm call. I'm also trying to not construct the paths directly, I've used File::Spec->catfile() in other places in this script. I'm trying to stick with core Perl modules, since I don't want users to have to install anything separately if possible. What I thought would work was some combination of File::Path::remove_tree, glob(), and File::Spec->catfile() (or similar), but I can't seem to put it all together. Most of the glob() examples I've seen have hard-coded directories. Please tell me I'm over-thinking this!

Comment on removing directories with wildcard glob in OS-portable way
Select or Download Code
Re: removing directories with wildcard glob in OS-portable way
by BrowserUk (Pope) on Dec 30, 2012 at 00:14 UTC
    Please tell me I'm over-thinking this!

    You're over thinking this! :) Or rather, going about things the hard way.

    Assuming the command you gave does what you need everywhere bar Windows, just use this when you detect Windows:

    system qq[ for /d %d in ($tmp\\scale-*) do @rd /q /s %d ];

    Eg:

    C:\test>dir /b /s ppp C:\test\ppp\donttouch C:\test\ppp\scale-123 C:\test\ppp\scale-124 C:\test\ppp\scale-125 C:\test\ppp\donttouch\junk.1 C:\test\ppp\scale-123\junk C:\test\ppp\scale-123\junk\junk.1 C:\test\ppp\scale-123\junk\junk.2 C:\test\ppp\scale-123\junk\junk.3 C:\test\ppp\scale-124\junk C:\test\ppp\scale-124\junk\junk.1 C:\test\ppp\scale-124\junk\junk.2 C:\test\ppp\scale-124\junk\junk.3 C:\test\ppp\scale-125\junk C:\test\ppp\scale-125\junk\junk.1 C:\test\ppp\scale-125\junk\junk.2 C:\test\ppp\scale-125\junk\junk.3 C:\test>for /d %d in (ppp\scale-*) do rd /q /s %d C:\test>rd /q /s ppp\scale-123 C:\test>rd /q /s ppp\scale-124 C:\test>rd /q /s ppp\scale-125 C:\test>dir /b /s ppp C:\test\ppp\donttouch C:\test\ppp\donttouch\junk.1

    Why reinvent an operation that the OS already knows how to do?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Thanks! After getting up from the computer I had one of those insights that makes me think "Wow, I feel stupid." How about:

      File::Path::remove_tree(glob(File::Spec->catdir($tmpdir,'scale-').'*'));

      No OS-detection necessary!

        Trust me, the OS detection is happening, just not in your code. But that's not the problem.

        It's all teh other stuff that is happening under the covers that you also don't know about.

        This:

        perl -E"system q[for /d %d in (scale-*) do @rd /q /s %d]"

        Does the same job as this:

        perl -MFile::Path -MFile::Spec -wE"File::Path::remove_tree(glob(File:: +Spec->catdir('.','scale-').'*'));"

        But uses only 7% of the user cpu and 3% of the system cpu.

        Does it matter? Depends alot on how big the trees are and how often you run the command I guess, but why use cycles you don't have to.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: removing directories with wildcard glob in OS-portable way
by Anonymous Monk on Dec 30, 2012 at 14:32 UTC
    It is sometimes better, more-consistent among different OSes, to write code (it can also be a one-liner ...) to iterate through the tree pushing directory-names onto an array, then in step 2 iterate through the accumulated array. Some systems e.g. early Windows have been known to "skip" names in their directory walker when targets disappear, so that at the end of the day not everything's gone. Problem being of course that the list might build up many names that when encountered no longer exist.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (13)
As of 2014-09-30 16:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (378 votes), past polls