tlialin
tlialin

Reputation: 71

Force a folder to be made if one doesn't exist

How do I force a folder to be made in my code below, line 31, when one doesn't exist already. I've tried the mkdir command but can't get it to work. I also tried >> instead of one. This is the line I'm talking about: "open my $fh, '>', "Ask/Parsed/Html/$"

 use lib '/Users/lialin/perl5/lib/perl5';
 use strict; use warnings;
 use feature 'say';
 use File::Slurp 'slurp';  # makes it easy to read files.
 use Mojo;
 use Mojo::UserAgent;
 use URI;

 #my $html_file = "Ask/Agilent_Technologies_ask.html";  # take file from directory
 #my $html_file = shift @ARGV; # take file from command lin

 my $calls_dir = "Ask/";
 opendir(my $search_dir, $calls_dir) or die "$!\n";
 my @html_files = grep /\.html$/i, readdir $search_dir;
 closedir $search_dir;
 #print "Got ", scalar @files, " files\n";

 #my %seen = ();
 foreach my $html_files (@html_files) {
 my %seen = ();
    my $current_file = $calls_dir . $html_files;
 open my $FILE, '<', $current_file or die "$html_files: $!\n";

 my $dom = Mojo::DOM->new(scalar slurp $calls_dir .$html_files);
 print $calls_dir .$html_files ;

 #for my $csshref ($dom->find('a[href]')->attr('href')->each) {
 #for my $link ($dom->find('a[href]')->attr('href')->each) {
 #  print $1;
 #say $1 #if $link->attr('href') =~ m{^https?://(.+?)/index\.php}s;
 open my $fh, '>', "Ask/Parsed/Html/${html_files}.result.txt" or die $!;
 for my $csshref ($dom->find('a[href]')->attr('href')->each) {
 my $cssurl = URI->new($csshref)->abs($calls_dir .$html_files);

 #open my $fh, '>', "Ask/${html_files}.result.txt" or die $!;
 $fh->print("$html_files\n");
 $fh->print("$cssurl\n");
 #$fh->print("\t"."$_\n");
 #print "$cssurl\n";
 #print $file."\t"."$_\n";

 }}

Upvotes: 0

Views: 70

Answers (1)

bytepusher
bytepusher

Reputation: 1578

I would suggest using File::Path, specifically make_path().

You can also write a recursive function with mkdir, but it seems to me it is not worth the trouble, this module worked fine for me.

Upvotes: 2

Related Questions