SPUG: test directory and then create
C.J. Collier
cjcollier at sinclair.net
Wed Jan 9 18:46:31 CST 2002
#I'm assuming this format for the file.
#(No newlines, just directories separated by tabs)
#---- Example File ----
thisdir/thisfile thatfile theotherdir/andfile yad/yaf stillanother/stillanother Andonemore
#---- End Example File ----
#-- start perl code --
#!/usr/bin/perl -w
use strict;
#open the file and associate a file handle with it
open DIRLIST, "external_file.txt";
#get a line from the filehandle and stick it in the $dirs scalar
my $dirs = <DIRLIST>
#split the directory list string on tabs and stick it in the @dir array
my @dir = split(/\t/, $dirs);
#iterate over the directories in the list and try to create them
foreach my $dir (@dir){
#test $dir to see if it's a directory
if(-d $dir){
print "Directory $dir already exists. Not creating.\n";
}else{
if(mkdir($dir)){
print "Created directory $dir\n";
}else{
print "Could not create directory $dir: $!\n";
}
}
}
#-- end perl code --
For questions like this, I suggest talking to the perl beginners' list
(beginners at perl.org), although I always love writing up this kind of
thing. IRC is also a good place. #perl on irc.openprojects.net is a
friendly environment for beginners.
Cheers,
C.J.
On Wed, 9 Jan 2002, garrett esperum wrote:
> Hello all,
>
> I am using solaris 2.6 and perl 5.6.1.
> I have an external file that has multiple directory names listed in it. The
> directory names in this file are seperated by a tab. I want to read this
> external file and I want to test to see if each of these directories exists.
> If these directories don't exist I want to create them. If they do exist I
> don't want to do anything.
> How do I do this in PERL? How do I do the dir test? And how do I perform the
> loop?
>
> Thank You!
>
> -garrett
>
> _________________________________________________________________
> Send and receive Hotmail on your mobile device: http://mobile.msn.com
>
>
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> POST TO: spug-list at pm.org PROBLEMS: owner-spug-list at pm.org
> Subscriptions; Email to majordomo at pm.org: ACTION LIST EMAIL
> Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
> For daily traffic, use spug-list for LIST ; for weekly, spug-list-digest
> Seattle Perl Users Group (SPUG) Home Page: http://zipcon.net/spug/
>
>
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
POST TO: spug-list at pm.org PROBLEMS: owner-spug-list at pm.org
Subscriptions; Email to majordomo at pm.org: ACTION LIST EMAIL
Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
For daily traffic, use spug-list for LIST ; for weekly, spug-list-digest
Seattle Perl Users Group (SPUG) Home Page: http://zipcon.net/spug/
More information about the spug-list
mailing list