Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping Through Windows Drive Locations

Status
Not open for further replies.

RF101

Technical User
May 17, 2006
33
US
All,

In the past I had a bunch of csv or text files sitting on a web server. My script would loop through all of the folders on the web server and pull out the parts of the files that I wanted.

Recently these files have been moved from the web server to a shared drive that I do not control. I cannot setup a new web server on the new drive locations.

Can someone tell me how I can convert my web server references in my script to a windows share drive location?

Script:

use strict;
use Date::Manip;
use LWP 5.64;


#@market = array, is the holder for the requested folder
my @market = ('folder1', 'folder2');
my $lastDate = ParseDate("1/1/09"); #sets the first date to collect
my $numDays = 30; #number of days to collect
my $date=substr(&DateCalc("today","+1 day"),0,8);

foreach my $market(@market) {

open FILE, ">>CFC_Summary_${date}.txt";

for (my $i=0; $i<=$numDays; $i++) {

my $date=substr(&DateCalc($lastDate,"+$i day"),0,8);

my $reportName = "${market}${date}cfc.csv";

print "$market $reportName\n";

my $url = "

my $browser = LWP::UserAgent->new;

my $response = $browser->get($url);

my $content = $response->content;

my @lines = split("\n",$content);

my $j;

my $x;

for ($j=0; $j<$#lines; $j++) {

if ($lines[$j] =~ /Totals/) { $x=1;} # Totals Only

print FILE "$date $market $lines[$j]\n"if ($x > 0);
last if ($lines[$j] =~ /Totals/);

# Here I am looping through the files and grabbing only the line that says Totals

}
}

close FILE;
}


Any help would be greatly appreciated.

Thanks,

RF
 
If you have access to the windows shares then you don't need LWP atall. If not though, then the webserver needs to get those files and expose them to the web.
 

I am a hack when it comes to Perl. I figured I could get rid of all of the LWP, url and browser references. Is there another module that would help with windows file access?

I am trying to replace the $url with a file directory location.

Is there a way I can replace the following:
my $response = $browser->get($url);

 
Ok, well you can declare the share as follows;-
Code:
$myshare = "\\\\SHARENAME\\SHAREFOLDER";

or 

$myshare = '\\SHARENAME\SHAREFOLDER';

The first one is if you quite with double quotes and are therefore subject to interpolation, or use single quotes and declare it like you would see in windows.

Then to read that folder:-
Code:
sub scanFolder{
    	opendir(SOURCE, $myshare ) || die("Cannot open directory" . $myshare );
	my @files= grep((/\.csv$/i), readdir SOURCE);
	closedir(SOURCE);
        return @files;
}

That is a subroutine i use to get xls files (but i changed it to csv above).

Then your file list will be

Code:
@myFileArray = &scanFolder;



As for a module to deal with it, probably, but I dont think it is necessary as perl deals with files pretty well on its own.

Once you have your file list you can loop through the files and read in each one and look for the total row.
Or if you need to copy them locally from the share before dealing with them then you can use File::Copy

Code:
use File::Copy;
copy("file1","file2");


for the reading of the csv files take a look at this thread.
 
jez,

this might not do what you think:

$myshare = '\\SHARENAME\SHAREFOLDER';


if you print the string it will see what I mean.

\SHARENAME\SHAREFOLDER


the backslash is still interpolated inside of a single-quoted construct as the escape character, so if you want two backslashes in sequence you have to at least escape the first one:

$myshare = '\\\SHARENAME\SHAREFOLDER';

or you can escape them both:

$myshare = '\\\\SHARENAME\SHAREFOLDER';



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I am using the first form in my code and only mentioned the other off the top of my head, so thanks, I stand corrected :)
.. teach me to try and be too clever for my boots.

:):):)
 
Not so much a correction but team work. I know I appreciate when another member points out something I have posted that is not correct, often because I was posting quickly or maybe just made a typo but at times I was just flat wrong.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top