YellowJackets has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

My requirement is to copy a file into two different directories and rename them with the following.

1. filename.txt to be moved from inbox directory to archive directory and filename to be renamed to filename.<dateTime>.txt

I tried to do this by using use Time::Piece; and use File::Copy;

Got dateTime by using "my $toDate = localtime->strftime('%F %T');"

Got archive file name by using "my $archiveFileName = $newArchive."_".$toDate.".txt";"

but when I try to use copy to copy the file to archive directory it wont work.

I am using "copy ($filename, archive\\$archiveFileName);" in code.

2. filename.txt to be moved from inbox directory to out directory and filename to be renamed to filenameout.txt

I was able to do this by using rename.

Can anyone suggest how can we achieve this functionality.

Regards,

Raj

Replies are listed 'Best First'.
Re: copy isn't working
by thanos1983 (Parson) on Mar 07, 2019 at 15:18 UTC

    Hello YellowJackets,

    Welcome to the Monastery.

    Can you provide us with a sample of your code between code tags?

    Update: I just noticed on your command:

    copy ($filename, archive\\$archiveFileName);

    Why you are using the double backslash? what type of operating system are you running the script (WindowsOS or LinuxOS)? I am asking because the slash can be different.

    Update2: Basic troubleshooting from the module documentation:

    copy("sourcefile","destinationfile") or die "Copy failed: $!";

    Add the or die "Copy failed: $!" and you will know why the copying is failing.

    Update3: Most likely the error is coming from the wrong path and also that you need to add double quotes. Last question. Why you are copying a file name $filename to another location e.g. archive\\$archiveFileName? I would assume you want something like that?

    copy($archiveFileName, "archive\\$archiveFileName") or die "Copy failed: $!"

    Looking forward to your update. BR / Thanos

    Seeking for Perl wisdom...on the process of learning...not there...yet!

      Update 1: I am using WindowsOS. \\ worked for rename so using the same for copy as well.

      Update 2: I did try with or die "Copy failed: $!" but returned "Copy failed: at c:\perl_scripts\copy_rename.pl line 33."

      Update 3: I want to archive the file in a different directory with timestamp after processing it. Hope that helps

      Code :

      use strict; use warnings; use Time::Piece; use File::Copy; my $toDate = localtime->strftime('%F %T'); foreach my $file (glob "C:\\data\\app\\AppSpecific1\\In\\test*") { my $archiveFileName = $file."_".$toDate.".txt"; print "copy $file, $archiveFileName\n"; copy ($file, "C:\\data\\app\\AppSpecific1\\Archive\\".$archiveFile +Name) or die "Copy failed: $!"; }

      Compilation error perl c:\perl_scripts\copy_rename.pl copy C:\data\app\AppSpecific1\In\test.txt, C:\data\app\AppSpecific1\In\test.txt_2019-03-07 12:22:44.txt Copy failed: at c:\perl_scripts\copy_rename.pl line 33.

        C:\data\app\AppSpecific1\In\test.txt_2019-03-07 12:22:44.txt

        Windows file names cannot contain colons :. Try using

        my $toDate = localtime->strftime('%Y-%m-%d_%H%M%S'); poj