944,167 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Unsolved
  • Views: 1946
  • Perl RSS
Nov 10th, 2009
0

Find and Rename directory

Expand Post »
Hi !

Sorry for my english !

My script must rename file name, but don't do it.

What is wrong with this script:

Perl Syntax (Toggle Plain Text)
  1. @rem = '--*-Perl-*--
  2. @echo off
  3. if "%OS%" == "Windows_NT" goto WinNT
  4. perl -x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9
  5. goto endofperl
  6. :WinNT
  7. perl -x -S %0 %*
  8. if NOT "%COMSPEC%" == "%SystemRoot%\system32\cmd.exe" goto endofperl
  9. if %errorlevel% == 9009 echo You do not have Perl in your PATH.
  10. if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul
  11. goto endofperl
  12. @rem ';
  13. #!/usr/bin/perl
  14. use warnings;
  15. use strict;
  16. use File::Path;
  17. use Cwd;
  18. use File::Copy;
  19. use File::Spec::Functions;
  20. #hardcoded
  21. my $FP = 'D:\conv';
  22. my $TP = 'D:\sgml_db';
  23. #my $NAME = 'mmc1';
  24. my ($from_file, $to_file) = ($ARGV[0], $ARGV[0]);
  25. my $cwd = `cd`;
  26. chomp $cwd;
  27. my ($from_dir, $to_dir) = ($cwd, $cwd);
  28. #rename file part
  29. #$to_file =~ s/.*\.(.+)/$NAME.$1/;
  30. my $some_file = (<$to_dir>)[0];
  31. my ($prefix) = $some_file =~ m/(\d+_\d+_\d+)[^\\]*$/;
  32. $to_file =~ s/^(\w+)(\d*)\.(\w+)$/"$prefix_" . uc($1) . ($2 || 0) . "_ESM.$3"/e;
  33. #rename dir part
  34. $to_dir =~ s/^\Q$FP\E/$TP/;
  35. mkpath($to_dir);
  36. copy(catfile($from_dir, $from_file), catfile($to_dir, $to_file))
  37. or die "can't copy file";
  38. exit 0;
  39.  
  40. __END__
  41. :endofperl
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
viktorijakup is offline Offline
8 posts
since Sep 2008
Nov 11th, 2009
0
Re: Find and Rename directory
Perl Syntax (Toggle Plain Text)
  1. my $FP = 'D:\conv';
  2. my $TP = 'D:\sgml_db';
  3. my $cwd = `cd`; # Get current working directory
  4. my ($from_dir, $to_dir) = ($cwd, $cwd);
  5.  
  6. # if your current working directory ($to_dir) doesn't have 'D:\conv' the below line doesn't change the value of $to_dir.
  7. # So $to_dir equals to current directory and it already exists.
  8. # So mkpath($to_dir) doesn't work.
  9.  
  10. $to_dir =~ s/^\Q$FP\E/$TP/;
  11. mkpath($to_dir);

So ensure your $to_dir value must match the $FP value and you will be get the expected output.
Reputation Points: 55
Solved Threads: 24
Junior Poster in Training
k_manimuthu is offline Offline
93 posts
since Jun 2009
Nov 15th, 2009
0
Re: Find and Rename directory
Perl Syntax (Toggle Plain Text)
  1. my $FP = 'D:\conv';
  2. my $TP = 'D:\sgml_db';
  3. my $cwd = `cd`; # Get current working directory
  4. my ($from_dir, $to_dir) = ($cwd, $cwd);
  5.  
  6. # if your current working directory ($to_dir) doesn't have 'D:\conv' the below line doesn't change the value of $to_dir.
  7. # So $to_dir equals to current directory and it already exists.
  8. # So mkpath($to_dir) doesn't work.
  9.  
  10. $to_dir =~ s/^\Q$FP\E/$TP/;
  11. mkpath($to_dir);

So ensure your $to_dir value must match the $FP value and you will be get the expected output.
Perl Syntax (Toggle Plain Text)
  1. $ head -n7 1perl.pl
  2. use strict;
  3. use warnings;
  4. use 5.010;
  5.  
  6.  
  7. my $cwd = `cd`;
  8. say $cwd;
  9.  
  10. $ perl 1perl.pl
  11.  
  12. $
Last edited by 7stud; Nov 15th, 2009 at 10:13 pm.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
7stud is offline Offline
9 posts
since Nov 2009
Nov 15th, 2009
0
Re: Find and Rename directory
Perl Syntax (Toggle Plain Text)
  1. $ head -n7 1perl.pl
  2. use strict;
  3. use warnings;
  4. use 5.010;
  5.  
  6. my $cwd = `pwd`;
  7. say $cwd;
  8.  
  9. $ perl 1perl.pl
  10. /Users/me/2testing/dir1/perl_programs
  11.  
  12. $
Reputation Points: 11
Solved Threads: 0
Newbie Poster
7stud is offline Offline
9 posts
since Nov 2009
Nov 17th, 2009
0
Re: Find and Rename directory
Hi !

Now I have problem:

"Global symbol $prefix" requires explicit package name".

What I must write for it.




Perl Syntax (Toggle Plain Text)
  1. my $FP = 'D:\conv';
  2. my $TP = 'D:\sgml_db';
  3. my $cwd = `cd`; # Get current working directory
  4. my ($from_dir, $to_dir) = ($cwd, $cwd);
  5.  
  6. # if your current working directory ($to_dir) doesn't have 'D:\conv' the below line doesn't change the value of $to_dir.
  7. # So $to_dir equals to current directory and it already exists.
  8. # So mkpath($to_dir) doesn't work.
  9.  
  10. $to_dir =~ s/^\Q$FP\E/$TP/;
  11. mkpath($to_dir);

So ensure your $to_dir value must match the $FP value and you will be get the expected output.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
viktorijakup is offline Offline
8 posts
since Sep 2008
Nov 18th, 2009
0
Re: Find and Rename directory
Quote ...
$to_file =~ s/^(\w+)(\d*)\.(\w+)$/"$prefix_" . uc($1) . ($2 || 0) ."_ESM.$3"/e;
For your output format purpose you added "_". But it treated as a scalar variable. So change "$prefix_" to "$prefix" for the above line.

Hello friend, Is the below lines are working in your program? why you use the lines?

Perl Syntax (Toggle Plain Text)
  1. my $some_file = (<$to_dir>)[0];
  2. my ($prefix) = $some_file =~ m/(\d+_\d+_\d+)[^\\]*$/;
Reputation Points: 55
Solved Threads: 24
Junior Poster in Training
k_manimuthu is offline Offline
93 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: Replace a string in perl
Next Thread in Perl Forum Timeline: Excel formula copy





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC