my $basepath = "c:\\foo\\bar"; my $dir = $basepath."\\".$gene."_".$geneid; mkdir($dir, 0755) or die "Failed creating $dir\n"; chdir($dir); #what file are you writing? That is so ambiguous. Let us pretend $string is your file contents and I will make up a filename becuase you weren't specific about that either. my $file = "baz.txt"; open(OUT, ">>$file") or die "Cannot open $file for writing\n"; print OUT $string; close(OUT);