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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Daily File Name Changes 1

Status
Not open for further replies.

YouEnjoyMyself

IS-IT--Management
Jul 27, 2004
111
US
Ok here is what I am trying to accomplish, mind you I have some perl expierence but not a lot. What I am trying to do is have a script that reads through a tab delimited text file and parses out a single column. I already have that part of the script (see below). The issue I am running into is that the input file that this script parses from gets a new file name everyday. The file name looks like this :

File List F-000064 2-23-2005

The part that changes is highlighted in bold. It changes to the date of the day and numerical part (000064) goes up by one upon each creation of the input file. Is there any way I can get the script to know when the input file name changes? I might not be asking this in the right way, any help is appreciated, these forums have helped me a lot in the past. Thanks in advance!

What I have so far:


open(DFILE, "< C:\\File List F-000064 2-23-2005") || die "ERROR\n"; #opens filelist created by vertices
while(<DFILE>){
chomp;
$sn = (split(/\t/,$_))[1];
$sn =~ s/^\s+//;
$sn =~ s/\s+$//;
if ($sn =~ /^[0-9]+$/) {
push(@list, $sn);
}
}
close(DFILE);

open(OUT, "> C:\\fileout.txt") || die "ERROR\n";
select(OUT);
$| = 1;
print OUT join(':', sort @list), "\n";
select(OUT);
$| = 1;
select(STDOUT);
$| = 1;
close(OUT);
#takes all serial numbers out of the file list and place them into a text file each seperated by a ':'

system ( 'C:\\eject.cmd' ) && die "Cannot run eject.cmd" ; #run eject command and ejects tapes from outfile.txt

 
Increment "File List F-000064 2-23-2005" to "File List F-000065 2-23-2005" and then test to see if the file exists with -f. If not increment the date and then test with -f again. Repeat until you find your file. You will need to keep track of the last file name somewhere.


Michael Libeson
 
What about something like the following:

Code:
use strict;

my $dir = "c:/";
my @logs;

opendir(DIR, "$dir");
my @filelist = readdir(DIR);
closedir(DIR);

foreach my $file (@filelist)
{
	if (-f "$dir$file")
	{
		if ($file =~ /^File List F-0000/i)
		{
			push @logs, $file;
		}
	}
}

my $log_file = pop @logs;

open (FILE, "$dir$log_file");
#process your file
close(FILE);

This will make sure you take the last file that starts with File List F- and process it. If you're removing these files after they've been processed, then you'll just need to find the match and process it.

- Rieekan
 
Rieekan,
The above code works great. I am running into an issue when it comes to adding it to the script i already have. What my script did originally is take the file list (File List F-xxxxxx x-xx-xxxx) and parse a column listing serial numbers (see sample below 1). It then spit the data out into a text file with the serial numbers all on one line seperated by a colon (see sample 2). I thank you for all your help its greatl appreciated.

sample 1 (input):

Site Serial Number Medium Misplaced Storage

xxxx 001233 xxxxx xxxxx
xxxx 001234 xxxxx xxxxx
xxxx 001235 xxxxx xxxxx
xxxx 001236 xxxxx xxxxx
xxxx 001237 xxxxx xxxxx


sample 2 (output):

001233:001234:001235:001236:001237








 
You should be able to take your original script and place everything between where you open and close the file you're parsing into the line labeled #process your file.

- Rieekan
 
Thanks. I tried that before posting. I figured out what was wrong though. I had to take off the use strict;
 
Ah, it's always a good idea to use strict so you don't run into any scope issues in your code. But, as long as it works for you, then you should be all set.

- Rieekan
 
I figured out what was wrong though. I had to take off the use strict;

Hate to be a killjoy, but --
You haven't really figured out what was wrong. You've just disabled one of Perl's safety features, so now it's letting you get away with it. I'd suggest you put strict back in and try to figure out what the problem actually is.

 
Okay so I know this post is pretty old, but I got a quick question..maybe someone can help. What I want to be able to do with this script is to grab the two latest lists or the two most recent lists. This is the code that was given to me (Thank you!) that grabs the latest file:


use strict;

my $dir = "c:/";
my @logs;

opendir(DIR, "$dir");
my @filelist = readdir(DIR);
closedir(DIR);

foreach my $file (@filelist)
{
if (-f "$dir$file")
{
if ($file =~ /^File List F-0000/i)
{
push @logs, $file;
}
}
}

my $log_file = pop @logs;

open (FILE, "$dir$log_file");
#process your file
close(FILE);
 
Add another line to grab the last element in the array after the line my $log_file = pop @logs;

my $log_file2 = pop @logs;

- Rieekan
 
YouEnjoyMyself

I notice that most of your posts seem to be to do with storage (tape) management issues, and interpreting the reports that come out of your TMS. Is there any facility to suppress the headers and footers on these reports, perhaps from the command line? It's just that it would probably make your parsing a whole lot simpler if you didn't have to deal with pagination.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top