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

I'm trying to write a simple Perl program that uses a system command to make a directory on NT or W2K, i.e.,
$Dir_Name = "d:\\path\\subdir"; system ("mkdir $Dir_Name");
This works 'most' of the time. However, we have run into some servers that the are giving us an error 'file name too long' because it is translating the \\ as / - which won't work too well on Windows. Should be simple - but its causing us much grief!

Thanks for the wisdom!

Replies are listed 'Best First'.
Re: Creating Directories
by chromatic (Archbishop) on Jun 11, 2003 at 19:43 UTC

    Perl also has a built-in mkdir command. I like File::Path's mkpath function. If you use those, especially in combination with File::Spec's catdir and catfile, you won't have to worry about path separators.

    Life's way too short to spend escaping backslashes.

      I am a recent convert to File::Path, especially on Windows. Unfortunately the Explorer process quite often hangs on my box, due to the excessive stuff it does under the hood. Using mkpath, Perl does it all quickly with no fuss.

      The added benefit is that you can call mkpath as:

      mkpath('c:\blah/blah/blah.pl');
      and it will just do the right thing. Nice :)

      --
      Barbie | Birmingham Perl Mongers | http://birmingham.pm.org/

Re: Creating Directories
by allolex (Curate) on Jun 11, 2003 at 19:42 UTC

    Using a system call to do this is unnecessary. Perl has a mkdir function built in.

    my $new_folder = 'c:\\path\\directory'; mkdir $new_folder unless -d $new_folder; die "Could not create dir $new_folder: $!" unless -d $new_folder;

    HTH

    --
    Allolex

Re: Creating Directories
by cciulla (Friar) on Jun 11, 2003 at 22:05 UTC

    I'm running WinXP and ActiveState 5.8.0, and this works:

    mkdir 'c:/adirectory/anotherdirectory';
    (Note the '/')

Re: Creating Directories
by fglock (Vicar) on Jun 11, 2003 at 19:39 UTC

    Use single quotes and it will keep the \\ untouched:

    $Dir_Name = 'd:\\path\\subdir';
Re: Creating Directories
by nimdokk (Vicar) on Jun 11, 2003 at 19:44 UTC
    I'm not familiar with Win32 Perl, but if it is similar enough to the *nix versions, then there is a function built into perl to create directories: mkdir(). Might be worth checking out since it does not require you to use a system call.

    just my $.02 :-)

    "Ex libris un peut de tout"

Re: Creating Directories
by Jenda (Abbot) on Jun 11, 2003 at 20:11 UTC

    How do you know it's translating the backslashes to slashes? Is there something you did not tell us? Does even a script that looks like the snipet above fail on those servers? What does the actual code look like?

    Jenda
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
       -- Rick Osborne

    Edit by castaway: Closed small tag in signature