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

Add timestamp to filename

Status
Not open for further replies.

tannx

MIS
Mar 25, 2007
10
EE
Below is script where it edits the file, removes txt extension and writes new file.
I also need to insert timestamp into filname (fileA2007-03-25 12:01:12 ; fileB2007-03-26 10:50:55). How can I achieve that? Help needed.

Code:
$failid="list.ao";
$fail=0;
if (open(FAILIDcnt, $failid)) {
           while($line = <FAILIDcnt>) {
		chomp($line);		
		$pnt[$fail]=($line);
		#print "$line\n";
                $fail ++;
           }
         close (FAILIDcnt);
}
$failm=0;
do {
       too( $pnt[$failm]);
       $failm ++;
} until $failm>$fail;
sub too {
   my @sisse = @_;
   my $fff=$sisse[0];
   if (open(IF, $fff)) {
       
       $fff=~ s/\.txt//;
       open(OF,">".$fff) or die "Can't open $fff!";
       while($line = <IF>) {
       
	  if ($line =~ /^(-------------------)/) {
           close (OF);
           close (IF);
           return 0;}
         $line =~ s/\t/;/g;
         print OF "$line";
       }
  }
}
 
In a *nix system:

$date = `date +%y-%m%d-%H:%M%S`;
chomp $date

or

use posix (read the module on cpan on options to be used).
$date =

system ("mv oldfileName $dir/$oldfileName$date");
or you can use the rename
open FH,"< filename$date" || die "$!\n";
 
I can get time now but unable to integrate it into file name.

Code:
use strict;
use warnings;
use POSIX qw(strftime);

my $date;
printf(OF."%s",strftime(("%Y-%m-%d  %H:%M:%S",localtime($date=time))));
 
If what you have works, can't you just
Code:
my $name = sprintf(OF."%s",strftime(("%Y-%m-%d  %H:%M:%S",localtime([red]$date=[/red]time))));
You can probably remove the bit in red, too, I'm not sure it adds any value...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Formatting issues only:

1) As steve said, you can remove the $date assignment in teh localtime function.
1b) You can also remove the time parameter itself as localtime works on time by default.
2) You have quite the excessive use parenthesis. You even have two sets around the the contents of strftime. Cut down on some of these.
3) Finally, what is OF? If that's a constant, you should just include it inside the format string definition itself instead of concatenating it.

Code:
my $name = sprintf [COLOR=red]OF[/color]."%s", strftime("%Y-%m-%d  %H:%M:%S", localtime);

- Miller
 
Why do you want to name files like this? I have a gut feeling there is probably a better solution than using a human readable date in your filenames.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
For human reader. I create 20+ csv files for him every month. I would like to automate my part. What they to after is not in my contract. Unfortunately I'm never done scripting.
So I'm still unable to get timestamp after filename :(
 
I often add date/time info onto output filenames while debugging scripts. Try adding this into your script:

Code:
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
    $sec=sprintf("%02d",$sec);
    $min=sprintf("%02d",$min);
    $hour=sprintf("%02d",$hour);
    $mday=sprintf("%02d", $mday);
    $mon=sprintf("%02d", $mon+1);
    $year=sprintf("%04d", $year+1900);
$output_tag = "$mday_$mon_$year_$hour_$min";

my $filename= '/tmp/filea'."$output_tag";

Obviously replace the /tmp/filea part of the name to suit your setup, or replace it with a variable e.g. "$filea".

You can change the $output_tag format as you want (I don't like spaces in filenames so have used underscores).
 
tannx said:
Unfortunately I'm never done scripting.
So I'm still unable to get timestamp after filename :(

tannx,

Not to pick on you, but what you're trying to do is not hard. What aspect of it are you unable to get to work. Can you not get the time? Or can you not copy/rename the file? That's all it takes unless my understanding of your issue is incomplete.

- Miller
 
I can get time but im unable to but it on the filename. Well I can add date into file name but then the filename is not added itself its time or name not both
 
tannx said:
I can get time but im unable to but it on the filename. Well I can add date into file name but then the filename is not added itself its time or name not both

tannx,

What is the exact code that you're using now? As I said, what you're trying to do is not difficult. If you can't get it to work then either it's an approach problem or a syntax error. But we won't be able to diagnose those without seeing your code.

- Miller
 
I'm useing the same code what I posted here. "list.ao" is text file which contain failenames what has to be converted.
Script , list.ao and all the txt files are in the same folder.
 
tannx said:
I'm useing the same code what I posted here. "list.ao" is text file which contain failenames what has to be converted.
Script , list.ao and all the txt files are in the same folder.

If you're referring the code that you posted originally, I don't see what it has to do with the task at hand (Please note the cleaned up formatting):

Code:
$failid="list.ao";
$fail=0;
if (open(FAILIDcnt, $failid)) {
	while($line = <FAILIDcnt>) {
		chomp($line);
		$pnt[$fail]=($line);
		#print "$line\n";
		$fail ++;
	}
	close (FAILIDcnt);
}
$failm=0;
do {
	too( $pnt[$failm]);
	$failm ++;
} until $failm>$fail;
sub too {
	my @sisse = @_;
	my $fff=$sisse[0];
	if (open(IF, $fff)) {
		$fff=~ s/\.txt//;
		open(OF,">".$fff) or die "Can't open $fff!";
		while($line = <IF>) {
			if ($line =~ /^(-------------------)/) {
				close (OF);
				close (IF);
				return 0;
			}
			$line =~ s/\t/;/g;
			print OF "$line";
		}
	}
}

If you're referring to this code, it appears to be psuedo code, not your actual script.

Code:
use strict;
use warnings;
use POSIX qw(strftime);

my $date;
printf(OF."%s",strftime(("%Y-%m-%d  %H:%M:%S",localtime($date=time))));

So I repeat, please show us what your using right now. The only things that the script needs to do is construct the current date, already demonstrated above steve or tony; get your list of files; and rename the files using the date suffix. So show us what you have so far and we can help you.

- Miller
 
I'v got it to work like this:

Code:
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
    $sec=sprintf("%02d",$sec);
    $min=sprintf("%02d",$min);
    $hour=sprintf("%02d",$hour);
    $mday=sprintf("%02d", $mday);
    $mon=sprintf("%02d", $mon+1);
    $year=sprintf("%04d", $year+1900);
$output_tag = "_$mday-$mon-$year-$hour-$min";
$failid="konf.aa";
$fail=0;
if (open(FAILIDcnt, $failid)) {
    while($line = <FAILIDcnt>) {
        chomp($line);
        $pnt[$fail]=($line);
        #print "$line\n";
        $fail ++;
    }
    close (FAILIDcnt);
}
$failm=0;
do {
    too( $pnt[$failm]);
    $failm ++;
} until $failm>$fail;
sub too {
    my @sisse = @_;
    my $fff=$sisse[0];
    if (open(IF, $fff)) {
        $fff=~ s/\.txt//;
        open(OF,">".$fff.$output_tag) or die "Can't open $fff!";
        while($line = <IF>) {
            if ($line =~ /^(-------------------)/) {
                close (OF);
                close (IF);
                return 0;
            }
            $line =~ s/\t/;/g;
            print OF "$line";
        }
    }
}
 
Hi tannx,

Glad you were able to figure it out.

Your code can be simplified and cleaned up a lot, so I took a moment and did that for you. I have not tested this code, but I hope that you take moment to read and understand it:

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]

