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

Hi Perl Monks,
I need to be able to write a perl script that can create and remove directories and files on both windows and unix based systems.

I could use the system(mkdir mydir) command and just have different code that runs depending on the file system, how I thought there might be a core perl module out there which would run on both platforms. Assuming that I can't find a perl module and I opt for the system() method, is there a DOS command that will allow me to remove entire directories and files, i.e. like 'rm -r mydir' in UNIX.

thanks for your help,
Tom

Replies are listed 'Best First'.
Re: platform independent system commands
by MZSanford (Curate) on Mar 19, 2003 at 14:21 UTC
    DOS does have deltree, which will remove entrie directory trees, but i would suggest using Perl's builtin functions (unlink, mkdir), as someone (a cast of 1000's really) has worked toward poratbility already.
    from the frivolous to the serious
Re: platform independent system commands
by sschneid (Deacon) on Mar 19, 2003 at 14:23 UTC
    If you want the script to be truly portable, I'd recursively unlink each file in the directory, and then rmdir the directory itself.

    It's a bit slower than a system call, but it's perl-native.

    -s.
Re: platform independent system commands
by hardburn (Abbot) on Mar 19, 2003 at 15:13 UTC

    Perl already provides the mkdir builtin, which works in a platform-independant manner.

    As for deleting a directory tree, I suggest using File::Find with the following as the wanted function: sub wanted { unlink }

    ----
    Reinvent a rounder wheel.

    Note: All code is untested, unless otherwise stated

Re: platform independent system commands
by BrowserUk (Patriarch) on Mar 19, 2003 at 16:55 UTC

    If you decide to go the system route, NT/2000/XP have an equivalent of the 'rm -r mydir', namely

    RD /S /Q mydir

    or rmdir /S /Q mydir

    It's certainly easier and quicker than having to walk the subtree deleting the files and then backing out removing the subdirectories.


    Examine what is said, not who speaks.
    1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
    2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
    3) Any sufficiently advanced technology is indistinguishable from magic.
    Arthur C. Clarke.