Link to home
Start Free TrialLog in
Avatar of mcgilljd
mcgilljd

asked on

perl creating a new folder named with todays date

I have a perl script that opens a webpage, gets all the .txt links from the page and savesw each link as a file in $SaveDir

Right now $SaveDir is static  

my $SaveDir = "I:/SECDocs/20070730/";

Every time it runs, I manually have to create a new folder with a matching name.

I would like script to create a new folder everyday on its own like:
my $SaveDir = "I:/SECDocs/$TodaysDate/";


How do I set $TodaysDate to be todays date and create a folder with that name?



Heres what I have:


#!/usr/bin/perl
use strict;
use warnings;
use LWP::Simple;
use Data::Dumper;
use HTML::SimpleLinkExtor;

#Update these as necessary

my @Pages = (
      "http://www.sec.gov/cgi-bin/browse-edgar?company=&CIK=&type=13f&owner=include&count=100&action=getcurrent",
      "http://www.sec.gov/cgi-bin/browse-edgar?action=getcurrent&datea=&dateb=&company=&type=13f&SIC=&State=&CIK=&owner=include&accno=&start=100&count=100",
      "http://www.sec.gov/cgi-bin/browse-edgar?action=getcurrent&datea=&dateb=&company=&type=13f&SIC=&State=&CIK=&owner=include&accno=&start=200&count=100",
      "http://www.sec.gov/cgi-bin/browse-edgar?action=getcurrent&datea=&dateb=&company=&type=13f&SIC=&State=&CIK=&owner=include&accno=&start=300&count=100",
      "http://www.sec.gov/cgi-bin/browse-edgar?action=getcurrent&datea=&dateb=&company=&type=13f&SIC=&State=&CIK=&owner=include&accno=&start=400&count=100",
      "http://www.sec.gov/cgi-bin/browse-edgar?action=getcurrent&datea=&dateb=&company=&type=13f&SIC=&State=&CIK=&owner=include&accno=&start=500&count=100",
      "http://www.sec.gov/cgi-bin/browse-edgar?action=getcurrent&datea=&dateb=&company=&type=13f&SIC=&State=&CIK=&owner=include&accno=&start=600&count=100",      
      "http://www.sec.gov/cgi-bin/browse-edgar?action=getcurrent&datea=&dateb=&company=&type=13f&SIC=&State=&CIK=&owner=include&accno=&start=700&count=100",
      "http://www.sec.gov/cgi-bin/browse-edgar?action=getcurrent&datea=&dateb=&company=&type=13f&SIC=&State=&CIK=&owner=include&accno=&start=800&count=100",
      "http://www.sec.gov/cgi-bin/browse-edgar?action=getcurrent&datea=&dateb=&company=&type=13f&SIC=&State=&CIK=&owner=include&accno=&start=900&count=100",
      "http://www.sec.gov/cgi-bin/browse-edgar?action=getcurrent&datea=&dateb=&company=&type=13f&SIC=&State=&CIK=&owner=include&accno=&start=1000&count=100",
      "http://www.sec.gov/cgi-bin/browse-edgar?action=getcurrent&datea=&dateb=&company=&type=13f&SIC=&State=&CIK=&owner=include&accno=&start=1100&count=100",
      "http://www.sec.gov/cgi-bin/browse-edgar?action=getcurrent&datea=&dateb=&company=&type=13f&SIC=&State=&CIK=&owner=include&accno=&start=1200&count=100",
      "http://www.sec.gov/cgi-bin/browse-edgar?action=getcurrent&datea=&dateb=&company=&type=13f&SIC=&State=&CIK=&owner=include&accno=&start=1300&count=100",
      "http://www.sec.gov/cgi-bin/browse-edgar?action=getcurrent&datea=&dateb=&company=&type=13f&SIC=&State=&CIK=&owner=include&accno=&start=1400&count=40"
      

      );


my $SaveDir = "I:/SECDocs/20070730/";

      #Make sure SaveDir ends in /
      $SaveDir .= "/" unless $SaveDir =~ /\/$/;


foreach  my $Page (@Pages) {

      #Get page and it's links
      GetPageAndLinks($SaveDir, $Page);
      };



sub GetPageAndLinks {
      my ($dir, $page) = @_;
     
      my $content = get($page);
     
      # Check the outcome of the response
      unless(defined($content)) {
            print STDERR "Could not get '$page': $@.\n";
            return;
      }
     
      my $extor = HTML::SimpleLinkExtor->new();
      $extor->parse($content);
     
      my @all_links = $extor->links;
      @all_links = grep {m!/Archives/edgar/data/.*\.txt!} @all_links;
      @all_links = map { $_ = "http://www.sec.gov$_"; } @all_links;
      foreach my $one_link (@all_links) {
            my $FileName = $one_link;
            $FileName =~ s/.*[\/]//;   #Remove leading info
            $FileName =~ s/\?.*//;     #Remove trailing info
            getstore($one_link, "$dir$FileName");
      print "$FileName";
      }
}






ASKER CERTIFIED SOLUTION
Avatar of Adam314
Adam314

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial