creating folders

I have a problem using the following code

$PATH='c:\\windows\\main';
sub write
{      
      my ($fileName,$data)=@_;
      open (TOFILE,"+>".$PATH.$fileName) || die "archivo -- $fileName -- no existe en $ENV{PWD} ";;
      print TOFILE $data;
      close TOFILE;      
}

if i execute my function like this:
write ("/text.txt","test data");  # it works fine, it creates text.txt if it wouldnt exists

but if i execute the function:
wirte ("/folder/test/text2.txt","more data");  # it crashes, because  "folder" doesnt exists on my main path and also "test" folder doesnt exists

so, my question is... is there an easy way to create necessary folders if they dont exist on my path??


LVL 2
jazz250Asked:
Who is Participating?
 
ozoCommented:
use File::Path;
use File::Spec::Win
mkpath(File::Spec::Win32->catpath((File::Spec::Win32->splitpath($PATH.$filename))[0,1]));
 open (TOFILE,"+>$PATH$fileName)" || die "archivo -- $fileName -- no existe en $ENV{PWD} ";
0
 
FishMongerCommented:
0
 
ozoCommented:
use File::Spec::Win32;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.