moving a file to a different name

S

Shawn

Hi,

I'm trying to move a file to different directory, filename (with a date
stamp within the name).

The file starts out as: report.txt
and I want to move it so it comes out as: gl_user_rept_021804.txt

So, I have been trying different versions of this command:
system("mv /var/shawn/report.txt /var/fred/gl_sys_rept_$date.txt");

I set date = `date +m+d+y`

And I can get it close but not quite what I want:

-rw-rw-r-- 1 gp22 pnpsup 5 Feb 18 10:06 gl_user_rept.021804
-rw-rw-r-- 1 gp22 pnpsup 5 Feb 18 10:37 gl_user_rept..txt

Any suggestions would be greatly appreciated.

Thanks
 
B

Ben Morrow

Shawn said:
I'm trying to move a file to different directory, filename (with a date
stamp within the name).

The file starts out as: report.txt
and I want to move it so it comes out as: gl_user_rept_021804.txt

#!/usr/bin/perl

use strict;
use warnings;

use File::Copy qw/mv/;
use POSIX qw/strftime/;

my $from = '/var/shawn/report.txt';
my $to = strftime '/var/fred/gl_sys_rept_%m%d%y.txt', localtime;

mv $from, $to or die "can't mv file: $!";

BTW, I'd recommend using YYYYMMDD unless you've a good reason not to:
it has a four-digit year, it's unambiguous in terms of m/d/y vs d/m/y
and (most importantly) it sorts in chronological order.

Ben
 
J

John J. Trammell

I'm trying to move a file to different directory, filename (with a date
stamp within the name).

The file starts out as: report.txt
and I want to move it so it comes out as: gl_user_rept_021804.txt

So, I have been trying different versions of this command:
system("mv /var/shawn/report.txt /var/fred/gl_sys_rept_$date.txt");

I set date = `date +m+d+y`

And I can get it close but not quite what I want:

-rw-rw-r-- 1 gp22 pnpsup 5 Feb 18 10:06 gl_user_rept.021804
-rw-rw-r-- 1 gp22 pnpsup 5 Feb 18 10:37 gl_user_rept..txt

Any suggestions would be greatly appreciated.

In no particular order:

* perldoc File::Copy -- much cleaner than system()
* use a date format that makes sense, like YYYYMMDD
* only hit the shell when you need to; use POSIX::strftime
or similar to create date strings.

Some untested code:

use POSIX 'strftime';
my $date = strftime "%Y%m%d", localtime;
use File::Copy 'move';
move("/var/shawn/report.txt","/var/fred/gl_sys_rept_$date.txt");
 
G

Gary E. Ansok

I'm trying to move a file to different directory, filename (with a date
stamp within the name).

The file starts out as: report.txt
and I want to move it so it comes out as: gl_user_rept_021804.txt

So, I have been trying different versions of this command:
system("mv /var/shawn/report.txt /var/fred/gl_sys_rept_$date.txt");

I set date = `date +m+d+y`

And I can get it close but not quite what I want:

-rw-rw-r-- 1 gp22 pnpsup 5 Feb 18 10:06 gl_user_rept.021804
-rw-rw-r-- 1 gp22 pnpsup 5 Feb 18 10:37 gl_user_rept..txt

Any suggestions would be greatly appreciated.

Other posts have discussed how to get the date and do the file renaming
in Perl (which I also highly recommend), but if you want to understand
what went wrong with your original code, print out $date like this:
print "Date is >>>$date<<<" and then look up the chomp() function.

Gary Ansok
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top