Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Rename *_valid1.csv to .csv

by Ma (Novice)
on Nov 05, 2013 at 14:00 UTC ( [id://1061306]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I am trying to rename all files having _valid.csv to .csv as follows:
abc_valid1.csv to abc.csv
delta_valid1.csv to delta.csv
I though the following code might do the job:
use File::Copy; my @list = <D:/CPPDaily/SOURCE/SOURCE/HMS/*_valid1.csv>; foreach my $file(@list){ my $from = $_; chomp $from; (my $to = $from) =~ s/_valid1//g; move($from, $to) or die(qq{failed to move $from -> $to}); }
But I am getting unexpected error message:
'Nov 4 08:00,/home/imanager/cppprod/del.csv' and 'Nov 4 08:00,/home/im +anager/cppprod/del.csv' are identical (not copied) at Daily.txt line +80. Use of uninitialized value $atime in utime at c:/Perl/lib/File/Copy.pm + line 393, <FH> line 1. Use of uninitialized value $mtime in utime at c:/Perl/lib/File/Copy.pm + line 393, <FH> line 1. failed to move Nov 4 08:00,/home/imanager/cppprod/del.csv -> Nov 4 08: +00,/home/imanager/cppprod/AR_LM.csv at Daily.txt line 80, <FH> line 1 +.

Replies are listed 'Best First'.
Re: Rename *_valid1.csv to .csv
by Tux (Canon) on Nov 05, 2013 at 14:18 UTC

    But you did not use the code as you posted. What is @list?

    use strict; use warnings; foreach my $vcsv (glob "D:/CPPDaily/SOURCE/SOURCE/HMS/*_valid1.csv") { (my $csv = $vcsv) =~ s/_valid1(\.csv)$/$1/i; if (-e $csv) { warn "$vcsv cannot be renamed: $csv already exists\n"; next; } rename $vcsv, $csv or die "failed to rename $vcsv -> $csv\n"; }

    Enjoy, Have FUN! H.Merijn
      Thank you. Yes, I took the relevant portion from the main program so that its easy to explain. The code you posted worked perfectly, except that it does not overwrite the file if it already exist. Any way to overwrite the file?
        Check the documentation, but I doubt it. You can precede the renamewith an unlinkto work around the limitation, if it exists.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1061306]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-19 19:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found