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

Parsing filenames

Status
Not open for further replies.

faceoftheearth

Programmer
May 17, 2007
2
US
I've set up a Perl script that essentially makes VLC stream an mms stream into a file. I have it set up so that VLC saves the file as month-day-year. So today the filename is 5-17-2007. What I want to do is to check if a file already exists for today and if so to append a number to the save name. So it would check if 5-17-2007 exists and if so would save the file as 5-17-2007-1. If it finds that 5-17-2007-1 exists, I want it to create 5-17-2007-2 etc.

As of now I can check if the current date already has a file using a simple if(-e $inpath.$date). If it finds that it assigns a random integer to the end of the filename. This works better, but there's still a chance of overwriting a file and it is just generally a horrible idea.

My tactic was to be (psuedo code)
if(-e $inpath.$date.number)
{
get filename, chop number off end, increment number, create new filename
}

How can I do that?

Thanks
 
Code:
my $i = 1;
while (-e $inpath . $date . $number) {
   $i++;
}

-------------
Cuvou.com | The NEW Kirsle.net
 
sorry

Code:
my $i = 1;
while (-e $inpath . $date . $i) {
   $i++;
}

-------------
Cuvou.com | The NEW Kirsle.net
 
Thanks, that should work great. I always make simple problems seem more complicated.
 
Code:
[gray][i]# Unique Destination (Not Race Condition Protected)[/i][/gray]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$name[/blue] = [blue]$inpath[/blue].[blue]$date[/blue][red];[/red]
[olive][b]for[/b][/olive] [red]([/red][black][b]my[/b][/black] [red]([/red][blue]$base[/blue],[blue]$suffix[/blue][red])[/red] = [red]([/red][blue]$name[/blue],[fuchsia]0[/fuchsia][red])[/red][red];[/red] [url=http://perldoc.perl.org/functions/-X.html][black][b]-e[/b][/black][/url] [blue]$name[/blue][red];[/red][red])[/red] [red]{[/red]
	[blue]$name[/blue] = [blue]$base[/blue] . [red]'[/red][purple]-[/purple][red]'[/red] . ++[blue]$suffix[/blue][red];[/red]
[red]}[/red]

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top