I am trying to move files and rename them with a date-time stamp I generate. I have a survey and my perl script processes the POST query string. Once I have the query string, I am using XML::Simple to get the stored results, update them, then write them back out.

I don't want to write to the the file I just read from in case there is a problem. I want to back it up. But it doesn't work. Any time I try to add a formatted date time stamp to the name, the operation fails.

Here is the code:

#! perl

use File::Copy;

($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst)=localtime(time);
$stamp = sprintf ("%4d-%02d-%02d %02d:%02d:%02d", $year+1900, $mon+1, $mday, $hour, $min, $sec);

$old_name = 'result.xml';
$new_name = 'results ' . $stamp . '.xml';
copy($old_name, $new_name) or print debug "Error copying.";

The following code does work however:

#! perl

use File::Copy;

($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst)=localtime(time);
$stamp = sprintf ("%4d-%02d-%02d %02d:%02d:%02d", $year+1900, $mon+1, $mday, $hour, $min, $sec);

$old_name = 'result.xml';
$new_name = 'new_results.xml';
copy($old_name, $new_name) or print debug "Error copying.";

Any ideas?

And no this isn't a homework assignment. It's a favor for my mom.

Well, neither pieces of code work on my system. Here's the output for the code you say does work after I ran it with the warnings flag.

Unquoted string "debug" may clash with future reserved word at exts.pl line 11.
Name "main::stamp" used only once: possible typo at exts.pl line 7.
Name "main::wday" used only once: possible typo at exts.pl line 6.
Name "main::isdst" used only once: possible typo at exts.pl line 6.
Name "main::debug" used only once: possible typo at exts.pl line 11.
Name "main::yday" used only once: possible typo at exts.pl line 6.
print() on unopened filehandle debug at exts.pl line 11.

So, you want to post all the code? I don't see how it could work without opening the file handle first. Just asking..

Ok, finally, got it working. Note, I changed the timestamp formatting, taking out the spaces and colon - replaced them with underscores, and escaped the dot in the file name extension.

See if this works for you.

#!/usr/bin/perl -w
use File::Copy;

($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
$stamp = sprintf ("%4d_%02d_%02d_%02d_%02d_%02d", $year+1900, $mon+1, $mday, $hour, $min, $sec);

$old_name = "results.xml";
$new_name = "results_".$stamp."\.xml";
print "Copying $old_name to $new_name\n\n";
copy($old_name, $new_name) or print "Error copying."

Output from running the code:
Copying results.xml to results_2008_03_05_13_38_57.xml

Here's the output file name

results_2008_03_05_13_38_57.xml

Ok. Thanks. Sorry about the typo. I forgot the "open(debug, ">debug.txt")" line.

The following code does work however:

#! perl

use File::Copy;

($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst)=localtime(time);
$stamp = sprintf ("%4d-%02d-%02d %02d:%02d:%02d", $year+1900, $mon+1, $mday, $hour, $min, $sec);

$old_name = 'result.xml';
$new_name = 'new_results.xml';
copy($old_name, $new_name) or print debug "Error copying.";

Any ideas?

And no this isn't a homework assignment. It's a favor for my mom.

You should not use ':' in filename, if you are using Windows.

katharnakh

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.