Find and Rename directory

Please support our Perl advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2008
Posts: 8
Reputation: viktorijakup is an unknown quantity at this point 
Solved Threads: 0
viktorijakup viktorijakup is offline Offline
Newbie Poster

Find and Rename directory

 
0
  #1
30 Days Ago
Hi !

Sorry for my english !

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

What is wrong with this script:

  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
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 10
Reputation: k_manimuthu is an unknown quantity at this point 
Solved Threads: 2
k_manimuthu k_manimuthu is offline Offline
Newbie Poster
 
0
  #2
29 Days Ago
  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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 9
Reputation: 7stud is an unknown quantity at this point 
Solved Threads: 0
7stud 7stud is offline Offline
Newbie Poster
 
0
  #3
24 Days Ago
Originally Posted by k_manimuthu View Post
  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.
  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; 24 Days Ago at 10:13 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 9
Reputation: 7stud is an unknown quantity at this point 
Solved Threads: 0
7stud 7stud is offline Offline
Newbie Poster
 
0
  #4
24 Days Ago
  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. $
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 8
Reputation: viktorijakup is an unknown quantity at this point 
Solved Threads: 0
viktorijakup viktorijakup is offline Offline
Newbie Poster
 
0
  #5
23 Days Ago
Hi !

Now I have problem:

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

What I must write for it.




Originally Posted by k_manimuthu View Post
  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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 10
Reputation: k_manimuthu is an unknown quantity at this point 
Solved Threads: 2
k_manimuthu k_manimuthu is offline Offline
Newbie Poster
 
0
  #6
22 Days Ago
$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?

  1. my $some_file = (<$to_dir>)[0];
  2. my ($prefix) = $some_file =~ m/(\d+_\d+_\d+)[^\\]*$/;
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the Perl Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC