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!

Creating number that increments

Status
Not open for further replies.

fergmj

Programmer
Feb 21, 2001
276
0
0
US
My script uploads files. For each upload, I need to rename the file in the following format:

yyyymmddxxx.txt

I can upload the files fine and I can parse out the date pieces and format them as I want.

What I don't know how to do is increment the number so that when a new date the counter goes back to 0.

For instance if I send 150 files today, I would have
20010814150.txt as the last filename.

Tomorrow the first filename should be 20010815001.txt

Any ideas?

Thanks

fergmj
 
Just compare the date to the last uploaded filename. If it is different, reset the counter to 0.
 
Sorry for the VERY beginner question....
How do I compare the dates?
 
No apology needed. You can't be too much of a beginner if you already figured out how to take the date and create your desired filename. The following should work:

$i=0 unless ($date =~ m/^$lastfile/);
 
If you don't expect a lot of files to be uploaded on the same day, here's a simple alternative. It may not be as efficient as other methods, but it works. Create the filename you want to use (start with 000 as the sequence number) and check if that file exists. If it does, increment the xxx part and check again. If you save the filename without the .txt part in a variable, you can even increment it directly (even if it contains non-numerics). Here's an example:
Code:
# $filename contains yyyymmdd000 to start
while ( -e "/pathname/$filename.txt" )
   $filename++;
}
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
raider2001 -

Can you explain your posting? What is the =~ m/^$lastfile/);

fergmj
 
It is using Regex to comare the $date with $lastfile. The ^ means it will check to see if the variable you are comparing (in this case $date) begins with $lastfile :)

Regex is a great tool when you get the hang of it!

Andy
 
fergmj,

Can you post your script? I need to do the same thing as you are doing.

 
three -

I can't because I work for a corporation that won't allow it. I am very sorry. Is there a particular part that you are interested in. I could sent a part of it but not the whole thing.

fergmj
 
fergmj,

Send as much as you can (or edit your corporation words etc.. it if needed) so I can at least understand it. My email is teser3@hotmail.com

Thanks!
 
Is there something special I need to include to get the regex to work? It fails when I use it.

Thanks

fergmj
 
Heres a little code that should do the trick:

Code:
#!/usr/bin/perl -w

chomp(my $date = qx(date +%Y%m%d));

my $num = 0;
my $file = "file$date$num";
while( -e $file ) {

    $num++;
    $file = "file$date$num";
}

print "$file\n";
open (FILE, ">$file");
print FILE "$num\n";
close FILE;

Hppy coding, cheers NEIL s-)
 
Here's a cool little tip: if you have a variable that has only been used as a string, and matches the pattern /^[a-zA-Z]*[0-9]*$/ it can be incremented as a string! What that really means is that not only can you increment values like "TMP0000", but you can even increment "Az" (it will wrap to "Ba", or "zz" to "aaa"! Pretty handy for creating temp file names and things like that.

By the way, that pattern just means that it must contain only letters and digits, and all digits (if any) must be at the end of the string (i.e. "A1A" isn't valid). Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
What does the while (-e $file ) {

$num++;
$file = "file$date$num";
}

do?
 
It means that while the filename you created exists (-e $file), increment the number, rebuild the filename, and try again.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
While the code is great and helpful, it doesn't tell me how to compare that with the current date and increment the $num if the two differ without using the regex above. but that doesn't work.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top