[black][b]use[/b][/black] [green]POSIX[/green] [red]qw([/red][purple]strftime[/purple][red])[/red][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$output_tag[/blue] = [maroon]strftime[/maroon][red]([/red][red]"[/red][purple]_%d-%m-%Y-%H-%M[/purple][red]"[/red], [url=http://perldoc.perl.org/functions/localtime.html][black][b]localtime[/b][/black][/url][red])[/red][red];[/red]

[black][b]my[/b][/black] [blue]$failid[/blue]=[red]"[/red][purple]konf.aa[/purple][red]"[/red][red];[/red]

[olive][b]if[/b][/olive] [red]([/red][url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url][red]([/red]FAILIDcnt, [blue]$failid[/blue][red])[/red][red])[/red] [red]{[/red]
	[olive][b]while[/b][/olive][red]([/red][black][b]my[/b][/black] [blue]$input[/blue] = <FAILIDcnt>[red])[/red] [red]{[/red]
		[url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red]([/red][blue]$input[/blue][red])[/red][red];[/red]

		[black][b]my[/b][/black] [blue]$output[/blue] = [blue]$input[/blue][red];[/red]
		[blue]$output[/blue] =~ [red]s/[/red][purple][purple][b]\.[/b][/purple]txt[/purple][red]/[/red][purple][/purple][red]/[/red][red];[/red]
		[blue]$output[/blue] .= [blue]$output_tag[/blue][red];[/red]

		[black][b]open[/b][/black][red]([/red]IF, [blue]$input[/blue][red])[/red] or [olive][b]next[/b][/olive][red];[/red]
		[black][b]open[/b][/black][red]([/red]OF,[red]'[/red][purple]>[/purple][red]'[/red].[blue]$output[/blue][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Can't open [blue]$output[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]

		[olive][b]while[/b][/olive][red]([/red][black][b]my[/b][/black] [blue]$line[/blue] = <IF>[red])[/red] [red]{[/red]
			[olive][b]last[/b][/olive] [olive][b]if[/b][/olive] [blue]$line[/blue] =~ [red]/[/red][purple]^-{19}[/purple][red]/[/red][red];[/red]
			[blue]$line[/blue] =~ [red]s/[/red][purple][purple][b]\t[/b][/purple][/purple][red]/[/red][purple];[/purple][red]/[/red][red]g[/red][red];[/red]
			[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] OF [red]"[/red][purple][blue]$line[/blue][/purple][red]"[/red][red];[/red]
		[red]}[/red]

		[url=http://perldoc.perl.org/functions/close.html][black][b]close[/b][/black][/url][red]([/red]OF[red])[/red][red];[/red]
		[black][b]close[/b][/black][red]([/red]IF[red])[/red][red];[/red]
	[red]}[/red]

	[black][b]close[/b][/black] [red]([/red]FAILIDcnt[red])[/red][red];[/red]
[red]}[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
Core (perl 5.8.8) Modules used :
[ul]
[li]POSIX - Perl interface to IEEE Std 1003.1[/li]
[/ul]
[/tt]

- Miller
 
Thank you all for your help.
And Miller your cleaned up version works perfectly, thanks.

Tann
 
Hi All!

Need more pointers for adding features into my script.

Right now when file "a" appears in folder "a" it will edited and renamed and moved into folder "b".
But the original file in folder "a" does not get removed.
I tought two possibilities.
1. original file in folder a gets deleted or
2. original file gets moved into different folder e.g archive.

Right now I'm lost. The best I achieved was if file "a" gets moved then original file will be deleted along with all the other files in the folder.
 
Hi all!

Script works well but now I need to add feature that output file will be saved useing ANSI codepage.
Source files I'm useing are all saved as Unicode.
all advice is welcomed.

Thnx.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top