in reply to file creation

Well, it will work if the directory structure exists, so you will have to write something like:

#!/bin/perl -w use strict; my $fileName = "dir1/dir2/dir3/filename.txt"; create_dirs( $fileName); open (LIST, ">$fileName"); sub create_dirs { my $filename= shift; my $dir; while( $filename=~ m{(/?[^/]+)(?=/)}g) { $dir .= $1; next if( -d $dir); mkdir( $dir) or die "cannot create $dir: $!"; } }

Replies are listed 'Best First'.
Re: Re: file creation
by rob_au (Abbot) on Jan 31, 2002 at 13:47 UTC
    Expanding on your idea and employing File::Spec to provide more friendly cross-platform execution of the subroutine ...

    sub create_dirs { use File::Spec; my $file = shift; my $dir; foreach ( File::Spec->splitdir( (File::Spec->splitpath( $file ))[1 +] ) ) { $dir = File::Spec->catdir($dir, $_); next if -d $dir; mkdir( $dir ) || die $!; } }

     

    perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'

Re: Re: file creation
by gav^ (Curate) on Jan 31, 2002 at 16:44 UTC

    Or:

    use File::Path; mkpath('/dir1/dir2/dir3/');

    gav^