copyscandirs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/usr/bin/perl -w
  2. # usage: copyscandirs <TargetDir> <ScanDirPattern>
  3. # note that you must not be in <TargetDir> but in the directory in which the
  4. # scna dirs are, which should be moved!
  5. #
  6. # algorithm:
  7. # get list of scan dirs
  8. # parse scan dir names (base name + number)
  9. # check largest number in goal dir
  10. # move and rename the scandirs
  11. use Getopt::Std;
  12. getopts('uhn');
  13. $TargetDir = "exexrangedurscan" ;
  14. $BaseFileName = "scan" ;
  15. if ($opt_u) {
  16. print "usage: copyscandirs <TargetDir> <ScanDirPattern>\n";
  17. }
  18. print "**************************\n";
  19. print "copyscandirs\n";
  20. $NumCmdArgs = $#ARGV+1 ;
  21. print "Number of command line arguments: $NumCmdArgs \n" ;
  22. if ($#ARGV >= 0)
  23. {
  24. $TmpTargetDir = $ARGV[0];
  25. # check existence of target dir: http://www.pageresource.com/cgirec/ptut17.htm
  26. if (-e $TmpTargetDir) {
  27. $TargetDir = $TmpTargetDir ;
  28. } else {
  29. print "TargetDir $TmpTargetDir doesn't exist \n" ;
  30. print " --> using default $TargetDir \n";
  31. }
  32. }
  33. if ($#ARGV >=1)
  34. {
  35. $BaseFileName = $ARGV[1] ;
  36. }
  37. print "TargetDir = $TargetDir \n";
  38. my(@DirNames) = glob "$BaseFileName*" ;
  39. my(@TargetScanDirs) = glob "$TargetDir/$BaseFileName*" ;
  40. $MaxTargetNumber = 0;
  41. foreach $Dir (@TargetScanDirs)
  42. {
  43. # print "$Dir\n" ;
  44. $_ = $Dir ;
  45. $Pattern = "$TargetDir/$BaseFileName";
  46. /$Pattern(.*)/ ;
  47. if ($1 > $MaxTargetNumber) {
  48. $MaxTargetNumber = $1 ;
  49. }
  50. }
  51. print "MaxTargetNumber = $MaxTargetNumber \n";
  52. $NextTargetNumber = $MaxTargetNumber + 1;
  53. foreach $Dir (@DirNames)
  54. {
  55. print "move $Dir to $TargetDir/$BaseFileName$NextTargetNumber\n" ;
  56. system ("mv $Dir $TargetDir/$BaseFileName$NextTargetNumber") ;
  57. ++$NextTargetNumber ;
  58. }