CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Add date and time to filename??

 



drunkBunny
Novice

Jul 31, 2010, 9:04 PM

Post #1 of 13 (7136 views)
Add date and time to filename?? Can't Post

Is it possible to add the date and time to a file-name in Perl ??

The closest I ever got was using this:

Code
$file = /path/to/file; 
$datetime_string = ctime(stat($file)->mtime);


But that doesn't add the date , time to the file-name.
The file will be uploaded to a ftp server, so I need to preserve
the file-name.


deepeshtronics
Novice

Aug 1, 2010, 4:16 AM

Post #2 of 13 (7129 views)
Re: [drunkBunny] Add date and time to filename?? [In reply to] Can't Post

Hi,

If you are working on UNIX platform then you can use the following code to add the date and time string into filename.


Code
 
#!/usr/perl/bin

use warnings;
use strict;

my $mod_file_name;
my $path;
my $org_file='/path/to/file';

my $datetime_string = `ls -lt $org_file|cut -d" " -f6-7`;
chomp $datetime_string;

if($org_file =~ m/(.*)\/(.+)$/){
$path=$1;
$mod_file_name=$2;
}else{
$mod_file_name=$org_file;
}

$mod_file_name = $path."/".$mod_file_name." $datetime_string";


Thanks


drunkBunny
Novice

Aug 1, 2010, 11:24 AM

Post #3 of 13 (7121 views)
Re: [deepeshtronics] Add date and time to filename?? [In reply to] Can't Post

Thank deep.
I usually do work on *nix

But sometimes I have to use Windows .
What would I have to change to use this on Windows?


FishMonger
Veteran / Moderator

Aug 1, 2010, 2:02 PM

Post #4 of 13 (7116 views)
Re: [drunkBunny] Add date and time to filename?? [In reply to] Can't Post

What format do you want the the datestamp and do you want the mtime or ctime?

Instead if shelling out via backticks, I'd simply use Perl's stat function (or if you prefer File::Stat) and then use the POSIX strftime function to format it as desired.

An instead of the proposed if/else block, I'd use File::Basename


drunkBunny
Novice

Aug 1, 2010, 2:17 PM

Post #5 of 13 (7113 views)
Re: [FishMonger] Add date and time to filename?? [In reply to] Can't Post

mtime is what I want.
I'm pretty new to perl, so forgive my newbie questions.
I never used the stat function or Basename, not sure on how to use them.

Also I assume adding date and time to a file is different on *nix than Windows.


FishMonger
Veteran / Moderator

Aug 1, 2010, 6:19 PM

Post #6 of 13 (7105 views)
Re: [drunkBunny] Add date and time to filename?? [In reply to] Can't Post


In Reply To
mtime is what I want.
I'm pretty new to perl, so forgive my newbie questions.
I never used the stat function or Basename, not sure on how to use them.

I've had 1 too many cocktails and am about to have dinner, so I'll post an example tomorrow.


In Reply To
Also I assume adding date and time to a file is different on *nix than Windows.

No, altering the filename is done exactly the same in both systems.


FishMonger
Veteran / Moderator

Aug 2, 2010, 9:01 AM

Post #7 of 13 (7084 views)
Re: [drunkBunny] Add date and time to filename?? [In reply to] Can't Post

You haven't specified how you want the datestamp formatted, so my example will use this format YYYYmmdd-HHMMSS and prepend it to the filename.

This works on both Windows and *nix systems.

Code
#!/usr/bin/perl 

use strict;
use warnings;
use POSIX qw(strftime);
use File::Basename;
use File::stat;

my $fullpath = 'd:/perl/input1.txt';
my $file = basename($fullpath);
my $dir = dirname($fullpath);
my $stat = stat($fullpath);
my $datestamp = strftime("%Y%m%d-%H%M%S", localtime($stat->mtime));
my $newname = "$dir/$datestamp.$file";

print $newname;



(This post was edited by FishMonger on Aug 2, 2010, 9:02 AM)


drunkBunny
Novice

Aug 2, 2010, 10:46 AM

Post #8 of 13 (7076 views)
Re: [FishMonger] Add date and time to filename?? [In reply to] Can't Post

Hey Fishmonger thanks for the help.
But using your code give me this error:
"Can't call method "mtime" on an undefined value"
Tried this on my *nix box.




Code
use Net::FTP; 
use File::Copy;
use File::Basename;
use File::stat;
use POSIX qw(strftime);
use warnings;

my $server = "ftp.server.net";
my $user = "user";
my $pass = "pass";
my $fullpath = "/home/rocko/myFile.txt";
my $file = basename($fullpath);
my $dir = dirname($fullpath);
my $stat = stat($fullpath);
my $datestamp = strftime("%m%d%Y-%H%M%S", localtime($stat->mtime));
my $newname = "$dir/$datestamp.$file";



FishMonger
Veteran / Moderator

Aug 2, 2010, 11:27 AM

Post #9 of 13 (7070 views)
Re: [drunkBunny] Add date and time to filename?? [In reply to] Can't Post

Does the file actually exist in that (local) directory?

Add a simple test.


Code
my $fullpath = "/home/rocko/myFile.txt"; 

die "Oops - '$fullpath' was not found" unless -e $fullpath;



drunkBunny
Novice

Aug 2, 2010, 12:27 PM

Post #10 of 13 (7066 views)
Re: [FishMonger] Add date and time to filename?? [In reply to] Can't Post

Aha!
No the file does not exist.
I need the script to make the file if it doesn't exist


FishMonger
Veteran / Moderator

Aug 2, 2010, 12:41 PM

Post #11 of 13 (7063 views)
Re: [drunkBunny] Add date and time to filename?? [In reply to] Can't Post

Then simply use the open function to create the file when needed.


drunkBunny
Novice

Aug 2, 2010, 1:14 PM

Post #12 of 13 (7060 views)
Re: [FishMonger] Add date and time to filename?? [In reply to] Can't Post

Ok , thanks.
I assume you can do the same for directories, add date and time.


FishMonger
Veteran / Moderator

Aug 2, 2010, 2:18 PM

Post #13 of 13 (7054 views)
Re: [drunkBunny] Add date and time to filename?? [In reply to] Can't Post

See: perldoc -f rename

Code
D:\perl>perldoc -f rename 
rename OLDNAME,NEWNAME
Changes the name of a file; an existing file NEWNAME will be
clobbered. Returns true for success, false otherwise.

Behavior of this function varies wildly depending on your system
implementation. For example, it will usually not work across
file system boundaries, even though the system *mv* command
sometimes compensates for this. Other restrictions include
whether it works on directories, open files, or pre-existing
files. Check perlport and either the rename(2) manpage or
equivalent system documentation for details.

For a platform independent "move" function look at the
File::Copy module.


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives