home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2261 < prev    next >
Internet Message Format  |  1990-12-28  |  4KB

  1. From: tchrist@convex.COM (Tom Christiansen)
  2. Newsgroups: alt.sources
  3. Subject: Re: Neat utility to convert uppercase filenames
  4. Message-ID: <110395@convex.convex.com>
  5. Date: 11 Dec 90 16:04:48 GMT
  6.  
  7. In article <2797@cirrusl.UUCP> dhesi%cirrusl@oliveb.ATC.olivetti.com (Rahul Dhesi) writes:
  8. :I think all the programs posted so far have the bug that if you have
  9. :files called "x" and "X", they will delete one of them.  
  10. :Doesn't disturb files whose names already contain any lowercase
  11. :characters (thus preserving names like "Makefile"), and even has online
  12. :help!  Not shar'd, cut out and use.
  13.  
  14. This is not a bug -- it's a feature.  Anyway, I've added a -i flag to
  15. lwall's rename script to emulate mv's behavior (although I don't believe
  16. in exiting !0 just because you said 'n'.).
  17.  
  18. And this one is as self-documenting as they can come. :-)
  19.  
  20. Again, this is a *generic* filename munging tool.  It'll do anything you
  21. could ever want to the filename, not just what you've thought of today.
  22. The "leave Makefiles alone" isn't special cased in, but see the examples.
  23.  
  24. Again, not sharred, even if it kinda looks like it.  Perl magic, you know.
  25.  
  26. --tom
  27.  
  28. #!/usr/bin/perl
  29. 'di';
  30. 'ig00';
  31. #
  32. # $Header: rename,v 3.0.1.2 90/08/09 03:17:57 lwall Locked $
  33. #
  34. # $Log:    rename,v $
  35. # Revision 3.0.1.2  90/08/09  03:17:57  lwall
  36. # patch19: added man page for relink and rename
  37.  
  38. if ($ARGV[0] eq '-i') {
  39.     shift;
  40.     if (open(TTYIN, "</dev/tty") && open(TTYOUT,">/dev/tty")) {
  41.     $inspect++;
  42.     select((select(TTYOUT),$|=1)[0]);
  43.     } 
  44. }
  45. ($op = shift) || die "Usage: rename [-i] perlexpr [filenames]\n";
  46. if (!@ARGV) {
  47.     @ARGV = <STDIN>;
  48.     chop(@ARGV);
  49. }
  50. for (@ARGV) {
  51.     unless (-e) {
  52.     print STDERR "$0: $_: $!\n";
  53.     $status = 1;
  54.     next;
  55.     } 
  56.     $was = $_;
  57.     eval $op;
  58.     die $@ if $@;
  59.     if ($was ne $_) {
  60.     if ($inspect && -e) {
  61.         print TTYOUT "remove $_? ";
  62.         next unless <TTYIN> =~ /^y/i;
  63.     } 
  64.     unless (rename($was, $_)) {
  65.         print STDERR "$0: can't rename $was to $_: $!\n";
  66.         $status = 1;
  67.     }
  68.     } 
  69. }
  70. exit $status;
  71. ##############################################################################
  72.  
  73.     # These next few lines are legal in both Perl and nroff.
  74.  
  75. .00;            # finish .ig
  76.  
  77. 'di            \" finish diversion--previous line must be blank
  78. .nr nl 0-1        \" fake up transition to first page again
  79. .nr % 0            \" start at page 1
  80. ';<<'.ex'; #__END__ ############# From here on it's a standard manual page ############
  81. .TH RENAME 1 "July 30, 1990"
  82. .AT 3
  83. .SH NAME
  84. rename \- renames multiple files
  85. .SH SYNOPSIS
  86. .B rename [-i] perlexpr [files]
  87. .SH DESCRIPTION
  88. .I Rename
  89. renames the filenames supplied according to the rule specified as the
  90. first argument.
  91. The argument is a Perl expression which is expected to modify the $_
  92. string in Perl for at least some of the filenames specified.
  93. If a given filename is not modified by the expression, it will not be
  94. renamed.
  95. If no filenames are given on the command line, filenames will be read
  96. via standard input.
  97. .PP
  98. The 
  99. .B \-i
  100. flag will prompt to remove the old file first if it exists.  This
  101. flag will be ignored if there is no tty.
  102. .PP
  103. For example, to rename all files matching *.bak to strip the extension,
  104. you might say
  105. .nf
  106.  
  107.     rename 's/\e.bak$//' *.bak
  108.  
  109. .fi
  110. To translate uppercase names to lower, you'd use
  111. .nf
  112.  
  113.     rename 'y/A-Z/a-z/' *
  114.  
  115. .fi
  116. To do the same thing but leave Makefiles unharmed:
  117. .nf
  118.  
  119.     rename 'y/A-Z/a-z/ unless /^Make/' *
  120.  
  121. .fi
  122. To rename all the *.f files to *.BAD, you'd use
  123. .nf
  124.  
  125.     rename 's/\e.f$/.BAD/' *.f
  126.  
  127. .SH ENVIRONMENT
  128. .fi
  129. No environment variables are used.
  130. .SH FILES
  131. .SH AUTHOR
  132. Larry Wall
  133. .SH "SEE ALSO"
  134. mv(1)
  135. .br
  136. perl(1)
  137. .SH DIAGNOSTICS
  138. If you give an invalid Perl expression you'll get a syntax error.
  139. .SH BUGS
  140. .I Rename
  141. does not check for the existence of target filenames, so use with care.
  142. .ex
  143. --
  144. Tom Christiansen        tchrist@convex.com    convex!tchrist
  145. "With a kernel dive, all things are possible, but it sure makes it hard
  146.  to look at yourself in the mirror the next morning."  -me
  147.