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

Copy file adding a date stamp

Status
Not open for further replies.

skeeter66

MIS
Dec 7, 2004
13
US
How would I use Perl to copy a file from one directory to another adding a date stamp to the file in the new directory. I need the date stamp as the same file from the previous day will be in the second directory as well.
 
I'm assuming that you want to save a file like file.ext as file-20041208.ext, correct?

If so, try this:

Code:
#!usr/bin/perl

use File::Copy;

my (undef,undef,undef,$mday,$mon,$year,undef,undef,undef) = localtime;
$year += 1900;
$mon += 1;

$date = sprintf "%02d/%02d/%04d", $mon, $mday, $year;

copy ('/path/to/file.ext', "/path/to/new/file$date.ext");

- Rieekan
 
I modified the above script with the appropriate file paths. I am working on a Windows 2003 server with other Perl scripts which run daily. I changed the first line in the script since this is not on UNIX. It tries to run but does not accomplish the task. I have copied the modified version below. Any further suggestions?
Thank you,

Code:
#!perl -w
#
#drmfilecopy.pl - Copies drm.zip to another directory with date stamp daily
#Provided by Rieekan via tek-tips.com forum. Modified by George Knight OMH IT

use File::Copy;

my (undef,undef,undef,$mday,$mon,$year,undef,undef,undef) = localtime;
$year += 1900;
$mon += 1;

$date = sprintf "%02d/%02d/%04d", $mon, $mday, $year;

copy ('c:\tsm_logs\drm.zip', "c:\tsm_logscopy\drm$date.zip");
 
> copy ('c:\tsm_logs\drm.zip',
> "c:\tsm_logscopy\drm$date.zip");

You may need to change the backslashes to slashes. If that doesn't work, change each \ to \\.
 
change the backslashes to forward slashes, along the same lines as cntr

perl likes / not \

to use back slashes use two of em
\==>\\ \\==>\\\\
This is because the backslash is used to escape special charcters @,$,.,\,|, and of course itself

HTH
--Paul

Nancy Griffith - songstress extraordinaire,
and composer of the snipers anthem "From a distance ...
 
I have changed the backslashes to forward slashes but the file still did not copy. I have commented out all of the lines dealing with the date stamp. Doing this allowed the file to be copied into the other directory. Any ideas on why the date stamp may not have worked. I did not receive any warnings with the date stamp in place.
Thanks
Code:
#!perl -w
#
#drmfilecopy.pl - Copies drm.zip to another directory with date stamp daily
#Provided by Rieekan via tek-tips.com forum. Modified by George Knight OMH IT

use File::Copy;

#my (undef,undef,undef,$mday,$mon,$year,undef,undef,undef) = localtime;
#$year += 1900;
#$mon += 1;

#$date = sprintf "%02d/%02d/%04d", $mon, $mday, $year;

copy ('c:\tsm_logs\drm.zip', "c:\tsm_logscopy\drm$date.zip");
 
try:

Code:
!perl -w

#drmfilecopy.pl - Copies drm.zip to another directory with date stamp daily
#Provided by Rieekan via tek-tips.com forum. Modified by George Knight OMH IT

use File::Copy;

my (undef,undef,undef,$mday,$mon,$year,undef,undef,undef) = localtime;
$year += 1900;
$mon += 1;

$date = sprintf "%02d/%02d/%04d", $mon, $mday, $year;

copy ("c:/tsm_logs/drm.zip", "c/tsm_logscopy/drm".$date.".zip");

I'm assuming you've already created the c:/tsm_logscopy/ folder and that c:/tsm_logs/drm.zip exists??

Rob Waite
 
I believe I have determined why this does not "appear" to be working. $date provides the date in this format 12/09/2004. When it attempts to add this date to the file name it does not work due to the Windows file naming conventions not allowing / or \ and other characters.
Any ideas on how to change the date format or at least changing the / after it is pulled down?
Thanks
 
Oops, yeah, I guess I shouldn't have done that.

Code:
$date = sprintf "%02d-%02d-%04d", $mon, $mday, $year;

That'll teach me to answer questions without my morning dose of caffeine.

- Rieekan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top